Skip to content

Instantly share code, notes, and snippets.

@morgankenyon
Created April 12, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morgankenyon/7156a9ed5b0fb832a8022503fb83a65b to your computer and use it in GitHub Desktop.
Save morgankenyon/7156a9ed5b0fb832a8022503fb83a65b to your computer and use it in GitHub Desktop.
# 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