Build y Vector
Shape the target vector y as an explicit (n, 1) column tensor so subtracting y from X.matmul(w) stays (n, 1) instead of broadcasting into an (n, n) grid.
The problem
In Build X Matrix, we shaped X as (n, p) so multiplying by a (p, 1) weight vector w produces column predictions pred = X.matmul(w) of shape (n, 1). Extracting the target column with table.select(1, 2) yields a 1D vector of shape (n,)—four numbers, one axis.
Subtracting (n,) from (n, 1) triggers one of tensor computing's nastiest silent bugs: broadcasting stretches (n, 1) across columns and (n,) down rows to create an (n, n) all-pairs difference matrix. Shaping y explicitly as (n, 1) keeps pred - y aligned row for row.
Work through the numbers
Take four records [x0, x1, y]: [1, 2, 9], [2, 3, 10], [3, 1, 14], and [4, 2, 13]. With w = [[3], [2]], column predictions pred = X.matmul(w) equal [[7], [12], [11], [16]] (shape 4 x 1).
Calling .unsqueeze(1) on [9, 10, 14, 13] forms y_col = [[9], [10], [14], [13]] (shape 4 x 1). Elementwise subtraction pred - y_col yields the 4 x 1 residual column [[-2], [2], [-3], [3]]. Without .unsqueeze(1), [[7], [12], [11], [16]] - [9, 10, 14, 13] expands into the 4 x 4 grid [[-2, -3, -7, -6], [3, 2, -2, -1], [2, 1, -3, -2], [7, 6, 2, 3]], burying the four true residuals along the diagonal among twelve cross-row differences.
The live model
The diagram pairs Target y (4 x 1 in blue) with Residual answer = pred - y at w = [[3], [2]] (pred = [[7], [12], [11], [16]]). At default as_column = 1, y is unsqueezed to (4, 1) and answer stays a green column [[-2], [2], [-3], [3]]. Set as_column = 0 to leave y as (4,): pred - y broadcasts into the 4 x 4 red grid [[-2, -3, -7, -6], [3, 2, -2, -1], [2, 1, -3, -2], [7, 6, 2, 3]].
![Column predictions Xw (4x1) minus unsqueezed column targets y (4x1) producing the aligned 4x1 residual vector [[-2], [2], [-3], [3]].](../assets/ml/build-y-vector--demo.png)
let as_column = scene.parameter("as_column", 1.0, 0.0, 1.0, 1.0);
let table = input;
let x0 = table.select(1, 0);
let x1 = table.select(1, 1);
let X = torch.stack(torch.list().add(x0).add(x1), 1);
let w = torch.zeros(2, 1).to(DType.F64);
w.set(0, 3.0);
w.set(1, 2.0);
let pred = X.matmul(w);
let y_raw = table.select(1, 2);
let answer = pred - y_raw;
if (as_column > 0.5) {
answer = pred - y_raw.unsqueeze(1);
}
out.set("pred", pred).set("residual", answer);
{
"pred": [
[
7
],
[
12
],
[
11
],
[
16
]
],
"residual": [
[
-2
],
[
2
],
[
-3
],
[
3
]
]
}Your turn
Sixteen red cells appear on the starter canvas because subtracting a 1D four-element target from a (4, 1) prediction compares every row with every other row. Give the extracted target vector a column axis so answer holds one residual per record in shape (4, 1). Check tests a three-row table and a five-row table.

let table = input;
let x0 = table.select(1, 0);
let x1 = table.select(1, 1);
let X = torch.stack(torch.list().add(x0).add(x1), 1);
let w = torch.zeros(2, 1).to(DType.F64);
w.set(0, 3.0);
w.set(1, 2.0);
let pred = X.matmul(w);
let y_col = table.select(1, 2);
let answer = pred - y_col;
out.set("pred", pred).set("residual", answer);
{
"pred": [
[
7
],
[
12
],
[
11
],
[
16
]
],
"residual": [
[
-2,
-3,
-7,
-6
],
[
3,
2,
-2,
-1
],
[
2,
1,
-3,
-2
],
[
7,
6,
2,
3
]
]
}Compute the (n, 1) residual column answer = pred - y_col from input so each prediction is compared only with its own row's target.
With both operands shaped (4, 1), the sixteen red cells collapse into the four green column residuals [[-2], [2], [-3], [3]].
Read the result carefully
Why does the (n, 1) - (n,) broadcast bug survive into production code so often? Because the next line in a training loop is usually loss = residual.pow(2.0).mean(), which reduces any shape—(4, 1) or (4, 4)—down to a single scalar.
Training still runs, loss still prints a single float, and gradients still flow, yet every prediction is now compared with every target, so the loss is smallest when all predictions sit at the mean of y: the model is pushed toward one constant instead of fitting individual targets. Locking both pred and y to (n, 1) eliminates that ambiguity completely.
Recap
X.matmul(w)withXof shape(n, p)andwof shape(p, 1)returns a 2D column tensor(n, 1).- Convert 1D selected targets
(n,)into(n, 1)via.unsqueeze(1)([[9], [10], [14], [13]]). - Verify
residualhas shape(n, 1)([[-2], [2], [-3], [3]]) before reducing to a scalar loss.