fork(1) download
  1. import numpy as np
  2.  
  3. # Initialization of weights
  4. def initialize_weights(input_size, output_size):
  5. return np.random.randn(input_size, output_size) * 0.1 # Smaller weight initialization
  6.  
  7. # Simple activation function
  8. def binary_output(x):
  9. return np.where(x >= 0.5, 1, 0) # Threshold at 0.5
  10.  
  11. # Training loop
  12. def train_neuron(inputs, targets, epochs, learning_rate):
  13. input_size = len(inputs[0]) # Number of features
  14. output_size = len(targets[0]) # Number of outputs
  15. weights = initialize_weights(input_size, output_size)
  16.  
  17. # Training loop
  18. for epoch in range(epochs):
  19. total_error = 0
  20. for i in range(len(inputs)):
  21. input_data = inputs[i]
  22. target = targets[i]
  23.  
  24. # Forward pass: Calculate the weighted sum (without activation)
  25. output = np.dot(input_data, weights)
  26.  
  27. # Apply the binary output threshold
  28. predicted = binary_output(output)
  29.  
  30. # Calculate error
  31. error = target - predicted
  32. total_error += np.sum(np.abs(error)) # Sum of absolute error
  33.  
  34. # Backpropagation: Update weights based on error (Gradient Descent)
  35. gradients = error[:, np.newaxis] * input_data # Gradient for each weight
  36. weights += learning_rate * gradients # Update weights
  37.  
  38. # Optionally, clip gradients to prevent explosion
  39. weights = np.clip(weights, -1, 1)
  40.  
  41. # Decaying learning rate for each epoch (optional)
  42. learning_rate *= (1 / (1 + 0.01 * epoch)) # Decaying over epochs
  43.  
  44. if epoch % 100 == 0:
  45. print(f"Epoch {epoch}, Error: {total_error / len(inputs)}")
  46.  
  47. return weights
  48.  
  49. # Input data (binary)
  50. inputs = np.array([
  51. [0, 0, 0, 0],
  52. [0, 0, 0, 1],
  53. [0, 0, 1, 0],
  54. [0, 0, 1, 1],
  55. [0, 1, 0, 0],
  56. [0, 1, 0, 1],
  57. [0, 1, 1, 0],
  58. [0, 1, 1, 1],
  59. [1, 0, 0, 0],
  60. [1, 0, 0, 1],
  61. [1, 0, 1, 0],
  62. [1, 0, 1, 1],
  63. [1, 1, 0, 0],
  64. [1, 1, 0, 1],
  65. [1, 1, 1, 0],
  66. [1, 1, 1, 1]
  67. ])
  68.  
  69. # Target data (binary XOR-like)
  70. targets = np.array([
  71. [0, 0, 0, 0],
  72. [0, 0, 0, 1],
  73. [0, 0, 1, 0],
  74. [0, 0, 1, 1],
  75. [0, 1, 0, 0],
  76. [0, 1, 0, 1],
  77. [0, 1, 1, 0],
  78. [0, 1, 1, 1],
  79. [1, 0, 0, 0],
  80. [1, 0, 0, 1],
  81. [1, 0, 1, 0],
  82. [1, 0, 1, 1],
  83. [1, 1, 0, 0],
  84. [1, 1, 0, 1],
  85. [1, 1, 1, 0],
  86. [1, 1, 1, 1]
  87. ])
  88.  
  89. # Train the model
  90. weights = train_neuron(inputs, targets, epochs=2000, learning_rate=0.1)
  91.  
  92. # After training, print final weights and check predictions
  93. print("Final weights:", weights)
  94. for i in range(len(inputs)):
  95. prediction = binary_output(np.dot(inputs[i], weights))
  96. print(f"Input: {inputs[i]}, Prediction: {prediction}")
  97.  
Success #stdin #stdout 3.11s 29148KB
stdin
Standard input is empty
stdout
Epoch 0, Error: 1.25
Epoch 100, Error: 1.375
Epoch 200, Error: 1.375
Epoch 300, Error: 1.375
Epoch 400, Error: 1.375
Epoch 500, Error: 1.375
Epoch 600, Error: 1.375
Epoch 700, Error: 1.375
Epoch 800, Error: 1.375
Epoch 900, Error: 1.375
Epoch 1000, Error: 1.375
Epoch 1100, Error: 1.375
Epoch 1200, Error: 1.375
Epoch 1300, Error: 1.375
Epoch 1400, Error: 1.375
Epoch 1500, Error: 1.375
Epoch 1600, Error: 1.375
Epoch 1700, Error: 1.375
Epoch 1800, Error: 1.375
Epoch 1900, Error: 1.375
Final weights: [[ 1.          0.11696194 -1.          1.        ]
 [ 0.35641588  1.         -1.          1.        ]
 [ 1.          1.          1.          1.        ]
 [-1.         -1.         -1.          0.55316147]]
Input: [0 0 0 0], Prediction: [0 0 0 0]
Input: [0 0 0 1], Prediction: [0 0 0 1]
Input: [0 0 1 0], Prediction: [1 1 1 1]
Input: [0 0 1 1], Prediction: [0 0 0 1]
Input: [0 1 0 0], Prediction: [0 1 0 1]
Input: [0 1 0 1], Prediction: [0 0 0 1]
Input: [0 1 1 0], Prediction: [1 1 0 1]
Input: [0 1 1 1], Prediction: [0 1 0 1]
Input: [1 0 0 0], Prediction: [1 0 0 1]
Input: [1 0 0 1], Prediction: [0 0 0 1]
Input: [1 0 1 0], Prediction: [1 1 0 1]
Input: [1 0 1 1], Prediction: [1 0 0 1]
Input: [1 1 0 0], Prediction: [1 1 0 1]
Input: [1 1 0 1], Prediction: [0 0 0 1]
Input: [1 1 1 0], Prediction: [1 1 0 1]
Input: [1 1 1 1], Prediction: [1 1 0 1]