def test_tanh_1_forward(self): print('\n==================================') print(' Test tanh forward ') print('==================================') x = np.linspace(-1.5, 1.5, num=30).reshape(6, 5) tanh = Tanh() out = tanh.forward(x) correct_out = [ [-0.90514825, -0.88460402, -0.85993717, -0.83047189, -0.79548953], [-0.75425761, -0.70607365, -0.65032522, -0.58656543, -0.51459914], [-0.43457022, -0.34703513, -0.25300497, -0.15393885, -0.05167806], [0.05167806, 0.15393885, 0.25300497, 0.34703513, 0.43457022], [0.51459914, 0.58656543, 0.65032522, 0.70607365, 0.75425761], [0.79548953, 0.83047189, 0.85993717, 0.88460402, 0.90514825] ] e = rel_error(correct_out, out) print('Relative difference:', e) self.assertTrue(e <= 5e-7)
def test_model(num_feat, num_classes): classifier = ClassifierModel() classifier.add_layer('FC-1', FCLayer(num_feat, 2)) classifier.add_layer('Sigmoid', Sigmoid()) classifier.add_layer('FC-2', FCLayer(2, 5)) classifier.add_layer('ReLU', ReLU()) classifier.add_layer('FC-3', FCLayer(5, 3)) classifier.add_layer('tanh', Tanh()) classifier.add_layer('FC-4', FCLayer(3, num_classes)) classifier.add_layer('Softmax', SoftmaxLayer()) return classifier
def test_tanh_2_backward(self): print('\n==================================') print(' Test tanh backward ') print('==================================') np.random.seed(123) x = np.random.randn(5, 5) tanh = Tanh() out = tanh.forward(x) dx = tanh.backward(x, 0.0) correct_dx = [ [-0.39900528, 0.42055529, 0.26147548, -0.26911137, -0.42115391], [0.22601191, -0.074565, -0.35876431, 0.34549378, -0.44238431], [-0.44192875, -0.0938645, 0.27373408, -0.43556065, -0.36680073], [-0.36170986, 0.10450339, 0.10753923, 0.41907822, 0.33386819], [0.44697633, 0.27393871, -0.43260586, 0.37333295, -0.349298] ] e = rel_error(correct_dx, dx) print('Relative difference:', e) self.assertTrue(e <= 5e-8)
def test_model(num_feat, num_classes): classifier = CNN_Classifier() classifier.add_layer( 'Conv-1', ConvolutionLayer(num_feat, 2, kernel_size=3, stride=1, pad=1)) classifier.add_layer('ReLU', ReLU()) classifier.add_layer( 'Conv-2', ConvolutionLayer(2, 3, kernel_size=3, stride=1, pad=1)) classifier.add_layer('tanh', Tanh()) classifier.add_layer( 'Conv-3', ConvolutionLayer(3, 3, kernel_size=3, stride=1, pad=0)) classifier.add_layer('Sigmoid', Sigmoid()) classifier.add_layer('Max-pool - 1', MaxPoolingLayer(kernel_size=2, stride=1)) classifier.add_layer('FC-4', FCLayer(12, num_classes)) classifier.add_layer('Softmax', SoftmaxLayer()) return classifier
""" # mnist / fashion_mnist dataset = 'mnist' # Hyper-parameters num_epochs = 100 learning_rate = 0.001 reg_lambda = 1e-8 print_every = 10 batch_size = 128 # Add layers model.add_layer('FC-1', FCLayer(784, 500)) model.add_layer('sigmoid-1', Tanh()) model.add_layer('FC-2', FCLayer(500, 500)) model.add_layer('sigmoid-2', Tanh()) model.add_layer('FC-3',FCLayer(500,10)) model.add_layer('Softmax Layer', SoftmaxLayer()) # ========================================================================= assert dataset in ['mnist', 'fashion_mnist'] # Dataset if dataset == 'mnist': x_train, y_train, x_test, y_test = load_mnist('./data') else: x_train, y_train, x_test, y_test = load_fashion_mnist('./data') x_train, x_test = np.squeeze(x_train), np.squeeze(x_test)
# Hyper-parameters num_epochs = 80 learning_rate = 0.1 reg_lambda = 1e-3 print_every = 10 batch_size = 128 # Add layers - example 1 #model.add_layer('FC-1', FCLayer(784, 10)) #model.add_layer('Sigmoid', Sigmoid()) #model.add_layer('Softmax Layer', SoftmaxLayer()) # Add layers - example 2 model.add_layer('FC-1', FCLayer(784, 200)) model.add_layer('Tanh1', Tanh()) model.add_layer('FC-2', FCLayer(200, 200)) model.add_layer('Tanh2', Tanh()) model.add_layer('FC-3', FCLayer(200, 10)) model.add_layer('Softmax Layer', SoftmaxLayer()) # ========================================================================= assert dataset in ['mnist', 'fashion_mnist'] # Dataset if dataset == 'mnist': x_train, y_train, x_test, y_test = load_mnist('./data') else: x_train, y_train, x_test, y_test = load_fashion_mnist('./data') x_train, x_test = np.squeeze(x_train), np.squeeze(x_test)