fork download
  1. import numpy as np
  2.  
  3. # Define the activation function (without sigmoid as you requested)
  4. def activation_function(x):
  5. return x # Identity function, which is just a linear activation
  6.  
  7. # Xavier initialization for weights
  8. def initialize_weights(input_size, output_size):
  9. return np.random.randn(input_size, output_size) * np.sqrt(2 / (input_size + output_size))
  10.  
  11. # Normalize input vector
  12. def normalize_input(input_vector):
  13. return 2 * (input_vector - np.min(input_vector)) / (np.max(input_vector) - np.min(input_vector)) - 1
  14.  
  15. # Training function
  16. def train_neural_network(input_data, target_data, learning_rate=0.1, max_epochs=2000, error_threshold=1e-5):
  17. input_size = len(input_data[0])
  18. output_size = len(target_data[0])
  19.  
  20. # Initialize weights and bias
  21. weights = initialize_weights(input_size, output_size)
  22. biases = np.zeros(output_size)
  23.  
  24. prev_weight_update = np.zeros_like(weights)
  25. prev_bias_update = np.zeros_like(biases)
  26.  
  27. # Dynamic learning rate decay
  28. decay_factor = 0.001
  29.  
  30. for epoch in range(max_epochs):
  31. total_error = 0
  32. for i in range(len(input_data)):
  33. input_vector = input_data[i]
  34. target_vector = target_data[i]
  35.  
  36. # Normalize the input vector
  37. normalized_input = normalize_input(input_vector)
  38.  
  39. # Forward pass
  40. output = np.dot(normalized_input, weights) + biases
  41. output = activation_function(output)
  42.  
  43. # Calculate error
  44. error = target_vector - output
  45. total_error += np.sum(error ** 2)
  46.  
  47. # Backpropagation (gradient descent)
  48. weight_gradient = -2 * np.outer(normalized_input, error)
  49. bias_gradient = -2 * error
  50.  
  51. # Momentum-based weight update
  52. weight_update = learning_rate * weight_gradient + 0.9 * prev_weight_update
  53. bias_update = learning_rate * bias_gradient + 0.9 * prev_bias_update
  54.  
  55. # Update weights and biases
  56. weights += weight_update
  57. biases += bias_update
  58.  
  59. prev_weight_update = weight_update
  60. prev_bias_update = bias_update
  61.  
  62. # Dynamic learning rate decay
  63. learning_rate = learning_rate / (1 + decay_factor * epoch)
  64.  
  65. # Early stopping: check if the error is below the threshold
  66. if total_error < error_threshold:
  67. print(f"Converged at epoch {epoch} with error: {total_error}")
  68. break
  69.  
  70. # Optional: Print status for every 100 epochs
  71. if epoch % 100 == 0:
  72. print(f"Epoch {epoch}, Error: {total_error}")
  73.  
  74. return weights, biases
  75.  
  76. # Test the network with the provided truth tables
  77. def test_neural_network(weights, biases, input_data):
  78. predictions = []
  79. for input_vector in input_data:
  80. normalized_input = normalize_input(input_vector)
  81. output = np.dot(normalized_input, weights) + biases
  82. output = activation_function(output)
  83. predictions.append(output)
  84. return predictions
  85.  
  86. # Define the truth tables
  87. input_data = [
  88. [0, 0, 0, 0],
  89. [0, 0, 0, 1],
  90. [0, 0, 1, 0],
  91. [0, 0, 1, 1],
  92. [0, 1, 0, 0],
  93. [0, 1, 0, 1],
  94. [0, 1, 1, 0],
  95. [0, 1, 1, 1],
  96. [1, 0, 0, 0],
  97. [1, 0, 0, 1],
  98. [1, 0, 1, 0],
  99. [1, 0, 1, 1],
  100. [1, 1, 0, 0],
  101. [1, 1, 0, 1],
  102. [1, 1, 1, 0],
  103. [1, 1, 1, 1]
  104. ]
  105.  
  106. # Corresponding targets (for the XOR problem)
  107. target_data = [
  108. [0, 0, 0, 0],
  109. [0, 0, 0, 1],
  110. [0, 0, 1, 0],
  111. [0, 0, 1, 1],
  112. [0, 1, 0, 0],
  113. [0, 1, 0, 1],
  114. [0, 1, 1, 0],
  115. [0, 1, 1, 1],
  116. [1, 0, 0, 0],
  117. [1, 0, 0, 1],
  118. [1, 0, 1, 0],
  119. [1, 0, 1, 1],
  120. [1, 1, 0, 0],
  121. [1, 1, 0, 1],
  122. [1, 1, 1, 0],
  123. [1, 1, 1, 1]
  124. ]
  125.  
  126. # Train the neural network
  127. learning_rate = 0.1
  128. max_epochs = 2000
  129. error_threshold = 1e-5
  130.  
  131. weights, biases = train_neural_network(input_data, target_data, learning_rate, max_epochs, error_threshold)
  132.  
  133. # Test the neural network
  134. predictions = test_neural_network(weights, biases, input_data)
  135.  
  136. # Display the results
  137. for i, (input_vector, target_vector, prediction) in enumerate(zip(input_data, target_data, predictions)):
  138. print(f"Table {i+1}: Input: {input_vector}, Target: {target_vector}, Prediction: {prediction}, Error: {np.abs(np.array(target_vector) - np.array(prediction))}")
  139.  
Success #stdin #stdout #stderr 2.84s 29092KB
stdin
Standard input is empty
stdout
Epoch 0, Error: nan
Epoch 100, Error: nan
Epoch 200, Error: nan
Epoch 300, Error: nan
Epoch 400, Error: nan
Epoch 500, Error: nan
Epoch 600, Error: nan
Epoch 700, Error: nan
Epoch 800, Error: nan
Epoch 900, Error: nan
Epoch 1000, Error: nan
Epoch 1100, Error: nan
Epoch 1200, Error: nan
Epoch 1300, Error: nan
Epoch 1400, Error: nan
Epoch 1500, Error: nan
Epoch 1600, Error: nan
Epoch 1700, Error: nan
Epoch 1800, Error: nan
Epoch 1900, Error: nan
Table 1: Input: [0, 0, 0, 0], Target: [0, 0, 0, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 2: Input: [0, 0, 0, 1], Target: [0, 0, 0, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 3: Input: [0, 0, 1, 0], Target: [0, 0, 1, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 4: Input: [0, 0, 1, 1], Target: [0, 0, 1, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 5: Input: [0, 1, 0, 0], Target: [0, 1, 0, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 6: Input: [0, 1, 0, 1], Target: [0, 1, 0, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 7: Input: [0, 1, 1, 0], Target: [0, 1, 1, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 8: Input: [0, 1, 1, 1], Target: [0, 1, 1, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 9: Input: [1, 0, 0, 0], Target: [1, 0, 0, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 10: Input: [1, 0, 0, 1], Target: [1, 0, 0, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 11: Input: [1, 0, 1, 0], Target: [1, 0, 1, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 12: Input: [1, 0, 1, 1], Target: [1, 0, 1, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 13: Input: [1, 1, 0, 0], Target: [1, 1, 0, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 14: Input: [1, 1, 0, 1], Target: [1, 1, 0, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 15: Input: [1, 1, 1, 0], Target: [1, 1, 1, 0], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
Table 16: Input: [1, 1, 1, 1], Target: [1, 1, 1, 1], Prediction: [nan nan nan nan], Error: [nan nan nan nan]
stderr
./prog.py:13: RuntimeWarning: invalid value encountered in true_divide