Advertisement
obernardovieira

Training perceptron for OR gate

Mar 20th, 2017
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. # input[0] works has bias
  2.  
  3. input = [
  4.     [1, 0, 0],
  5.     [1, 0, 1],
  6.     [1, 1, 0],
  7.     [1, 1, 1]
  8. ]
  9.  
  10. output = [
  11.     0,
  12.     1,
  13.     1,
  14.     1
  15. ]
  16.  
  17. weights = [
  18.     0.0,
  19.     0.0,
  20.     0.0
  21. ]
  22.  
  23. learning_tax = 0.5
  24.  
  25. remaining_error = 0.0
  26.  
  27.  
  28. def calculate_output(c_input, desired_output):
  29.     obtained_output = weights[0] * c_input[0] + \
  30.         weights[1] * c_input[1] + \
  31.         weights[2] * c_input[2]
  32.  
  33.     output_error = desired_output - obtained_output
  34.  
  35.     if output_error != 0.0:
  36.         for s in range(0, 3):
  37.             weights[s] += learning_tax * output_error * c_input[s]
  38.  
  39.     return output_error
  40.  
  41. for b in range(0, 100):
  42.     for c in range(0, 4):
  43.         remaining_error = calculate_output(input[c], output[c])
  44.  
  45.     if b % 10 == 0:
  46.         print('\nWeights: {} - {} - {}\nRemainingError: {}'
  47.               .format(weights[0], weights[1], weights[2], remaining_error))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement