Created
April 12, 2020 18:52
-
-
Save morgankenyon/7156a9ed5b0fb832a8022503fb83a65b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First, install TensorFlow (https://www.tensorflow.org/) and Keras (https://keras.io/). | |
from keras.datasets import mnist | |
import numpy as np | |
(training_images, training_labels), (test_images, test_labels) = mnist.load_data() | |
#normalize image values | |
training_images = training_images / 255 | |
test_images = test_images / 255 | |
#flatten image arrays | |
training_images = training_images.reshape(len(training_images),28*28) | |
test_images = test_images.reshape(len(test_images),28*28) | |
#convert labels into arrays | |
zeroArray = [0,0,0,0,0,0,0,0,0,0] | |
def transformLabels(labels): | |
transformed = [] | |
for i in range(len(labels)): | |
label = labels[i] | |
transformed.append(zeroArray[0:label] + [1] + zeroArray[label+1:]) | |
return transformed | |
training_labels = transformLabels(training_labels) | |
test_labels = transformLabels(test_labels) | |
np.random.seed(10) | |
weights = np.random.rand(784,10) | |
alpha = 0.01 | |
def neural_network(input, weights): | |
return np.dot(input, weights) | |
#training phase | |
num_of_training_images = 40000 | |
for iteration in range(num_of_training_images): | |
image = training_images[iteration] | |
label = training_labels[iteration] | |
pred = neural_network(image, weights) | |
error = [0,0,0,0,0,0,0,0,0,0] | |
delta = [0,0,0,0,0,0,0,0,0,0] | |
for i in range(len(goal_pred)): | |
error[i] = (pred[i] - label[i]) ** 2 | |
delta[i] = pred[i] - label[i] | |
weight_deltas = np.outer(image, delta) | |
for i in range(len(weights)): | |
for j in range(len(weights[i])): | |
weights[i][j] -= alpha * weight_deltas[i][j] | |
#testing phase | |
num_correct = 0 | |
for i in range(len(test_images)): | |
image = test_images[i] | |
label = test_labels[i] | |
pred = neural_network(image, weights) | |
if (np.argmax(label) == np.argmax(pred)): | |
num_correct += 1 | |
print (str(num_correct) + "/" + str(len(test_images))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment