class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers width, height, n_channels = input_shape self.conv1 = ConvolutionalLayer(n_channels, conv1_channels, 3, 1) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(4, 4) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(4, 4) self.flatten = Flattener() self.fc = FullyConnectedLayer( (height // 4 // 4) * (width // 4 // 4) * conv2_channels, n_output_classes) self.conv1_params = self.conv1.params() self.conv2_params = self.conv2.params() self.fc_params = self.fc.params() def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # Before running forward and backward pass through the model, # clear parameter gradients aggregated from the previous pass for key, value in self.params().items(): value.grad.fill(0) # TODO Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment conv1 = self.conv1.forward(X) relu1 = self.relu1.forward(conv1) maxpool1 = self.maxpool1.forward(relu1) conv2 = self.conv2.forward(maxpool1) relu2 = self.relu2.forward(conv2) maxpool2 = self.maxpool2.forward(relu2) flatten = self.flatten.forward(maxpool2) fc = self.fc.forward(flatten) loss, d_preds = softmax_with_cross_entropy(fc, y) fc = self.fc.backward(d_preds) flatten = self.flatten.backward(fc) maxpool2 = self.maxpool2.backward(flatten) relu2 = self.relu2.backward(maxpool2) conv2 = self.conv2.backward(relu2) maxpool1 = self.maxpool1.backward(conv2) relu1 = self.relu1.backward(maxpool1) conv1 = self.conv1.backward(relu1) return loss def predict(self, X): # You can probably copy the code from previous assignment conv1 = self.conv1.forward(X) relu1 = self.relu1.forward(conv1) maxpool1 = self.maxpool1.forward(relu1) conv2 = self.conv2.forward(maxpool1) relu2 = self.relu2.forward(conv2) maxpool2 = self.maxpool2.forward(relu2) flatten = self.flatten.forward(maxpool2) fc = self.fc.forward(flatten) return np.argmax(fc, axis=1) def params(self): result = {} # TODO: Aggregate all the params from all the layers # which have parameters d1 = {k + '1': v for k, v in self.conv1_params.items()} d2 = {k + '2': v for k, v in self.conv2_params.items()} d3 = {k + '3': v for k, v in self.fc_params.items()} result = {**d1, **d2, **d3} return result
class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ filter_size = 3 padding = 1 pool_size = 4 stride = 4 width, height, n_channels = input_shape assert ((height + 2 * padding - filter_size + 1) % pool_size == 0) assert ((width + 2 * padding - filter_size + 1) % pool_size == 0) height = (height + 2 * padding - filter_size + 1) // pool_size width = (width + 2 * padding - filter_size + 1) // pool_size assert ((height + 2 * padding - filter_size + 1) % pool_size == 0) assert ((width + 2 * padding - filter_size + 1) % pool_size == 0) height = (height + 2 * padding - filter_size + 1) // pool_size width = (width + 2 * padding - filter_size + 1) // pool_size # TODO Create necessary layers self.Conv_1 = ConvolutionalLayer(n_channels, conv1_channels, filter_size, padding) self.Relu_1 = ReLULayer() self.Maxpool_1 = MaxPoolingLayer(pool_size, stride) self.Conv_2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding) self.Relu_2 = ReLULayer() self.Maxpool_2 = MaxPoolingLayer(pool_size, stride) self.Flattener = Flattener() self.FC = FullyConnectedLayer(height * width * conv2_channels, n_output_classes) def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # Before running forward and backward pass through the model, # clear parameter gradients aggregated from the previous pass for param in self.params().values(): param.grad = np.zeros_like(param.value) # TODO Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment conv_1 = self.Conv_1.forward(X) relu_1 = self.Relu_1.forward(conv_1) maxpool_1 = self.Maxpool_1.forward(relu_1) conv_2 = self.Conv_2.forward(maxpool_1) relu_2 = self.Relu_2.forward(conv_2) maxpool_2 = self.Maxpool_2.forward(relu_2) flat = self.Flattener.forward(maxpool_2) fc = self.FC.forward(flat) loss, grad = softmax_with_cross_entropy(fc, y + 1) d_fc = self.FC.backward(grad) d_flat = self.Flattener.backward(d_fc) d_maxpool_2 = self.Maxpool_2.backward(d_flat) d_relu_2 = self.Relu_2.backward(d_maxpool_2) d_conv_2 = self.Conv_2.backward(d_relu_2) d_maxpool_1 = self.Maxpool_1.backward(d_conv_2) d_relu_1 = self.Relu_1.backward(d_maxpool_1) dX = self.Conv_1.backward(d_relu_1) return loss def predict(self, X): # You can probably copy the code from previous assignment # predictions = self.fc2.forward(self.ReLU.forward(self.fc1.forward(X))) predictions = self.FC.forward( self.Flattener.forward( self.Maxpool_2.forward( self.Relu_2.forward( self.Conv_2.forward( self.Maxpool_1.forward( self.Relu_1.forward( self.Conv_1.forward(X)))))))) pred = np.argmax(predictions, axis=1) return pred def params(self): result = { 'conv1_W': self.Conv_1.params()['W'], 'conv1_B': self.Conv_1.params()['B'], 'conv2_W': self.Conv_2.params()['W'], 'conv2_B': self.Conv_2.params()['B'], 'fc_W': self.FC.params()['W'], 'fc_B': self.FC.params()['B'], } # TODO: Aggregate all the params from all the layers # which have parameters return result
class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.height = input_shape[0] self.width = input_shape[1] self.input_channels = input_shape[2] self.n_output_classes = n_output_classes self.conv1_channels = conv1_channels self.conv2_channels = conv2_channels self.conv1_layer = ConvolutionalLayer(self.input_channels, self.conv1_channels, 3, 1) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(4, 4) self.conv2_layer = ConvolutionalLayer(self.conv1_channels, self.conv2_channels, 3, 1) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(4, 4) self.flattener = Flattener() self.fc_layer = FullyConnectedLayer(2*2*self.conv2_channels, self.n_output_classes) def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # nullify layers gradients # Conv1 Layer self.params()['W1'].grad = np.zeros((3, 3, self.input_channels, self.conv1_channels)) self.params()['B1'].grad = np.zeros(self.conv1_channels) # Conv2 Layer self.params()['W2'].grad = np.zeros((3, 3, self.conv1_channels, self.conv2_channels)) self.params()['B2'].grad = np.zeros(self.conv2_channels) # FC Layer self.params()['W3'].grad = np.zeros((2*2*self.conv2_channels, self.n_output_classes)) self.params()['B3'].grad = np.zeros((1, self.n_output_classes)) # forward conv layer 1 conv_forward1 = self.conv1_layer.forward(X) # forward relu activation funtcion 1 relu_forward1 = self.relu1.forward(conv_forward1) # forward maxpool layer 1 maxpool_forward1 = self.maxpool1.forward(relu_forward1) # forward conv layer 2 conv_forward2 = self.conv2_layer.forward(maxpool_forward1) # forward relu activation funtcion 2 relu_forward2 = self.relu2.forward(conv_forward2) # forward maxpool layer 2 maxpool_forward2 = self.maxpool2.forward(relu_forward2) # forward flattener layer flattener_forward = self.flattener.forward(maxpool_forward2) # forward FC layer fc_forward = self.fc_layer.forward(flattener_forward) # calculate loss and grad loss, grad = softmax_with_cross_entropy(fc_forward, y) # backward FC layer fc_backward = self.fc_layer.backward(grad) # backward flattener layer flattener_backward = self.flattener.backward(fc_backward) # backward maxpool layer 2 maxpool_backward2 = self.maxpool2.backward(flattener_backward) # backward relu activation funtcion 2 relu_backward2 = self.relu2.backward(maxpool_backward2) # forward conv layer 2 conv_backward2 = self.conv2_layer.backward(relu_backward2) # backward maxpool layer 1 maxpool_backward1 = self.maxpool1.backward(conv_backward2) # backward relu activation funtcion 1 relu_backward1 = self.relu1.backward(maxpool_backward1) # forward conv layer 1 conv_backward1 = self.conv1_layer.backward(relu_backward1) return loss def predict(self, X): # forward conv layer 1 conv_forward1 = self.conv1_layer.forward(X) # forward relu activation funtcion 1 relu_forward1 = self.relu1.forward(conv_forward1) # forward maxpool layer 1 maxpool_forward1 = self.maxpool1.forward(relu_forward1) # forward conv layer 2 conv_forward2 = self.conv2_layer.forward(maxpool_forward1) # forward relu activation funtcion 2 relu_forward2 = self.relu2.forward(conv_forward2) # forward maxpool layer 2 maxpool_forward2 = self.maxpool2.forward(relu_forward2) # forward flattener layer flattener_forward = self.flattener.forward(maxpool_forward2) # forward FC layer fc_forward = self.fc_layer.forward(flattener_forward) # make prediction prediciton = fc_forward.argmax(axis=1) return prediciton def params(self): result = {'W1': self.conv1_layer.params()['W'], 'B1': self.conv1_layer.params()['B'], 'W2': self.conv2_layer.params()['W'], 'B2': self.conv2_layer.params()['B'], 'W3': self.fc_layer.params()['W'], 'B3': self.fc_layer.params()['B']} return result
class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO_ Create necessary layers # raise Exception("Not implemented!") weight, height, cannels = input_shape filter_size = 3 pool_size = 4 padding = 1 stride = pool_size self.conv1 = ConvolutionalLayer(cannels, conv1_channels, filter_size, padding) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(pool_size, stride) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(pool_size, stride) self.flatten = Flattener() n_fc_input = int(height / pool_size / pool_size * weight / pool_size / pool_size * conv2_channels) self.fc = FullyConnectedLayer(n_fc_input, n_output_classes) def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # Before running forward and backward pass through the model, # clear parameter gradients aggregated from the previous pass # TODO_ Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment # raise Exception("Not implemented!") # initialization params = self.params() W1 = params["W1"] B1 = params["B1"] W2 = params["W2"] B2 = params["B2"] W3 = params["W3"] B3 = params["B3"] # the cleaning of params # W1.value = np.zeros_like(W1.value) # B1.value = np.zeros_like(B1.value) # W2.value = np.zeros_like(W2.value) # B2.value = np.zeros_like(B2.value) # W3.value = np.zeros_like(W3.value) # B3.value = np.zeros_like(B3.value) # the cleaning of gradients W1.grad = np.zeros_like(W1.value) B1.grad = np.zeros_like(B1.value) W2.grad = np.zeros_like(W2.value) B2.grad = np.zeros_like(B2.value) W3.grad = np.zeros_like(W3.value) B3.grad = np.zeros_like(B3.value) # forward pass out = self.conv1.forward(X) out = self.relu1.forward(out) out = self.maxpool1.forward(out) out = self.conv2.forward(out) out = self.relu2.forward(out) out = self.maxpool2.forward(out) out = self.flatten.forward(out) out = self.fc.forward(out) loss, d_preds = softmax_with_cross_entropy(out, y) # backward pass d_out = self.fc.backward(d_preds) d_out = self.flatten.backward(d_out) d_out = self.maxpool2.backward(d_out) d_out = self.relu2.backward(d_out) d_out = self.conv2.backward(d_out) d_out = self.maxpool1.backward(d_out) d_out = self.relu1.backward(d_out) d_out = self.conv1.backward(d_out) return loss def predict(self, X): # You can probably copy the code from previous assignment # raise Exception("Not implemented!") out = self.conv1.forward(X) out = self.relu1.forward(out) out = self.maxpool1.forward(out) out = self.conv2.forward(out) out = self.relu2.forward(out) out = self.maxpool2.forward(out) out = self.flatten.forward(out) out = self.fc.forward(out) probs = softmax(out) y_pred = np.argmax(probs, axis=1) return y_pred def params(self): result = {} # TODO_: Aggregate all the params from all the layers # which have parameters # raise Exception("Not implemented!") result = { "W1": self.conv1.params()["W"], "B1": self.conv1.params()["B"], "W2": self.conv2.params()["W"], "B2": self.conv2.params()["B"], "W3": self.fc.params()["W"], "B3": self.fc.params()["B"] } return result
class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers image_width, image_height, n_channels = input_shape conv_padding = 0 conv_filter_size = 3 max_pool_size = 4 max_pool_stride = 1 conv1_output_size = image_width - conv_filter_size + 1 maxpool1_output_size = int( (conv1_output_size - max_pool_size) / max_pool_stride) + 1 conv2_output_size = maxpool1_output_size - conv_filter_size + 1 maxpool2_output_size = int( (conv2_output_size - max_pool_size) / max_pool_stride) + 1 # correct if height == width !!! fc_input_size = maxpool2_output_size * maxpool2_output_size * conv2_channels self.conv1 = ConvolutionalLayer(n_channels, conv1_channels, conv_filter_size, conv_padding) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(max_pool_size, max_pool_stride) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, conv_filter_size, conv_padding) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(max_pool_size, max_pool_stride) self.flattener = Flattener() self.fc = FullyConnectedLayer(fc_input_size, n_output_classes) def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # Before running forward and backward pass through the model, # clear parameter gradients aggregated from the previous pass # TODO Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment self._zero_grad() predictions = self._forward(X) loss, dprediction = softmax_with_cross_entropy(predictions, y) self._backward(dprediction) return loss def _zero_grad(self): for param in self.params().values(): param.grad = np.zeros_like(param.value) def _forward(self, X): output = self.conv1.forward(X) output = self.relu1.forward(output) output = self.maxpool1.forward(output) output = self.conv2.forward(output) output = self.relu2.forward(output) output = self.maxpool2.forward(output) output = self.flattener.forward(output) output = self.fc.forward(output) return output def _backward(self, dprediction): grad = self.fc.backward(dprediction) grad = self.flattener.backward(grad) grad = self.maxpool2.backward(grad) grad = self.relu2.backward(grad) grad = self.conv2.backward(grad) grad = self.maxpool1.backward(grad) grad = self.relu1.backward(grad) self.conv1.backward(grad) def predict(self, X): # You can probably copy the code from previous assignment predictions = self._forward(X) y_pred = np.argmax(softmax(predictions), axis=1) return y_pred def params(self): result = {} # TODO: Aggregate all the params from all the layers # which have parameters for k, v in self.conv1.params().items(): result["".join(["conv1_", k])] = v for k, v in self.conv2.params().items(): result["".join(["conv2_", k])] = v for k, v in self.fc.params().items(): result["".join(["fc_", k])] = v return result
class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels, reg=0): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ image_width, image_height, n_channels = input_shape padding_1 = 1 padding_2 = 1 filter_size_1 = 3 filter_size_2 = 3 pooling_size_1 = 4 pooling_size_2 = 4 stride_1 = 4 stride_2 = 4 height = image_height + 2 * padding_1 width = image_width + 2 * padding_1 out_height = height - filter_size_1 + 1 out_width = width - filter_size_1 + 1 #print(height, width, filter_size_1, out_height, out_width); assert (out_height - pooling_size_1) % stride_1 == 0 assert (out_width - pooling_size_1) % stride_1 == 0 height = out_height width = out_width out_height = int((height - pooling_size_1) / stride_1 + 1) out_width = int((width - pooling_size_1) / stride_1 + 1) #print(height, width, pooling_size_1, out_height, out_width); height = out_height + 2 * padding_2 width = out_width + 2 * padding_2 out_height = height - filter_size_2 + 1 out_width = width - filter_size_2 + 1 #print(height, width, filter_size_2, out_height, out_width); assert (out_height - pooling_size_2) % stride_2 == 0 assert (out_width - pooling_size_2) % stride_2 == 0 height = out_height width = out_width out_height = int((height - pooling_size_2) / stride_2 + 1) out_width = int((width - pooling_size_2) / stride_2 + 1) #print(height, width, pooling_size_2, out_height, out_width); # TODO Create necessary layers self.Conv_first = ConvolutionalLayer(n_channels, conv1_channels, filter_size_1, padding_1) self.Relu_first = ReLULayer() self.Maxpool_first = MaxPoolingLayer(pooling_size_1, stride_1) self.Conv_second = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size_2, padding_2) self.Relu_second = ReLULayer() self.Maxpool_second = MaxPoolingLayer(pooling_size_2, stride_2) self.Flattener = Flattener() self.FC = FullyConnectedLayer(out_height * out_width * conv2_channels, n_output_classes) self.n_output = n_output_classes self.reg = reg #print(out_height*out_width*conv2_channels, n_output_classes); def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # Before running forward and backward pass through the model, # clear parameter gradients aggregated from the previous pass for param in self.params(): self.params()[param].grad = np.zeros_like( self.params()[param].grad) # TODO Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment #assert check_layer_gradient(self.Conv_first, X); #assert check_layer_param_gradient(self.Conv_first, X, 'W') #print("X = ", X); #print("X_end = ", X[:, 25:, 25:, :]); X1 = self.Conv_first.forward(X) #print(self.Conv_first.W.value); #print("X1 = ", X1); #print("W = ", self.Conv_first.params()['W'].value); #assert check_layer_gradient(self.Relu_first, X1); X1_Relu = self.Relu_first.forward(X1) #print("X1_Relu = ", X1_Relu); #assert check_layer_gradient(self.Maxpool_first, X1_Relu); X1_Max = self.Maxpool_first.forward(X1_Relu) #assert check_layer_gradient(self.Conv_second, X1_Max); X2 = self.Conv_second.forward(X1_Max) #assert check_layer_gradient(self.Relu_second, X2); X2_Relu = self.Relu_second.forward(X2) #assert check_layer_gradient(self.Maxpool_second, X2_Relu); X2_Max = self.Maxpool_second.forward(X2_Relu) #assert check_layer_gradient(self.Flattener, X2_Max); X3 = self.Flattener.forward(X2_Max) #assert check_layer_gradient(self.FC, X3); X3_FC = self.FC.forward(X3) loss, dX3_FC = softmax_with_cross_entropy(X3_FC, y + 1) dX3 = self.FC.backward(dX3_FC) dX2_Max = self.Flattener.backward(dX3) dX2_Relu = self.Maxpool_second.backward(dX2_Max) #print("dX2_Max = ", dX2_Max); #print("dX2_Relu = ", dX2_Relu); dX2 = self.Relu_second.backward(dX2_Relu) dX1_Max = self.Conv_second.backward(dX2) dX1_Relu = self.Maxpool_first.backward(dX1_Max) dX1 = self.Relu_first.backward(dX1_Relu) dX = self.Conv_first.backward(dX1) reg_loss_w, reg_grad_w = l2_regularization(self.FC.W.value, self.reg) reg_loss_b, reg_grad_b = l2_regularization(self.FC.B.value, self.reg) loss += (reg_loss_w + reg_loss_b) self.FC.W.grad += reg_grad_w return loss def predict(self, X): # You can probably copy the code from previous assignment pred = np.zeros(X.shape[0], np.int) predictions = self.FC.forward( self.Flattener.forward( self.Maxpool_second.forward( self.Relu_second.forward( self.Conv_second.forward( self.Maxpool_first.forward( self.Relu_first.forward( self.Conv_first.forward(X)))))))) #print("predictions = ", predictions); i = 0 for predict in predictions: values = [softmax_with_cross_entropy(predict, target_index + 1)[0] \ for target_index in range(self.n_output)] pred[i] = min(range(len(values)), key=values.__getitem__) i += 1 #print("pred = ", pred); return pred def params(self): result = {} # TODO: Aggregate all the params from all the layers # which have parameters result = {} dict_first = self.Conv_first.params() dict_second = self.Conv_second.params() dict_FC = self.FC.params() # TODO Implement aggregating all of the params for key in dict_first.keys(): result[key + 'C1'] = dict_first[key] for key in dict_second.keys(): result[key + 'C2'] = dict_second[key] for key in dict_FC.keys(): result[key + 'F1'] = dict_FC[key] return result
class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels, reg): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.reg = reg self.conv1 = ConvolutionalLayer(in_channels=input_shape[-1], out_channels=conv1_channels, filter_size=3, padding=1) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(pool_size=4, stride=4) self.conv2 = ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(pool_size=4, stride=4) self.flattener = Flattener() ## n_input = 4*conv2_channels - hard coding here, because of constant picture size 32 32 3 self.fullyconlayer = FullyConnectedLayer(n_input=4 * conv2_channels, n_output=n_output_classes) self.W_fc_layer = None self.B_fc_layer = None self.W_con1_layer = None self.B_con1_layer = None self.W_con2_layer = None self.B_con2_layer = None # TODO Create necessary layers #raise Exception("Not implemented!") def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # Before running forward and backward pass through the model, # clear parameter gradients aggregated from the previous pass # TODO Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment self.conv1.params()['W'].grad = 0 self.conv1.params()['B'].grad = 0 self.conv2.params()['W'].grad = 0 self.conv2.params()['B'].grad = 0 self.fullyconlayer.params()['W'].grad = 0 self.fullyconlayer.params()['B'].grad = 0 to_relu = self.conv1.forward(X) to_maxpool1 = self.relu1.forward(to_relu) to_conv2 = self.maxpool1.forward(to_maxpool1) to_relu2 = self.conv2.forward(to_conv2) to_maxpool2 = self.relu2.forward(to_relu2) to_flat = self.maxpool2.forward(to_maxpool2) to_fc_layer = self.flattener.forward(to_flat) preds = self.fullyconlayer.forward(to_fc_layer) loss, dprediction = softmax_with_cross_entropy(preds, y) grad_from_fc_layer = self.fullyconlayer.backward(dprediction) self.W_fc_layer = self.fullyconlayer.params()['W'] self.B_fc_layer = self.fullyconlayer.params()['B'] grad_from_flatten = self.flattener.backward(grad_from_fc_layer) grad_from_maxpool2 = self.maxpool2.backward(grad_from_flatten) grad_from_relu2 = self.relu2.backward(grad_from_maxpool2) grad_from_conv2 = self.conv2.backward(grad_from_relu2) self.W_con2_layer = self.conv2.params()['W'] self.B_con2_layer = self.conv2.params()['B'] grad_from_maxpool1 = self.maxpool1.backward(grad_from_conv2) grad_from_relu1 = self.relu1.backward(grad_from_maxpool1) grad_from_conv1 = self.conv1.backward(grad_from_relu1) self.W_con1_layer = self.conv1.params()['W'] self.B_con1_layer = self.conv1.params()['B'] loss_fc, grad_fc = l2_regularization(self.W_fc_layer.value, self.reg) loss += loss_fc self.W_fc_layer.grad += grad_fc return loss #raise Exception("Not implemented!") def predict(self, X): # You can probably copy the code from previous assignment pred = np.zeros(X.shape[0], np.int) to_relu = self.conv1.forward(X) to_maxpool1 = self.relu1.forward(to_relu) to_conv2 = self.maxpool1.forward(to_maxpool1) to_relu2 = self.conv2.forward(to_conv2) to_maxpool2 = self.relu2.forward(to_relu2) to_flat = self.maxpool2.forward(to_maxpool2) to_fc_layer = self.flattener.forward(to_flat) preds = self.fullyconlayer.forward(to_fc_layer) probs = softmax(preds) pred = np.argmax(probs, axis=-1) return pred #raise Exception("Not implemented!") def params(self): result = { 'W_fc_layer': self.W_fc_layer, 'B_fc_layer': self.B_fc_layer, 'W_con1_layer': self.W_con1_layer, 'B_con1_layer': self.B_con1_layer, 'W_con2_layer': self.W_con2_layer, 'B_con2_layer': self.B_con2_layer } # TODO: Aggregate all the params from all the layers # which have parameters #raise Exception("Not implemented!") return result
class ConvNet: """ Implements a very simple conv net Input -> Conv[3x3] -> Relu -> Maxpool[4x4] -> Conv[3x3] -> Relu -> MaxPool[4x4] -> Flatten -> FC -> Softmax """ def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.conv1 = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1) self.reLu1 = ReLULayer() self.mxPl1 = MaxPoolingLayer(4, 4) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.reLu2 = ReLULayer() self.mxPl2 = MaxPoolingLayer(4, 4) self.flat = Flattener() self.fCL = FullyConnectedLayer(4 * conv2_channels, n_output_classes) def compute_loss_and_gradients(self, X, y): """ Computes total loss and updates parameter gradients on a batch of training examples Arguments: X, np array (batch_size, height, width, input_features) - input data y, np array of int (batch_size) - classes """ # Before running forward and backward pass through the model, # clear parameter gradients aggregated from the previous pass # TODO Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment for param in self.params().values(): param.grad = np.zeros_like(param.value) # TODO Compute loss and fill param gradients # Don't worry about implementing L2 regularization, we will not # need it in this assignment # raise Exception("Not implemented!") pred = self.conv1.forward(X) pred = self.reLu1.forward(pred) pred = self.mxPl1.forward(pred) pred = self.conv2.forward(pred) pred = self.reLu2.forward(pred) pred = self.mxPl2.forward(pred) pred = self.flat.forward(pred) pred = self.fCL.forward(pred) loss, loss_grad = softmax_with_cross_entropy(pred, y) grad = self.fCL.backward(loss_grad) grad = self.flat.backward(grad) grad = self.mxPl2.backward(grad) grad = self.reLu2.backward(grad) grad = self.conv2.backward(grad) grad = self.mxPl1.backward(grad) grad = self.reLu1.backward(grad) grad = self.conv1.backward(grad) return loss def predict(self, X): # You can probably copy the code from previous assignment pred = self.conv1.forward(X) pred = self.reLu1.forward(pred) pred = self.mxPl1.forward(pred) pred = self.conv2.forward(pred) pred = self.reLu2.forward(pred) pred = self.mxPl2.forward(pred) pred = self.flat.forward(pred) pred = self.fCL.forward(pred) pred = np.argmax(pred, axis=1) return pred def params(self): result = {} result['Conv1W'] = self.conv1.params()['W'] result['Conv2W'] = self.conv2.params()['W'] result['FC_W'] = self.fCL.params()['W'] result['Conv1B'] = self.conv1.params()['B'] result['Conv2B'] = self.conv2.params()['B'] result['FC_B'] = self.fCL.params()['B'] return result