Foundations: Tensors & Data
Basic Tensor Operations
's amplifier has stopped on a shape error. wants the feed restored before more readings arrive.
At the start of the shift, 's amplifier has stopped with this error: Error in matMul: inner shapes (64) and (1) of Tensors with shapes 1,64 and 1,64 and transposeA=false and transposeB=false must match. All sixty-four channels, one stream of readings per sensor, are waiting for the fix.


The hull array takes 983 thousand raw samples a minute before integration boils them down to readings. Whatever that red text is, it is not keeping up. Show me which readings go into each result before you send another hour to this console.
Restore the feed to 's console. First check the calculation on the recording just arranged: average neighboring moments for each sensor, apply the gain, then combine channels into a few weighted sums. The full 64-channel recording stays in the archive while you trace this excerpt.
Matching sensors across neighboring moments
wanted to multiply each reading by its matching gain, the number that
controls how much to amplify it. That is element-wise multiplication,
written mul. He used matMul instead. has a different
rule, and [1, 64] times [1, 64] does not meet it.
reopens the three moments checked. He keeps sensors A and B in the order listed in the recording header, leaving C and D aside for now. points to the first two rows and waits for the calculation.
The earlier excerpt contains these values:
| Local moment | Sensor A | Sensor B |
|---|---|---|
| 1 | 2 | 20 |
| 2 | 3 | 18 |
| 3 | 4 | 16 |
These are fictional recorded readings from the same selection used in Shapes. The header gives their order. It supplies no physical unit here, so we keep them as readings without adding one.
makes two overlapping views. The first holds moments 1 and 2. The second holds moments 2 and 3. Matching positions now pair the same sensor at neighboring moments. Averaging each pair smooths this short . It also changes the readings, so the raw recording stays available.
// Columns stay in header order: sensor A, then sensor B.
const pass1 = tf.tensor2d([[2, 20], [3, 18]]); // moments 1 and 2
const pass2 = tf.tensor2d([[3, 18], [4, 16]]); // moments 2 and 3
const gain = 2;
const { merged, boosted } = tf.tidy(() => {
const merged = pass1.add(pass2).div(2);
const boosted = merged.mul(gain);
return { merged, boosted };
});
console.log(merged.arraySync()); // [[2.5, 19], [3.5, 17]]
console.log(boosted.arraySync()); // [[5, 38], [7, 34]]
// Keep these tensors for the bank calculation below.Follow sensor A through the first pair: (2 + 3) / 2 = 2.5.
Gain 2 gives 5. Sensor B uses (20 + 18) / 2, then the same gain.
The gain is a dimensionless setting chosen for this example. Doubling a value
also doubles any error in it. This example establishes no noise reduction
from independent repeated measurements.
Each operation creates a tensor. tf.tidy() releases temporary tensors when
its synchronous callback finishes. That callback must finish without await.
TensorFlow.js keeps the tensors in the returned object, so you can use
boosted in the next calculation and dispose of both results afterward.
One channel-bank sum
also wants to inspect channels together. A bank is one supplied set of channel . Bank 0 keeps A, Bank 1 keeps B, and Bank 2 adds A and B. These fixed settings help you check the calculation. They make no claim about which sensors detect an .
Matrix multiplication computes a dot product for each output: multiply
each channel by its weight, then add the products. The first boosted row is
[5, 38]. Bank 2 uses [1, 1], so its score is 5 * 1 + 38 * 1 = 43.
Element-wise mul produces the separate products. matMul also sums them.
| Tensor | Row meaning | Column meaning | Shape |
|---|---|---|---|
boosted | Neighboring-moment pair | Sensor A, B | [2, 2] |
mix | Bank 0, 1, 2 | Sensor A, B | [3, 2] |
mix.transpose() | Sensor A, B | Bank 0, 1, 2 | [2, 3] |
mixed | Neighboring-moment pair | Bank 0, 1, 2 | [2, 3] |
Transpose puts each bank's weights in a column. The multiplication then has
matching sensor axes: [2, 2] times [2, 3] produces [2, 3].
// Continue with boosted from merge-two-passes.ts.
const mix = tf.tensor2d([[1, 0], [0, 1], [1, 1]]);
const mixed = tf.tidy(() => boosted.matMul(mix.transpose()));
console.log(mixed.arraySync()); // [[5, 38, 43], [7, 34, 41]]
tf.dispose([pass1, pass2, merged, boosted, mix, mixed]);In the activity, select Channel-bank sums, then choose an output row and
column. The highlighted sensor values and weights show every contributor to
that score. Check their meanings as well as their counts. The general matrix
rule is [m, n] times [n, p] produces [m, p], where the shared n counts
the values in each dot product.
Broadcasting one reference row
keeps the first moment beside the chart as a fixed reference. He checks what the same merge function does when he reuses that row for both outputs.
This case blends each row with the first moment. It is a chosen comparison,
not another capture of those moments. The reference has shape [2]: A's
reading 2, then B's reading 20.
const earlier = tf.tensor2d([[2, 20], [3, 18]]); // moments 1 and 2
const reference = tf.tensor1d([2, 20]); // moment 1, reused
const blended = tf.tidy(() => earlier.add(reference).div(2));
console.log(blended.arraySync()); // [[2, 20], [2.5, 19]]
tf.dispose([earlier, reference, blended]); lets that one row apply to both rows. TensorFlow.js compares
shapes from right to left. Each pair of sizes must match, or one must be 1.
A missing leading axis acts like size 1. Here, [2] matches the last axis
of [2, 2]. The result keeps shape [2, 2].
Select Fixed first-moment reference in the activity. Trace sensor B in
the second output row: (18 + 20) / 2 = 19 at gain 1. The same reference
value reaches each row, while A and B keep their own columns. A scalar gain
has one value and applies to every result.
Challenge
Help write the reusable functions behind those calculations. The
challenge supplies separate diagnostic inputs with varied values and shapes,
including a [3, 2] block paired with one [2] row. These are test cases for
your code, rather than excerpts of the 64-channel recording.
Return both the average and the amplified result from mergePasses. Return
one per reading and bank from mixChannels. The result tables
show your function's outputs for the supplied cases. Check the numbers as
well as the shapes before uses the functions on the full recording.
