Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import matplotlib.pyplot as plt
- import NN
- import numpy
- data = open('/home/aaron/Eric workspace/Untitled Folder/mnist_train.csv','r')
- data_r = data.read()
- raw_data_list = data_r.split()
- data.close()
- pixel_tuple = [numpy.asfarray(x.split(',')[1:]) for x in raw_data_list]
- #get MNIST data, form array actual_numbers to store the target values of each image
- #form array scaled_array as a 28X28 array of greyscale pixels corresponding to MNIST images.
- #Rescale greyscale values in scaled_array to lie between 0.1 and 1
- actual_numbers = [int(x.split(',', 1)[0]) for x in raw_data_list]
- scaled_array = [(0.99/255)*x + 0.01 for x in pixel_tuple]
- #turn actual_numbers into an array of lists of length 10 which are 0 everywhere except the index
- def listify(x, onodes):
- #turns the number n from 0 to onodes-1 into the list of length onodes which is supported at index n
- l = [0]*onodes
- l[x] = 1
- l = numpy.asfarray(l)
- return l + 0.1
- #create a neural network object:
- input_nodes = 784
- hidden_nodes = 200
- output_nodes = 10
- learning_rate = 0.08
- n = NN.neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)
- target_outputs = [listify(x, output_nodes) for x in actual_numbers]
- #Train the neural network
- count = 0
- for item in scaled_array:
- n.train(item, target_outputs[count])
- count+=1
- #load testing data
- data = open('/home/aaron/Eric workspace/Untitled Folder/mnist_test.csv','r')
- data_r = data.read()
- raw_data_list = data_r.split()
- data.close()
- pixel_tuple = [numpy.asfarray(x.split(',')[1:]) for x in raw_data_list]
- #get MNIST data, form array actual_numbers to store the target values of each image
- #form array scaled_array as a 1x784 of greyscale pixels corresponding to MNIST images.
- #Rescale greyscale values in scaled_array to lie between 0.1 and 1
- actual_numbers = [int(x.split(',', 1)[0]) for x in raw_data_list]
- scaled_array = [(0.99/255)*x + 0.01 for x in pixel_tuple]
- i = 0
- while i < 10:
- op = n.query(scaled_array[i])
- op_list = [list(x)[0] for x in op]
- guess = op_list.index(max(op_list))
- print("Guess:", guess, "Actual:", actual_numbers[i])
- i +=1
- scorecard = []
- count = 0
- for item in scaled_array:
- op = n.query(item)
- op_list = [list(x)[0] for x in op]
- guess = op_list.index(max(op_list))
- scorecard.append(guess == actual_numbers[count])
- count +=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement