print('Filepath: {}'.format(filepath)) print() f = open(filepath, 'wa') num_layers = 5 num_hidden_nodes = 784 num_training_iterations = 1000 # Get the cifar10 dataset print("Getting data...") (X_train, y_train), (X_test, y_test) = data.get_mnist() # Compile network print("Compiling model...", end='') nn = NeuralNetwork(X_train.shape[1]) for x in range(num_layers): nn.add_layer(Dense(num_hidden_nodes)) get_layer_for_setting(nn, setting) nn.add_layer(Dense(10)) nn.add_layer(Activation('softmax')) nn.compile(loss_fn='categorical_crossentropy', init_fn='lecun', pred_fn='argmax', learning_rate=learning_rate, use_normal=True) print('finished!') ap_loss = [] for i in range(1, num_training_iterations+1): start = time() nn.train(X_train, y_train) time_elapsed = time() - start s = "{},{},{}".format(i, nn.get_accuracy(X_train, y_train), time_elapsed)
[6.7, 3.3, 5.7, 2.5], [6.7, 3.0, 5.2, 2.3], [6.3, 2.5, 5.0, 1.9], [6.5, 3.0, 5.2, 2.0], [6.2, 3.4, 5.4, 2.3], [5.9, 3.0, 5.1, 1.8]]) y = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) y = data.one_hot_encode(y) ap_losses = [] relu_losses = [] for x in range(trials): nn = NeuralNetwork(4) for x in range(num_layers): nn.add_layer(Dense(num_hidden_nodes)) nn.add_layer(ActivationPool(act_fns)) nn.add_layer(Dense(3)) nn.add_layer(Activation('softmax')) nn.compile(loss_fn='categorical_crossentropy', pred_fn='argmax', learning_rate=learning_rate) ap_losses.append(nn.get_loss(X, y)) for x in range(trials): nn = NeuralNetwork(4) for x in range(num_layers): nn.add_layer(Dense(num_hidden_nodes)) nn.add_layer(Activation('relu')) nn.add_layer(Dense(3)) nn.add_layer(Activation('softmax'))
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) y = data.one_hot_encode(y) x_plot_before = [] y_plot_before = [] z_plot_before = [] x_plot_after = [] y_plot_after = [] z_plot_after = [] nn = NeuralNetwork(4) for x in range(num_layers): nn.add_layer(Dense(num_hidden_nodes)) #nn.add_layer(Activation('relu')) nn.add_layer(MReLU(coefs=[P_val, D_val], trainable=trainable_mrelu)) nn.add_layer(Dense(3)) nn.add_layer(Activation('softmax')) nn.compile(loss_fn='categorical_crossentropy', pred_fn='argmax', learning_rate=learning_rate) weights = nn.layers[2].W.get_value() for I in np.arange(0, 1, 0.05): for J in np.arange(0, 1, 0.05): weights[0][0] = I weights[0][1] = J nn.layers[2].W.set_value(weights)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) y = data.one_hot_encode(y) x_plot_before = [] y_plot_before = [] z_plot_before = [] x_plot_after = [] y_plot_after = [] z_plot_after = [] nn = NeuralNetwork(4) for x in range(num_layers): nn.add_layer(Dense(num_hidden_nodes)) #nn.add_layer(Activation('relu')) nn.add_layer(ActivationPool([theano.tensor.nnet.relu, theano.tensor.nnet.softplus, theano.tensor.nnet.sigmoid])) nn.add_layer(Dense(3)) nn.add_layer(Activation('softmax')) nn.compile(loss_fn='categorical_crossentropy', pred_fn='argmax', learning_rate=learning_rate) weights = nn.layers[2].W.get_value() print("Final loss: {}".format(nn.get_loss(X, y))) for I in np.arange(0, 1, 0.05): for J in np.arange(0, 1, 0.05): weights[0][0] = I weights[0][1] = J nn.layers[2].W.set_value(weights)
def test_construct_nn(): nn = NeuralNetwork(2) nn.add_layer(Dense(40)) nn.add_layer(Activation('relu')) nn.add_layer(Dense(2)) nn.add_layer(Activation('softmax')) assert len(nn.layers) == 4 assert not hasattr(nn, 'train_fn') nn.compile(categorical_crossentropy, argmax) assert hasattr(nn, 'train_fn')