Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #import stuff
  2.  
  3. # package for math operations
  4. import numpy
  5. # package for sigmoid function
  6. import scipy.special
  7. # package for graphics
  8. import matplotlib.pyplot
  9. get_ipython().magic('matplotlib inline')
  10.  
  11.  
  12. # create the network
  13.  
  14.  
  15. class neuralNetwork:
  16. def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
  17. # create nodes
  18. self.inodes = inputnodes
  19. self.hnodes = hiddennodes
  20. self.onodes = outputnodes
  21. # use learningrate
  22. self.lr = learningrate
  23. # link weights
  24. self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
  25. self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))
  26. # create sigmoid function
  27. self.activation_function = lambda x: scipy.special.expit(x)
  28.  
  29. pass
  30.  
  31. def train(self, inputs_list, targets_list):
  32. # convert inputs into array
  33. inputs = numpy.array(inputs_list, ndmin=2).T
  34. targets = numpy.array(targets_list, ndmin=2).T
  35. # calculate inputs and outputs of the hidden layer
  36. hidden_inputs = numpy.dot(self.wih, inputs)
  37. hidden_outputs = self.activation_function(hidden_inputs)
  38. # calculate inputs and outputs of output layer
  39. final_inputs = numpy.dot(self.who, hidden_outputs)
  40. final_outputs = self.activation_function(final_inputs)
  41. # calculate the error
  42. output_errors = targets - final_outputs
  43. hidden_errors = numpy.dot(self.who.T, output_errors)
  44. # adjust weights depending on error
  45. self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))
  46. self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))
  47.  
  48. pass
  49.  
  50. def query(self, inputs_list):
  51. # convert inputs into array
  52. inputs = numpy.array(inputs_list, ndmin=2).T
  53. # calculate inputs and outputs of hidden layer
  54. hidden_inputs = numpy.dot(self.wih, inputs)
  55. hidden_outputs = self.activation_function(hidden_inputs)
  56. # calculate inputs and outputs of output layer
  57. final_inputs = numpy.dot(self.who, hidden_outputs)
  58. final_outputs = self.activation_function(final_inputs)
  59.  
  60. return final_outputs
  61.  
  62. pass
  63.  
  64.  
  65. # train network
  66.  
  67. # set number of nodes and inputs
  68. input_nodes = 784
  69. hidden_inputs = 100
  70. output_nodes = 10
  71.  
  72. # set learningrate
  73.  
  74. learning_rate = 0.2
  75.  
  76. # create instance of neural network
  77. n = neuralNetwork(input_nodes, hidden_inputs, output_nodes, learning_rate)
  78.  
  79. # load training data
  80. training_data_file = open ("mnist_train.csv", 'r')
  81. training_data_list = training_data_file.readlines()
  82. training_data_file.close()
  83.  
  84. # split data records and train
  85. epochs = 2
  86. for e in range(epochs):
  87. for record in training_data_list:
  88. all_values = record.split(',')
  89. inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
  90. targets = numpy.zeros(output_nodes) + 0.01
  91. targets[int(all_values[0])] = 0.99
  92. n.train(inputs, targets)
  93. pass
  94. pass
  95.  
  96.  
  97.  
  98. # test neural network
  99.  
  100. # how well does the network perform?
  101. scorecard = []
  102. # open test data
  103. test_data_file = open ("mnist_test.csv", 'r')
  104. test_data_list = test_data_file.readlines()
  105. test_data_file.close()
  106. # iterate through the whole test set
  107. for record in test_data_list:
  108. # prepare data
  109. all_values = record.split(',')
  110. # print the correct answer
  111. correct_label = int(all_values[0])
  112. # print(correct_label, "correct label")
  113. inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
  114. # calculate outputs
  115. outputs = n.query(inputs)
  116. # translate network's answer
  117. label = numpy.argmax(outputs)
  118. # print(label, "networks answer")
  119. # fill scorecard
  120. if (label == correct_label):
  121. scorecard.append(1)
  122. else:
  123. scorecard.append(0)
  124. pass
  125.  
  126. pass
  127.  
  128.  
  129.  
  130.  
  131.  
  132. # calculate performance of network
  133. scorecard_array = numpy.asarray(scorecard)
  134. print ("Performance = ", scorecard_array.sum() / scorecard_array.size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement