fork download
  1. import numpy as np
  2.  
  3. # Parameters
  4. learning_rate = 0.1
  5. max_iterations = 1000
  6. lower_threshold = 0.8 # Lower threshold for activation
  7. upper_threshold = 1.2 # Upper threshold for activation
  8.  
  9. # Threshold activation function with two thresholds
  10. def activation_function(weighted_sum):
  11. if weighted_sum >= upper_threshold:
  12. return 1
  13. else:
  14. return 0
  15.  
  16. # Generate all 16 possible truth tables for 2 inputs
  17. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  18. all_targets = [
  19. [0, 0, 0, 0], # 0
  20. [0, 0, 0, 1], # 1
  21. [0, 0, 1, 0], # 2
  22. [0, 0, 1, 1], # 3
  23. [0, 1, 0, 0], # 4
  24. [0, 1, 0, 1], # 5
  25. [0, 1, 1, 0], # 6
  26. [0, 1, 1, 1], # 7
  27. [1, 0, 0, 0], # 8
  28. [1, 0, 0, 1], # 9
  29. [1, 0, 1, 0], # 10
  30. [1, 0, 1, 1], # 11
  31. [1, 1, 0, 0], # 12
  32. [1, 1, 0, 1], # 13
  33. [1, 1, 1, 0], # 14
  34. [1, 1, 1, 1], # 15
  35. ]
  36.  
  37. # Initialize weights for the neurons in a smaller range
  38. weights_input_to_hidden = np.random.rand(3) * 0.1 # 2 inputs + 1 bias → 1 hidden neuron
  39. weights_hidden_to_output = np.random.rand(2) * 0.1 # 1 hidden neuron + 1 bias → 1 output neuron
  40.  
  41. # Training loop
  42. network_trained = False
  43. for target in all_targets: # Train for all possible tables
  44. epoch = 0
  45. while epoch < max_iterations:
  46. epoch += 1
  47. all_correct = True # Flag to track if all outputs are correct
  48.  
  49. for input_vector, expected_target in zip(inputs, target):
  50. N1, N2 = input_vector
  51. bias = 1 # Bias input
  52.  
  53. # Forward pass (hidden layer)
  54. N3_input = np.dot(np.append(input_vector, bias), weights_input_to_hidden) # Hidden layer sum
  55. N3 = activation_function(N3_input) # Output of the hidden neuron
  56.  
  57. # Output layer
  58. N4_input = (N3 * weights_hidden_to_output[0] + bias * weights_hidden_to_output[1]) # Weighted sum to output
  59. N4 = activation_function(N4_input) # Output neuron decision
  60.  
  61. # Error calculation
  62. error = expected_target - N4
  63.  
  64. # If there is an error, update the weights
  65.  
  66.  
Success #stdin #stdout 1.18s 28732KB
stdin
Standard input is empty
stdout
Standard output is empty