0% found this document useful (0 votes)
4 views10 pages

Convolution Neural Network

The document defines a ConvLayer class for a convolutional neural network, including methods for forward and backward propagation. It initializes weights and biases, performs convolution operations, and computes gradients for weight updates. Additionally, it includes code for training a CNN model and evaluating its performance on a dataset.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Convolution Neural Network

The document defines a ConvLayer class for a convolutional neural network, including methods for forward and backward propagation. It initializes weights and biases, performs convolution operations, and computes gradients for weight updates. Additionally, it includes code for training a CNN model and evaluating its performance on a dataset.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class ConvLayer:

def __init__(self, in_channels, out_channels, kernel_size, stride = 1, padding=0):


[Link] = in_channels
[Link] = out_channels
self.kernel_size = kernel_size
[Link] = padding
[Link] = stride

f_in = in_channels * kernel_size * kernel_size


f_out = out_channels * kernel_size * kernel_size
limit = [Link](6.0/(f_in + f_out))

w_shape = ([Link], [Link], kernel_size, kernel_size)

self.W = [Link](-limit, limit, w_shape) # (OC, IC, K, K)


self.B = [Link](([Link], 1)) # (OC, 1)

self.inp_cache = None
[Link] = None
[Link] = None

def forward(self, x):


# Conv Layer
N, C, H, W = [Link]

xp = [Link](x)
oh, ow = self.out_size(H), self.out_size(W)
out = [Link]((N, [Link], oh, ow))
self.inp_cache = x

for b in range(N):
for oc in range([Link]):
for i in range(oh):
for j in range(ow):
h_start = i * [Link]
w_start = j * [Link]
h_end = h_start + self.kernel_size
w_end = w_start + self.kernel_size
region = xp[b, :, h_start:h_end, w_start:w_end] # (N, IC, K, K)
out[b, oc, i, j] = [Link](region * self.W[oc]) + self.B[oc].item()
return out

def backward(self, dout):


X = self.inp_cache
N, C, H, W = [Link]
xp = [Link](X)

_, _, oh, ow = [Link]

[Link] = np.zeros_like(self.W)
[Link] = np.zeros_like(self.B)
dxp = np.zeros_like(xp)

for b in range(N):
for oc in range([Link]):
for i in range(oh):
for j in range(ow):
h_start = i * [Link]
w_start = j * [Link]
h_end = h_start + self.kernel_size
w_end = w_start + self.kernel_size

# gradiesnt wrt tp weigths self.W


region = xp[b, :, h_start:h_end, w_start:w_end] # (N, IC, K, K)
[Link][oc] += dout[b, oc, i, j] * region

# gradiesnt wrt to bias term self.B


[Link][oc] += dout[b, oc, i, j]

# gradiesnt wrt. to padded x i.e., xp


dxp[b, :, h_start:h_end, w_start:w_end] = dout[b, oc, i, j] * self

dx = [Link](dxp)
return dx
x = [Link][:4500, 1:]
y = [Link][:4500, 0]
x = [Link](-1, 1, 28, 28)
y = one_hot(y, 10)

[Link], [Link]
model = CNN()
model, tl, va = train(model, x[:3000], y[:3000], lr = 0.001, epochs=25)
visualize_results(tl, va)
evaluate(model, x[3000:4000], y[3000:4000])

for i in range(4000, 4500, 40):


plot(i)

You might also like