images, labels = (x_train[0:1000].reshape(1000, 28 * 28) /
255, y_train[0:1000])
one_hot_labels = np.zeros((len(labels), 10))
for i, l in enumerate(labels):
one_hot_labels[i][l] = 1
labels = one_hot_labels
test_images = x_test.reshape(len(x_test), 28 * 28) / 255
test_labels = np.zeros((len(y_test), 10))
for i, l in enumerate(y_test):
test_labels[i][l] = 1
import sys import numpy as np from keras.datasets import mnist
np.random.seed(1)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
images, labels = (x_train[0:1000].reshape(1000, 28 * 28) / 255, y_train[0:1000]) one_hot_labels = np.zeros((len(labels), 10)) for i, l in enumerate(labels): one_hot_labels[i][l] = 1 labels = one_hot_labels
test_images = x_test.reshape(len(x_test), 28 * 28) / 255 test_labels = np.zeros((len(y_test), 10)) for i, l in enumerate(y_test): test_labels[i][l] = 1
def tanh(x): return np.tanh(x) def tahn2deriv(output): return 1 - (output ** 2) def softmax(x): temp = np.exp(x) return temp / np.sum(temp, axis = 1, keepdims = True)
alpha, iterations, hidden_size = (2, 300, 100) pixels_per_image, num_labels = (784, 10) batch_size = 100
weights_0_1 = 0.02 np.random.random((pixels_per_image, hidden_size)) - 0.01 weights_1_2 = 0.2 np.random.random((hidden_size, num_labels)) - 0.1
for j in range(iterations): correct_cnt = 0 for i in range(int(len(images) / batch_size)): batch_start, batch_end = ((i batch_size), ((i+1) batch_size)) layer_0 = images[batch_start : batch_end] layer_1 = tanh(np.dot(layer_0, weights_0_1)) dropout_mask = np.random.randint(2, size=layer_1.shape) layer_1 = dropout_mask 2 layer_2 = softmax(np.dot(layer_1, weights_1_2))
if(j % 10 == 0): sys.stdout.write("\n" + "I:" + str(j) + \ " Test accuracy:" + str(test_correct_cnt / float(len(test_images))) + \ " Train accuracy:" + str(correct_cnt / float(len(images))))