Advertisement
OreganoHauch

neuralNetwork

Dec 31st, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import numpy
  2. # scipy.special for the sigmoid function expit()
  3. import scipy.special
  4. # neural network class definition
  5. class neuralNetwork:
  6.  
  7. # initialize the neural network
  8. def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
  9. # set number of nodes in each input, hidden, output layer
  10. self.inodes = inputnodes
  11. self.hnodes = hiddennodes
  12. self.onodes = outputnodes
  13.  
  14. # link weight matrices, wih and who
  15. # weights inside the arrays are w_i_j, where link is from node i to node j in the next layer
  16. # w11 w21
  17. # w12 w22
  18. # etc.
  19. self.wih = (2 * numpy.random.rand(self.hnodes, self.inodes) - 1)
  20. self.who = (2 * numpy.random.rand(self.onodes, self.hnodes) - 1)
  21.  
  22. # learning rate
  23. self.lr = learningrate
  24. pass
  25.  
  26. # activation function is the sigmoid function
  27. self.activation_function = lambda x: scipy.special.expit(x)
  28. pass
  29.  
  30. #train the neural network
  31. def train():
  32. pass
  33.  
  34. #query the neural network
  35. def query(self, inputs_list):
  36. # convert inputs list to 2d array
  37. inputs = numpy.array(inputs_list, ndmin=2).T
  38.  
  39. # calculate signals into hidden layer
  40. hidden_inputs = numpy.dot(self.wih, inputs)
  41. # calculate the signals emerging from hidden layer
  42. hidden_outputs = self.activation_function(hidden_inputs)
  43.  
  44. # calculate signals into final output layer
  45. final_inputs = numpy.dot(self.who, hidden_outputs)
  46. #calculate the signals emerging from final output layer
  47. final_outputs = self.activation_function(final_inputs)
  48.  
  49. return final_outputs
  50.  
  51.  
  52. # number of input, hidden and output nodes
  53. input_nodes = 3
  54. hidden_nodes = 3
  55. output_nodes = 3
  56.  
  57. # learning rate is 0.3
  58. learning_rate = 0.3
  59.  
  60. # create instance of neural network
  61. n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement