Speed

Build X Matrix

Arrange n observations along axis 0 and p features along axis 1 by transposing column streams rather than calling reshape(n, p).

The problem

Once Choose Feature Columns fixes which predictors enter the model, linear algebra demands one universal layout for the design matrix X: axis `0` indexes the `n` observations (rows) and axis `1` indexes the `p` features (columns). With X shaped (n, p) and weights w shaped (p, 1), the matrix product X.matmul(w) computes one dot product per observation.

Columnar files and sensor buffers frequently arrive feature-major as (p, n)—one array per feature across n timestamps. Turning (p, n) into (n, p) with .reshape(n, p) instead of .transpose(0, 1) matches the target shape while silently scrambling which numbers belong to which observation.

Work through the numbers

Suppose p = 2 sensor channels record n = 4 students as a 2 x 4 tensor: hours = [2, 3, 5, 6] on row 0 and practice = [1, 4, 2, 5] on row 1. Student 0 studied 2 hours with 1 practice test ([2, 1]); student 1 studied 3 hours with 4 practice tests ([3, 4]).

Transposing axes 0 and 1 builds the true 4 x 2 design matrix X = [[2, 1], [3, 4], [5, 2], [6, 5]]. Multiplying by w = [[3], [2]] yields [[8], [17], [19], [28]]. Compare .reshape(4, 2): reading flat memory [2, 3, 5, 6, 1, 4, 2, 5] two elements at a time produces [[2, 3], [5, 6], [1, 4], [2, 5]], pairing student 0's hours (2) with student 1's hours (3).

The live model

The grid shows X (hours in blue, practice in green) beside the dot-product column 3 * x0 + 2 * x1 in amber. At default obs2_hours = 5, row obs 2 holds [5, 2] with score 3 * 5 + 2 * 2 = 19. Drag obs2_hours left to 1 ([1, 2], score 7) or right to 9 ([9, 2], score 31): only row obs 2 changes, confirming that each row of X isolates a single observation.

Two 4-element feature streams transposed into the 4x2 design matrix X with observations on rows and features on columns.
let obs2_hours = scene.parameter("obs2_hours", 5.0, 1.0, 9.0, 4.0);
let streams = input.clone();
streams.set(2, obs2_hours);
let answer = streams.transpose(0, 1);
let w = torch.zeros(2, 1).to(DType.F64);
w.set(0, 3.0);
w.set(1, 2.0);
out.set("X", answer).set("Xw", answer.matmul(w));
{
  "X": [
    [
      2,
      1
    ],
    [
      3,
      4
    ],
    [
      5,
      2
    ],
    [
      6,
      5
    ]
  ],
  "Xw": [
    [
      8
    ],
    [
      17
    ],
    [
      19
    ],
    [
      28
    ]
  ]
}

Your turn

Notice how the starter already produces shape (4, 2), yet row 0 pairs student 0's hours (2) with student 1's hours (3) in [[2, 3], [5, 6], [1, 4], [2, 5]]. Reorient the (p, n) stream tensor so each row of answer gathers one student's [hours, practice] pair. Check runs on a (2, 3) stream pair and a (2, 5) stream pair.

The starter calls reshape(4, 2) on a (2, 4) stream tensor, scrambling observations across rows.
let streams = input;
let n = streams.size(1);
let p = streams.size(0);
let answer = streams.reshape(n, p);
out.set("X", answer);
{
  "X": [
    [
      2,
      3
    ],
    [
      5,
      6
    ],
    [
      1,
      4
    ],
    [
      2,
      5
    ]
  ]
}

Build the (n, p) design matrix answer from the (p, n) feature-stream tensor input, with one observation per row and one feature per column.

When the observation and feature axes swap cleanly, answer recovers [[2, 1], [3, 4], [5, 2], [6, 5]] with dot products [8, 17, 19, 28].

Read the result carefully

Why is the reshape bug so dangerous? Shape assertions (assert X.shape == (n, p)) pass without complaint. matmul(w) runs without throwing an error, gradient descent converges to finite weights, and only a manual spot-check of row 0 against the original record reveals that features from neighboring rows were spliced together. Whenever axes swap meaning ((features, batch) to (batch, features)), reach for transpose or stack(..., 1), never reshape.

Recap

  • Standardize X to shape (n, p): one row per observation on axis 0, one column per feature on axis 1.
  • Convert (p, n) feature-major streams into X via .transpose(0, 1) ([[2, 1], [3, 4], [5, 2], [6, 5]]).
  • Never use .reshape(n, p) to swap axes; matching dimensions does not mean matching coordinates.