Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/python
  2.  
  3. threshold = 0.6
  4. learning_rate = 0.4
  5. weights = [1, 0, 1]
  6. training_set = [((1, 0, 0), 0), ((1, 0, 1), 0), ((1, 1, 0), 0), ((1, 1, 1), 1)]
  7.  
  8. def sum_function(values):
  9.     return sum(value * weight for value, weight in zip(values, weights))
  10.  
  11. while True:
  12.     print '-' * 60
  13.     error_count = 0
  14.     for input_vector, desired_output in training_set:
  15.         print weights
  16.         result = 1 if sum_function(input_vector) > threshold else 0
  17.         error = desired_output - result
  18.         if error != 0:
  19.             error_count += 1
  20.             for index, value in enumerate(input_vector):
  21.                 weights[index] += learning_rate * error * value
  22.     if error_count == 0:
  23.         break