Felanpro

NeuralNetworkFrameworkBetaVersion4

Dec 9th, 2022 (edited)
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | Source Code | 0 0
  1. '''
  2. This neural network framework nudges both weights and biases when it performs backpropagation. It can handle multiple outputs.
  3. '''
  4.  
  5. import numpy as np
  6.  
  7. class Layer:
  8.     def __init__(self, inputNodes, outputNodes):
  9.         self.weights = 0.1 * np.random.randn(inputNodes, outputNodes)
  10.         self.biases = 1 + np.zeros((1, outputNodes))
  11.    
  12.     def forward(self, inputs):
  13.         self.output = np.dot(inputs, self.weights) + self.biases
  14. class Activation_ReLU:
  15.     def forward(self, inputs):
  16.         self.output = np.maximum(0, inputs)    
  17.        
  18. learningRate = 0.000001
  19. def backwards(network, input_, desired):
  20.     currentLayer = len(network) - 1
  21.  
  22.    
  23.     dError = 2*(network[currentLayer].output[0] - desired)
  24.    
  25.     gradients = np.zeros((len(network), 5)) #The digit here represent maximum number of neurons per layer
  26.  
  27.     for neuronsPerLastLayer in range(len(network[currentLayer].output[0])):
  28.         gradients[currentLayer][neuronsPerLastLayer] = dError[neuronsPerLastLayer]    
  29.                      
  30.     currentLayer = len(network) - 1
  31.     while currentLayer > 0: # Per layer
  32.         if type(network[currentLayer - 1]) == Activation_ReLU:
  33.             pass
  34.         else:
  35.                 #Nudge the weights and biases
  36.                 for neuronCurrentLayer in range(len(network[currentLayer].output[0])): # Per neuron in current layer
  37.                     network[currentLayer].biases[0][neuronCurrentLayer] -= 1 * gradients[currentLayer][neuronCurrentLayer] * learningRate
  38.                     for neuronPreviousLayer in range(len(network[currentLayer - 1].output[0])): # Per neuron in previous layer/per weight per neuron in current layer
  39.                         network[currentLayer].weights[neuronPreviousLayer][neuronCurrentLayer] -= network[currentLayer - 1].output[0][neuronPreviousLayer] * gradients[currentLayer][neuronCurrentLayer] * learningRate    
  40.                
  41.                
  42.                 # Calculate gradients for every neuron in the next layer you're going to adjust
  43.                 for neuronCurrentLayer in range(len(network[currentLayer].output[0])): # Per neuron in current layer
  44.                     for neuronPreviousLayer in range(len(network[currentLayer - 1].output[0])): # Per neuron in previous layer
  45.                         gradients[currentLayer - 1][neuronPreviousLayer] += network[currentLayer].weights[neuronPreviousLayer][neuronCurrentLayer] * gradients[currentLayer][neuronCurrentLayer]  
  46.        
  47.         currentLayer -= 1 #Go to previous layer
  48.     print("Error: ", (network[len(network) - 1].output[0] - desired))
  49.        
  50. #Create training data
  51. inputs = [3, 6, 2, 8, 12, 90, 45, 23, 88, 18]
  52. desired = np.array([[6, 6], [12, 12], [4, 4], [16, 16], [24, 24], [180, 180], [90, 90], [46, 46], [176, 176], [36, 36]])
  53.  
  54. #Create neural network
  55. layer1 = Layer(1, 5)
  56. '''
  57. Layer1 weights aren't going to be affected by the backwards function, so juts set the weights to 1 if you want like this:
  58. layer1.weights *= 1/layer1.weights
  59. '''
  60.  
  61. layer2 = Layer(5, 3)
  62.  
  63. layer3 = Layer(3, 4)
  64.  
  65. layer4 = Layer(4, 2)
  66.  
  67. #Train the network
  68. for iteration in range(6000):
  69.     for x in range(len(inputs)):
  70.         layer1.forward(inputs[x])
  71.         layer2.forward(layer1.output)
  72.         layer3.forward(layer2.output)
  73.         layer4.forward(layer3.output)
  74.         backwards([layer1, layer2, layer3, layer4], inputs[x], desired[x])
  75.        
  76. #Test the network
  77. userInput = 24
  78. layer1.forward(userInput)
  79. layer2.forward(layer1.output)
  80. layer3.forward(layer2.output)
  81. layer4.forward(layer3.output)
  82.  
  83. print("Guess: ", layer4.output)
Advertisement
Add Comment
Please, Sign In to add comment