Compute Several Weighted Scores
Treat each weight column as another familiar weighted sum, then add one bias to each output column.
One weight array gives one score per observation. We may instead need two different scores from the same sensor values. Each score can begin as the weighted sum we already know.
The two weight arrays answer two separate questions about every observation. We can calculate them separately before arranging them into one operation.
Expand Both Outputs for One Observation
For the first observation [-1.0, -2.0], output 0 uses weights [1.0, 0.5]:
(-1.0 × 1.0) + (-2.0 × 0.5)
= -1.0 + -1.0
= -2.0
Output 1 uses weights [-0.5, 1.0]:
(-1.0 × -0.5) + (-2.0 × 1.0)
= 0.5 + -2.0
= -1.5
These are two applications of the previous lesson's rule. The same two sensor values are paired with a different weight set for each output.
Now add one fixed bias to each output:
bias = np.array([0.25, -0.5])
The first observation's final outputs are:
output 0: -2.0 + 0.25 = -1.75
output 1: -1.5 + -0.5 = -2.0
The bias has shape (2,) because there are two outputs. It is not one value
per sensor. Its positions belong to the output axis that exists after the
weighted sums have been calculated.
Q1. Calculate one biased output by hand
For the third observation [1.0, 2.0], calculate output 1 using weights
[-0.5, 1.0] and bias -0.5.
Select one choice, then check.
HintKeep this output's values together
Calculate 1.0 * -0.5 + 2.0 * 1.0, then add -0.5.
SolutionAdd the output-1 bias last
The contributions are -0.5 and 2.0, so the weighted sum is 1.5.
Adding the bias gives 1.0.
Store One Weight Set in Each Column
Arrange the two weight arrays as columns of one table:
Read its axes before using it:
- axis 0 contains sensors;
- axis 1 contains outputs.
Column 0 is [1.0, 0.5], the weights for output 0. Column 1 is [-0.5, 1.0], the weights for output 1. Keeping outputs in columns makes the sensor
positions meet in this order:
centered @ weights
(observations, sensors) @ (sensors, outputs)
-> (observations, outputs)
For our arrays, (3, 2) @ (2, 2) produces shape (3, 2). The matching middle
sizes are the two sensors. The output keeps three observations and introduces
two output positions.
[[-2. -1.5]
[ 0. 0. ]
[ 2. 1.5]]
weighted[0, 1] is the -1.5 calculation expanded above. Every entry follows
the same rule: choose one observation row and one output column, multiply the
two aligned sensor positions, then add the contributions.
Q2. Predict the output axes and shape
measurements has shape (7, 3): seven observations and three sensors. A
weight table has shape (3, 4): three sensors and four outputs. What does
measurements @ weights return?
Select one choice, then check.
HintKeep the outer axes in order
(observations, sensors) @ (sensors, outputs) leaves observations first and
outputs second.
SolutionThe result is observations by outputs
The matching sensor size is 3. The remaining sizes are 7 observations and 4
outputs, so the result has shape (7, 4).
Add One Bias per Output
The weighted table and bias have compatible named axes:
weighted: (3 observations, 2 outputs)
bias: (2 outputs)
Adding the bias applies the same two output adjustments to every observation:
[[-1.75 -2. ]
[ 0.25 -0.5 ]
[ 2.25 1. ]]
The order matters. centered @ weights first combines sensor values into two
outputs. The bias then aligns with those output columns. Writing weights @ centered would reverse the roles and does not have matching inner sizes for
these shapes. Reordering operands is not a formatting choice; it changes which
positions meet and which axes remain.
Q3. Compute two outputs and add their biases
Complete the weight table and calculation. Print the weighted values, final scores, and output shape.
Editable Python
Ready to run.
HintPlace outputs in columns
Use rows [1.0, -0.5] and [0.5, 1.0]. Then calculate
weighted = centered @ weights and scores = weighted + bias.
SolutionCombine sensors before adding output bias
Several weighted outputs are several familiar weighted sums placed side by side. Store one weight set in each output column, match the sensor axis, and keep the resulting axes in observation–output order. Add one bias per output only after the sensor contributions have been combined.
References
- NumPy documentation:
numpy.matmul— matching inner sizes, output shape, and the@operator.