# Our training data x = 3 expected = 21 # Initial weight and bias weight = 1.0 bias = 0.0 # Learning rate learning_rate = 0.01 def predict(x, weight, bias): return x * weight + bias def loss(expected, predicted): return (expected - predicted) ** 2 # Training for i in range(100): prediction = predict(x, weight, bias) current_loss = loss(expected, prediction) # Calculate numerical gradient epsilon = 0.0001 new_prediction = predict(x, weight + epsilon, bias) new_loss = loss(expected, new_prediction) gradient = (new_loss - current_loss) / epsilon # Update weight weight -= learning_rate * gradient if i % 10 == 0: print( "Iteration:", i, "Prediction:", round(prediction, 4), "Loss:", round(current_loss, 4), "Weight:", round(weight, 4) ) print("\nFinal prediction:", predict(x, weight, bias))