def __init__(self, n_input, n_output, hidden_layer_size, reg): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict hidden_layer_size, int - number of neurons in the hidden layer reg, float - L2 regularization strength """ self.reg = reg self.dense_1 = FullyConnectedLayer(n_input, hidden_layer_size) self.dense_2 = FullyConnectedLayer(hidden_layer_size, n_output) self.relu_1 = ReLULayer() self.relu_2 = ReLULayer()
def __init__(self, n_input, n_output, hidden_layer_size, reg): """ Initializes the neural network Arguments: hidden_layer_size, int - number of neurons in the hidden layer n_input, int - dimension of the model input n_output, int - number of classes to predict reg, float - L2 regularization strength """ self.reg = reg # TODO Create necessary layers self.first = FullyConnectedLayer(n_input, hidden_layer_size) self.ReLU = ReLULayer() self.second = FullyConnectedLayer(hidden_layer_size, n_output) self.n_output = n_output
def __init__(self, n_input, n_output, hidden_layer_size, reg): self.layers = [ FullyConnectedLayer(n_input, hidden_layer_size), ReLULayer(), FullyConnectedLayer(hidden_layer_size, n_output) ] self.reg = reg
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, 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 Set parameter gradient to zeros # Hint: using self.params() might be useful! params1 = self.fc1.params() params2 = self.fc2.params() for key in ['W', 'B']: params1[key].grad.fill(0) params2[key].grad.fill(0) # TODO Compute loss and fill param gradients # by running forward and backward passes through the model self.relu1 = ReLULayer() x = self.relu1.forward(self.fc1.forward(X)) y_pred = self.fc2.forward(x) loss, dpred = softmax_with_cross_entropy(y_pred, y) dout = self.fc2.backward(dpred) dout = self.relu1.backward(dout) dout = self.fc1.backward(dout) # After that, implement l2 regularization on all params # Hint: self.params() is useful again! loss_fc1_W_reg, grad_fc1_W_reg = l2_regularization( params1['W'].value, self.reg) loss_fc1_B_reg, grad_fc1_B_reg = l2_regularization( params1['B'].value, self.reg) loss_fc2_W_reg, grad_fc2_W_reg = l2_regularization( params2['W'].value, self.reg) loss_fc2_B_reg, grad_fc2_B_reg = l2_regularization( params2['B'].value, self.reg) self.fc2.W.grad += grad_fc2_W_reg self.fc2.B.grad += grad_fc2_B_reg self.fc1.W.grad += grad_fc1_W_reg self.fc1.B.grad += grad_fc1_B_reg return loss + (loss_fc1_W_reg + loss_fc1_B_reg + loss_fc2_W_reg + loss_fc2_B_reg)
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 self.model = [ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1), ReLULayer(), MaxPoolingLayer(4, 4),\ ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1), ReLULayer(),\ MaxPoolingLayer(4, 4), Flattener(), FullyConnectedLayer(np.prod(input_shape[:-1]) // 256 * conv2_channels,\ n_output_classes)]
def __init__(self, n_input, n_output, hidden_layer_size, reg): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict hidden_layer_size, int - number of neurons in the hidden layer reg, float - L2 regularization strength """ self.reg = reg # TODO Create necessary layers #raise Exception("Not implemented!") self.layer1 = FullyConnectedLayer(n_input, hidden_layer_size) self.relu_layer = ReLULayer() self.layer2 = FullyConnectedLayer(hidden_layer_size, n_output)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels, filter_size=3): """ 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.input_shape = input_shape self.n_output_classes = n_output_classes self.conv1_channels = conv1_channels self.conv2_channels = conv2_channels self.filter_size = filter_size self.padding = 1 c1 = int( (input_shape[0] - self.filter_size + 2 * self.padding) / 1) + 1 mp1 = int((c1 - 4) / 4) + 1 c2 = int((mp1 - self.filter_size + 2 * self.padding) / 1) + 1 self.size_after_2maxpool = int((c2 - 4) / 4) + 1 self.RL1 = ReLULayer() self.RL2 = ReLULayer() self.MaxPool1 = MaxPoolingLayer(pool_size=4, stride=4) self.MaxPool2 = MaxPoolingLayer(pool_size=4, stride=4) self.Flatten = Flattener() self.Conv1 = ConvolutionalLayer(in_channels=self.input_shape[-1], out_channels=conv1_channels, filter_size=self.filter_size, padding=self.padding) self.Conv2 = ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=self.filter_size, padding=self.padding) self.FC = FullyConnectedLayer(n_input=conv2_channels * self.size_after_2maxpool**2, n_output=self.n_output_classes)
def ensure_layers(self): if self.layers is None: self.layers = [ ("Input Layer", FullyConnectedLayer(self.n_input, self.hidden_layer_size)), ("ReLU Layer", ReLULayer()), ("Hidden Layer", FullyConnectedLayer(self.hidden_layer_size, self.n_output)), ]
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, im_channels = input_shape out_height_conv1 = int((height - 3 + 2 * 1) / 1 + 1) out_width_conv1 = int((width - 3 + 2 * 1) / 1 + 1) out_height_maxpool1 = int((out_height_conv1 - 4) / 2 + 1) out_width_maxpool1 = int((out_width_conv1 - 4) / 2 + 1) out_height_conv2 = int((out_height_maxpool1 - 3 + 2 * 1) / 1 + 1) out_width_conv2 = int((out_width_maxpool1 - 3 + 2 * 1) / 1 + 1) out_height_maxpool2 = int((out_height_conv2 - 4) / 2 + 1) out_width_maxpool2 = int((out_width_conv2 - 4) / 2 + 1) neurons_in_fc = out_height_maxpool2 * out_width_maxpool2 * conv2_channels self.sequential = [ ConvolutionalLayer(im_channels, conv1_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(pool_size=4, stride=2), ConvolutionalLayer(conv1_channels, conv2_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(pool_size=4, stride=2), Flattener(), FullyConnectedLayer(neurons_in_fc, n_output_classes) ]
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 # padding=0, stride=1 self.padding = 0 self.stride = 1 self.filter_size = 3 self.pool_size = 4 self.conv1_out_size = int((input_shape[0] - self.filter_size + 2 * self.padding) / self.stride + 1) self.max_pool1_out_size = int((self.conv1_out_size - self.pool_size) / self.stride + 1) self.conv2_out_size = int((self.max_pool1_out_size - self.filter_size + 2 * self.padding) / self.stride + 1) self.max_pool2_out_size = int((self.conv2_out_size - self.pool_size) / self.stride + 1) # padding=0, stride=1 self.sequence = [ ConvolutionalLayer(input_shape[2], conv1_channels, self.filter_size, self.padding), ReLULayer(), MaxPoolingLayer(self.pool_size, self.stride), ConvolutionalLayer(conv1_channels, conv2_channels, self.filter_size, self.padding), ReLULayer(), MaxPoolingLayer(self.pool_size, self.stride), Flattener(), FullyConnectedLayer( self.max_pool2_out_size * self.max_pool2_out_size * conv2_channels, n_output_classes) ]
def __init__(self, n_input, n_output, hidden_layer_size, reg=0): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict hidden_layer_size, int - number of neurons in the hidden layer reg, float - L2 regularization strength """ self.reg = reg self.n_input = n_input self.n_output = n_output self.h_size = hidden_layer_size # TODO Create necessary layers self.RL = ReLULayer() self.FC1 = FullyConnectedLayer(n_input=self.n_input, n_output=self.h_size) self.FC2 = FullyConnectedLayer(self.h_size, self.n_output)
def __init__(self, n_input, n_output, hidden_layer_size, reg): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict hidden_layer_size, int - number of neurons in the hidden layer reg, float - L2 regularization strength """ self.reg = reg self.layers = {} self.layers['hidden'] = FullyConnectedLayer(n_input, hidden_layer_size) self.layers['hidden_ReLU'] = ReLULayer() self.layers['output'] = FullyConnectedLayer(hidden_layer_size, n_output) self.layers['output_ReLU'] = ReLULayer() self.layers_order = ['hidden', 'hidden_ReLU', 'output', 'output_ReLU']
def __init__(self, n_input, n_output, hidden_layer_size, reg): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict hidden_layer_size, int - number of neurons in the hidden layer reg, float - L2 regularization strength """ self.reg = reg # Network architecture self.layers = { 'Linear1': FullyConnectedLayer(n_input, hidden_layer_size), 'ReLU1': ReLULayer(), 'Linear2': FullyConnectedLayer(hidden_layer_size, n_output), 'ReLU2': ReLULayer() }
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): self.layers = [ConvolutionalLayer(in_channels=input_shape[2], \ out_channels=input_shape[2], \ filter_size=conv1_channels, \ padding=2), ReLULayer(), MaxPoolingLayer(pool_size=4, \ stride=2), ConvolutionalLayer(in_channels=input_shape[2], \ out_channels=input_shape[2], \ filter_size=conv2_channels, \ padding=2), ReLULayer(), MaxPoolingLayer(pool_size=4, \ stride=2), Flattener(), FullyConnectedLayer(n_input=192, \ n_output=n_output_classes)]
def __init__(self, input_shape, n_input_classes, 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 stride = 1 conv1_channels, int - number of filters in the 1st conv layer, padding=1 after first conv - shape (2, 32, 32, 2) after first max pooling - shape (2, 8, 8, 2) conv2_channels, int - number of filters in the 2nd conv layer, padding=1 after second conv - shape (2, 8, 8, 2) after second max poolling - width =2, height = 2 channels = 2 for convolutional layer in_channels = 8 """ # TODO Create necessary layers layers = [] image_width, image_height, channels = input_shape conv1_layer = ConvolutionalLayer(in_channels=channels, out_channels=conv1_channels, filter_size=3, padding=1) layers.append(conv1_layer) layers.append(ReLULayer()) layers.append(MaxPoolingLayer(4, 4)) conv2_layer = ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1) layers.append(conv2_layer) layers.append(ReLULayer()) layers.append(MaxPoolingLayer(4, 4)) layers.append(Flattener()) layers.append(FullyConnectedLayer(n_input_classes, n_output_classes)) self.layers = layers self.reg = reg
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 __init__(self, n_input, n_output, hidden_layer_size, reg): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict hidden_layer_size, int - number of neurons in the hidden layer reg, float - L2 regularization strength """ self.reg = reg # TODO Create necessary layers self.input_layer = FullyConnectedLayer(n_input, hidden_layer_size) self.relu = ReLULayer() self.output_layer = FullyConnectedLayer(hidden_layer_size, n_output) self.W_in = self.input_layer.params()['W'] self.W_out = self.output_layer.params()['W'] self.B_in = self.input_layer.params()['B'] self.B_out = self.output_layer.params()['B']
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!") image_width, image_height, image_channels = input_shape maxpool1_size = 4 maxpool2_size = 4 flattener_width = int(image_width / (maxpool1_size * maxpool2_size)) flattener_height = int(image_width / (maxpool1_size * maxpool2_size)) self.layers = [ ConvolutionalLayer(in_channels=image_channels, out_channels=conv1_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(maxpool1_size, maxpool1_size), ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(maxpool2_size, maxpool2_size), Flattener(), FullyConnectedLayer( flattener_width * flattener_height * conv2_channels, n_output_classes) ]
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!") self.layers = {"conv1": ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1), "relu1": ReLULayer(), "maxpool1": MaxPoolingLayer(4, 4), "conv2": ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1), "relu2": ReLULayer(), "maxpool2": MaxPoolingLayer(4, 4), "flatten": Flattener(), "fc": FullyConnectedLayer((input_shape[0]//16)*(input_shape[1]//16)*conv2_channels, n_output_classes)}
def __init__(self, n_input, n_output, hidden_layer_size, reg): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict hidden_layer_size, int - number of neurons in the hidden layer reg, float - L2 regularization strength """ self.reg = reg # TODO Create necessary layers # Create layers self.first_layer = FullyConnectedLayer(n_input, hidden_layer_size) self.activation = ReLULayer() self.second_layer = FullyConnectedLayer(hidden_layer_size, n_output) # Add params to the net self.first_layer_params = self.first_layer.params() self.second_layer_params = self.second_layer.params()
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.layers = [ ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1), ReLULayer(), MaxPoolingLayer(4, 4), ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1), ReLULayer(), MaxPoolingLayer(4, 4), Flattener(), FullyConnectedLayer(2 * 2 * conv2_channels, 10) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network :param input_shape: tuple of 3 ints - image_width, image_height, n_channels :param n_output_classes: int - number of classes to predict :param conv1_channels: int - number of filters in the 1st conv layer :param conv2_channels: int - number of filters in the 2nd conv layer """ self.convolution_one = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1) self.relu_one = ReLULayer() self.maxpool_one = MaxPoolingLayer(4, 4) self.convolution_two = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.relu_two = ReLULayer() self.maxpool_two = MaxPoolingLayer(4, 4) self.flattener = Flattener() height = ((input_shape[0] + 2 * 1 - 3 + 1) // 4 + 2 * 1 - 3 + 1) // 4 width = ((input_shape[1] + 2 * 1 - 3 + 1) // 4 + 2 * 1 - 3 + 1) // 4 self.fc = FullyConnectedLayer(width * height * conv2_channels, n_output_classes)
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
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 self.layer1 = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1) self.layer2 = ReLULayer() self.layer3 = MaxPoolingLayer(4, 4) self.layer4 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.layer5 = ReLULayer() self.layer6 = MaxPoolingLayer(4, 4) self.layer7 = Flattener() self.layer8 = FullyConnectedLayer( input_shape[0] * input_shape[1] * conv2_channels // (16 * 16), n_output_classes)
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 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_input_channels = input_shape kernel_size = 3 padding = 1 conv_stride = 1 pooling_stride = 4 filter_size = 4 conv1_output = (width - kernel_size + 2 * padding) / conv_stride + 1 pooling1_output = (conv1_output - filter_size) / pooling_stride + 1 conv2_output = (pooling1_output - kernel_size + 2 * padding) / conv_stride + 1 pooling2_output = (conv2_output - filter_size) / pooling_stride + 1 fc_input = int(pooling2_output * pooling2_output * conv2_channels) self.Sequential = [ ConvolutionalLayer(n_input_channels, conv1_channels, kernel_size, padding), ReLULayer(), MaxPoolingLayer(filter_size, pooling_stride), ConvolutionalLayer(conv1_channels, conv2_channels, kernel_size, padding), ReLULayer(), MaxPoolingLayer(filter_size, pooling_stride), Flattener(), FullyConnectedLayer(fc_input, n_output_classes) ]
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 __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 __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 self.layers = [] image_width, image_height, n_channels = input_shape filter_size = 3 pool_size = 4 stride = pool_size padding = 1 fc_input = (image_height // (pool_size**2)) * (image_width // (pool_size**2)) * conv2_channels self.layers.append( ConvolutionalLayer(n_channels, conv1_channels, filter_size, padding)) self.layers.append(ReLULayer()) self.layers.append(MaxPoolingLayer(pool_size, stride)) self.layers.append( ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding)) self.layers.append(ReLULayer()) self.layers.append(MaxPoolingLayer(pool_size, stride)) self.layers.append(Flattener()) self.layers.append(FullyConnectedLayer(fc_input, n_output_classes))
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 self.out_classes = n_output_classes image_width, image_height, in_channels = input_shape self.Conv1 = ConvolutionalLayer(in_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(4 * conv2_channels, n_output_classes)
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 """ self.reg = reg # TODO Create necessary layers assert input_shape[0] % 4 == 0 & input_shape[1] % 4 == 0, "Invalid input_shape value" self.layers = [ConvolutionalLayer(input_shape[2], conv1_channels, 3, 0), ReLULayer(), MaxPoolingLayer(4, 4), ConvolutionalLayer(conv1_channels, conv2_channels, 3, 0), ReLULayer(), MaxPoolingLayer(4, 4), Flattener(), FullyConnectedLayer(4 * conv2_channels, n_output_classes) ]