One neuron gives one sum per row. A layer is several neurons that all read the same inputs, each with its own weights and its own bias. Put each neuron's weights in a column of one table, W, and numpy works out every neuron for every row in one line.
Tables in numpy: a.shape gives back a table's size as (rows, columns). Between two tables, @ is matrix multiplication: every row of the left table meets every column of the right one, and their items are multiplied in pairs and added up. The left table's columns must match the right table's rows; the result has the left's rows and the right's columns.
A = np.array([[1, 2],
[3, 4]])
B = np.array([[10],
[1]])
print(A.shape, B.shape) # (2, 2) (2, 1)
print(A @ B) # [[12]
# [34]]
print(A + np.array([100, 0]))
# [[101 2]
# [103 4]]
The last line shows how a short array is added to a table: its first number goes to the whole first column, its second to the second column. That's how + b gives each neuron its bias. Here X is (4, 2) and W is (2, 3): the inner 2s match, so the layer's sums form a (4, 3) table, 4 rows and 3 neurons.
Your code must print these lines:
X shape: (4, 2) W shape: (2, 3) row 4, neuron 1: 3.0 z shape: (4, 3) [[ 0. 1. -0.5] [ 2. 2. -1.5] [ 1. -1. 0. ] [ 3. 0. -1. ]] [[0.5 0.731 0.378] [0.881 0.881 0.182] [0.731 0.269 0.5 ] [0.953 0.5 0.269]]
After them, the check prints z[3, 0] and the error numpy gives when the shapes don't match: (4, 2) @ (3, 3).
Your task: run every row of X through all three neurons in one line and store the sums in z, print its shape and the sums, then print the layer's output: sigmoid(z) rounded to 3 decimals.
Assignment
Follow the Coding Tutorial to compute the whole layer at once and print its shape, its sums and its output.
Hint
Write X @ W, not W @ X: (2, 3) @ (4, 2) has inner sizes 3 and 4, which don't match. Then add b, and round the sigmoid table with np.round(…, 3).
Free plan: 30 tutor messages. PRO is unlimited.
