Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. from PIL import Image
  2.  
  3. import matplotlib.pyplot as plt
  4. import NN
  5. import numpy
  6. data = open('/home/aaron/Eric workspace/Untitled Folder/mnist_train.csv','r')
  7.  
  8. data_r = data.read()
  9. raw_data_list = data_r.split()
  10. data.close()
  11.  
  12. pixel_tuple = [numpy.asfarray(x.split(',')[1:]) for x in raw_data_list]
  13.  
  14. #get MNIST data, form array actual_numbers to store the target values of each image
  15. #form array scaled_array as a 28X28 array of greyscale pixels corresponding to MNIST images.
  16. #Rescale greyscale values in scaled_array to lie between 0.1 and 1
  17.  
  18. actual_numbers = [int(x.split(',', 1)[0]) for x in raw_data_list]
  19. scaled_array = [(0.99/255)*x + 0.01 for x in pixel_tuple]
  20.  
  21. #turn actual_numbers into an array of lists of length 10 which are 0 everywhere except the index
  22.  
  23. def listify(x, onodes):
  24. #turns the number n from 0 to onodes-1 into the list of length onodes which is supported at index n
  25. l = [0]*onodes
  26. l[x] = 1
  27. l = numpy.asfarray(l)
  28. return l + 0.1
  29.  
  30. #create a neural network object:
  31. input_nodes = 784
  32. hidden_nodes = 200
  33. output_nodes = 10
  34. learning_rate = 0.08
  35. n = NN.neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)
  36. target_outputs = [listify(x, output_nodes) for x in actual_numbers]
  37.  
  38. #Train the neural network
  39. count = 0
  40. for item in scaled_array:
  41. n.train(item, target_outputs[count])
  42. count+=1
  43.  
  44. #load testing data
  45. data = open('/home/aaron/Eric workspace/Untitled Folder/mnist_test.csv','r')
  46. data_r = data.read()
  47. raw_data_list = data_r.split()
  48. data.close()
  49.  
  50. pixel_tuple = [numpy.asfarray(x.split(',')[1:]) for x in raw_data_list]
  51.  
  52. #get MNIST data, form array actual_numbers to store the target values of each image
  53. #form array scaled_array as a 1x784 of greyscale pixels corresponding to MNIST images.
  54. #Rescale greyscale values in scaled_array to lie between 0.1 and 1
  55.  
  56. actual_numbers = [int(x.split(',', 1)[0]) for x in raw_data_list]
  57. scaled_array = [(0.99/255)*x + 0.01 for x in pixel_tuple]
  58.  
  59. i = 0
  60. while i < 10:
  61. op = n.query(scaled_array[i])
  62. op_list = [list(x)[0] for x in op]
  63. guess = op_list.index(max(op_list))
  64. print("Guess:", guess, "Actual:", actual_numbers[i])
  65. i +=1
  66.  
  67. scorecard = []
  68. count = 0
  69. for item in scaled_array:
  70. op = n.query(item)
  71. op_list = [list(x)[0] for x in op]
  72. guess = op_list.index(max(op_list))
  73. scorecard.append(guess == actual_numbers[count])
  74. count +=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement