import math def sigmoid(x): return 1 / (1 + math.exp(-x)) def forward(inputs, weights, biases): hidden = [] for i in range(3): total = 0 for j in range(2): total += inputs[j] * weights[i][j] total += biases[i] hidden.append(sigmoid(total)) output = 0 for i in range(3): output += hidden[i] * weights[3][i] output += biases[3] return output, hidden def loss_function(expected, predicted): return (expected - predicted) ** 2 # Initial weights weights = [ [0.2, 0.4], [0.3, 0.1], [0.5, 0.2], [0.4, 0.3, 0.2] ] biases = [0.1, 0.2, 0.1, 0.1] # Training example inputs = [3, 4] expected = 12 learning_rate = 0.01 # Forward pass prediction, hidden = forward(inputs, weights, biases) # Calculate loss loss = loss_function(expected, prediction) print("Prediction:", prediction) print("Loss:", loss) # Backpropagation: output layer error_gradient = 2 * (prediction - expected) output_weight_gradients = [] for i in range(3): gradient = error_gradient * hidden[i] output_weight_gradients.append(gradient) output_bias_gradient = error_gradient # Update output weights for i in range(3): weights[3][i] -= learning_rate * output_weight_gradients[i] biases[3] -= learning_rate * output_bias_gradient print("\nUpdated output weights:", weights[3]) print("Updated output bias:", biases[3])