The loss says how wrong the network is. To make it less wrong you change a weight, but which way, and how much does it help? The gradient answers that: the slope of the loss for one weight, how much the loss moves per unit the weight moves.
Find it with a nudge. Change the weight by a tiny amount, compute the loss again, and divide the change in the loss by the change in the weight. Here it is on a simple function, f(w) = w * w, at w = 3:
def f(w):
return w * w
print(round((f(3.001) - f(3)) / 0.001, 3)) # 6.001
Calculus says the exact slope of w * w is 2 * w, which is 6 here, and the nudge gets almost the same. A positive slope means the value rises when w rises, so to go down you lower w. To nudge one weight in an array, add an array of the same length: adding numpy arrays adds them item by item, so np.array([1, 2]) + np.array([10, 0]) gives back an array holding 11 and 2.
The formula. For one neuron the exact slope comes in one go. For each row, twice the error says how the squared error changes with the answer, and out * (1 - out) says how the sigmoid changes with its sum; their product says how the loss changes with that row's sum. A weight moves each row's sum by that row's input, so you multiply by the input column X[:, 0] (every row, column 0) and average over the rows with np.mean.
The starting code has one neuron learning AND, with w = [0.5, 0.5] and b = 0, and prints its answers and its loss. Your finished script prints this (the last two lines come from the check: the same test for the bias b, and a nudge a thousand times smaller):
out: [0.5 0.622 0.622 0.731] loss: 0.27431 loss after the nudge: 0.274357 slope from the nudge: 0.047 slope from the formula: 0.047 bias: slope from the nudge 0.182 from the formula 0.182 w[0] with a nudge of 0.000001: 0.047
Your task: nudge w[0] up by 0.001, print the new loss and the slope from the nudge, then compute the same slope with the formula and print it.
Assignment
Follow the Coding Tutorial to find the gradient for w[0] twice: with a nudge, then with the formula.
Hint
Both slopes should print 0.047. If the formula gives something else, check that you multiply by the first column of X only, not by the whole X.
Free plan: 30 tutor messages. PRO is unlimited.
