Advertisement
1997kobra

Neural Network

Apr 11th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import math
  3. import random
  4. import numpy as np
  5.  
  6.  
  7. def layer(inputs, weights, bias):
  8.     """Calculates the values of the next layer"""
  9.     #adds one as bias to the inputs
  10.     inputs = np.vstack([inputs, [1]]) if bias else inputs
  11.     return np.tanh(np.dot(weights, inputs))
  12.  
  13.  
  14. def network(inputs, all_weights, bias):
  15.     """same as network(network(inputs, weights[0]), weights[1])..."""
  16.     outputs = inputs
  17.     for weights in all_weights:
  18.         outputs = layer(outputs, weights, bias)
  19.     return outputs
  20.  
  21.  
  22. def derivative(x):
  23.     """Derivative of the tan function"""
  24.     return 1/(math.cosh(x)**2)
  25.  
  26.  
  27. def calculate_errors(ideal_outputs, outputs, all_weights):
  28.     """calculates the error for the last layer"""
  29.     error_signals = [np.subtract(ideal_outputs, outputs)[0]]
  30.     for weights in reversed(all_weights[1:]):
  31.         error_signals.append(np.dot(error_signals[-1], weights))
  32.     return reversed(error_signals)
  33.  
  34.  
  35. def backpropagate(speed, all_weights, all_errors, inputs, bias):
  36.     new_all_weights = []
  37.     outputs = inputs
  38.     for weights, errors in zip(all_weights, all_errors):
  39.         outputs = layer(outputs, weights, bias)
  40.         new_weights = np.array([[]])
  41.         for index,error in enumerate(errors):
  42.             new_row = np.array([])
  43.             row = weights[index:index+1]
  44.             for weight in row:
  45.                 new_row = np.append(new_row,
  46.                             weight+speed*error*inputs[index]*outputs[index])
  47.             new_weights = np.append(new_weights,new_row) if np.array_equal(new_weights,np.array([[]])) else np.vstack((new_weights,new_row))
  48.         new_all_weights.append(new_weights)
  49.     return new_all_weights
  50.  
  51.  
  52. def main():
  53.     weights = [np.random.rand(2,2),np.random.rand(1,2)]
  54.     isenough = False
  55.     #while not isenough:
  56.     inputs = np.array([[np.random.randint(0,2)],
  57.                         [np.random.randint(0,2)]])
  58.     ideal_outputs = np.array([inputs[0]^inputs[1]])
  59.     outputs = network(inputs,weights,False)
  60.     print weights
  61.     errors = calculate_errors(ideal_outputs, outputs, weights)
  62.     weights = backpropagate(0.1,weights, errors, inputs, False)
  63.     isenough = abs(outputs[0]-(inputs[0]^inputs[1]))<0.1
  64.     inputs = np.array([[np.random.randint(0,2)],
  65.                         [np.random.randint(0,2)]])
  66.     ideal_outputs = np.array([inputs[0]^inputs[1]])
  67.     outputs = network(inputs,weights,False)
  68.     print weights
  69.  
  70.  
  71. if __name__ == "__main__":
  72.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement