A neural network stacks layers. The hidden layer is the layer from the last lesson: it turns each row's 2 inputs into 3 numbers between 0 and 1. The output layer, a single neuron, reads those 3 numbers as its inputs and gives one answer per row. Sending the inputs through every layer, in order, is the forward pass.
The shapes chain together: X is (4, 2) and W1 is (2, 3), so the hidden layer is (4, 3). W2 is (3, 1), one weight for each hidden neuron, so the output is (4, 1): one answer per row.
Returning two values: a function can give back two results separated by a comma, and two names before the = take them in order. Here the second result is built from the first, the way the output layer is built from the hidden one:
def double_and_square(n):
doubled = n * 2
squared = doubled ** 2
return doubled, squared
d, s = double_and_square(3)
print(d, s) # 6 36
A column as a row: in a[:, 0], the part before the comma picks rows and the part after picks columns, and : means all of them. So np.array([[7], [8]])[:, 0] gives back the flat array [7 8].
Your code must print these lines:
W1 shape: (2, 3) W2 shape: (3, 1) out: [0.736 0.704 0.877 0.816]
After them, the check prints the shapes of hidden and out, the hidden table, and what forward gives for the single row [1, 1].
Your task: write a function forward(X) that runs the hidden layer, then the output layer, and returns both results; call it on X and print the network's four answers. Store the two results of forward(X) in variables named hidden and out, because the check reads them. Print only the two lines shown above. Do not print the shapes, the hidden table or the result for [1, 1] yourself, since the check prints those.
Assignment
Follow the Coding Tutorial to write forward, run it on X and print the output column.
Hint
Inside forward, the output layer reads hidden, not X: (4, 3) @ (3, 1). Wrap each layer's sum in sigmoid(…), and round the answers with np.round(…, 3).
Free plan: 30 tutor messages. PRO is unlimited.
