#!/bin/python3 import Layers as lys import numpy as np x_1 = np.array([1, 4]) x_2 = np.array([2, 5]) lin_1 = lys.Linear(2, 5, 0.001) relu = lys.Relu() h1 = relu.forward(lin_1.forward(x_1)) lin_2 = lys.Linear(h1.shape[0], 2, 0.001) sm_cel = lys.SM_CEL() y = np.array([1, 0]) h2 = sm_cel.forward_SM(lin_2.forward(h1)) print(h2) # FORWARD TESTING SUCCESSFUL # Start Backward prop testing g_1 = sm_cel.backward(h2, y) g_2 = lin_2.backward(g_1, h1) g_3 = relu.backward(h1) g_4 = lin_1.backward(g_3, x_1)
def run(): # Fetch data f1 = file('../data/mldata/mnist_data.pkl','rb') mnist = pickle.load(f1) f1.close() split = 60000 X_train = np.reshape(mnist.data[:split], (-1,1,28,28))/255.0 Y_train = mnist.target[:split] X_test = np.reshape(mnist.data[split:], (-1,1,28,28))/255.0 Y_test = mnist.target[split:] n_classes = np.unique(Y_train).size # Downsample training data n_train_samples = 3000 train_idxs = np.random.random_integers(0, split-1, n_train_samples) X_train = X_train[train_idxs, ...] Y_train = Y_train[train_idxs, ...] Y_train_one_hot = one_hot(Y_train) print ('number of train samples: %d')%(n_train_samples) print ('number of test samples: %d')%(X_test.shape[0]) # setup network nn = NeuralNetwork( layers = [ Layers.Convolution( n_feats=12, filter_shape=(5,5), strides=(1,1), weight_scale=0.1, weight_decay=0.001), Layers.Activation('relu'), Layers.Pool( pool_shape=(2,2), strides=(2,2), mode='max'), Layers.Convolution( n_feats=16, filter_shape=(5,5), strides=(1,1), weight_scale=0.1, weight_decay=0.001), Layers.Activation('relu'), Layers.Flatten(), Layers.Linear( n_out=n_classes, weight_scale=0.1, weight_decay=0.02), Layers.Softmax() ] ) #check gradient # nn.check_gradients(X_train[:10], Y_train_one_hot[:10]) # Train neural network t0 = time.time() nn.train(X_train, Y_train_one_hot, learning_rate=0.05, max_iter=3, batch_size=32) t1 = time.time() print('Duration: %.1fs' % (t1-t0)) # Evaluate on test data # Y_test_one_hot = one_hot(Y_test) error = nn.error(X_test, Y_test) print('Test error rate: %.4f' % error)
images, labels = mndata.load_training() images_array = np.array(images) labels_array = np.array(labels).reshape((-1, 1)) from sklearn.preprocessing import OneHotEncoder ohe = OneHotEncoder(categories='auto', sparse=False) ohe.fit(labels_array) labels_array = ohe.transform(labels_array) x_1 = images_array[100] / 255 y = labels_array[100] #print(sample_image, sample_label) # Layers lin_1 = lys.Linear(784, 300, 10) relu = lys.Relu() lin_2 = lys.Linear(300, 10, 10) sigmoid = lys.Sigmoid() mse = lys.MSE() # n = 1000 # for i in range(n): # # Forward pass # h1 = lin_1.forward(x_1) # act_h1 = relu.forward(h1) # h2 = lin_2.forward(act_h1) # yhat = sigmoid.forward(h2) # loss = mse.forward(yhat, y) # # Backward pass