TensorFlow.js reshape vs transpose
reshape() changes a tensor's shape while keeping its flattened values in order. transpose() permutes its axes. For a two-dimensional tensor, the default transpose exchanges rows and columns.
By Tensorcraft, a group of JavaScript engineers. Updated . Example verified with TensorFlow.js 4.22.0.
One sensor's readings over time
In Deep Orbit, the crew investigates an unexplained signal. Farah selects sensors A, B, C, and D from Demir's fictional 64-sensor recording. The excerpt below contains those four sensors at three local moments. It uses the same data as the free tensor-shapes activity.
The supplied header says each row contains one moment, with sensors in A, B, C, D order. Moment labels 0, 1, and 2 refer to this excerpt. They are not timestamps for the full recording. These are illustrative readings, without assigned physical units.
The crew needs each sensor's readings in time order. Sensor A has [2, 3, 4]. Both operations below produce shape [4, 3], but they produce different rows.
Select any reading in the input. Its matching cell stays highlighted in both outputs. The comparison uses JavaScript array operations for this fixed example. The TensorFlow.js code below produces the same values.
| Local moment | A | B | C | D |
|---|---|---|---|---|
| 0 | ||||
| 1 | ||||
| 2 |
| Row | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 2 | 20 | 5 |
| 1 | 8 | 3 | 18 |
| 2 | 7 | 8 | 4 |
| 3 | 16 | 6 | 9 |
| Sensor | 0 | 1 | 2 |
|---|---|---|---|
| A | 2 | 3 | 4 |
| B | 20 | 18 | 16 |
| C | 5 | 7 | 6 |
| D | 8 | 8 | 9 |
Sensor B at local moment 0 has value 20. Reshape puts it at row 0, column 1. Transpose puts it at sensor row B, moment column 0.
How reshape groups the values
Flatten the input row by row and you get [2, 20, 5, 8, 3, 18, 7, 8, 4, 16, 6, 9]. Reshape groups that sequence into four rows of three values. Its first row becomes [2, 20, 5]: readings from sensors A, B, and C at moment 0.
The numbers retain their flattened order. The new row does not represent sensor A over time. A later chart that labels this row as sensor A would show other sensors' readings under A's name.
Reshape is useful when you need a different grouping that preserves this order. For example, adding a batch dimension for one example can change [3, 4] to [1, 3, 4]. The target dimensions must multiply to the original element count. Here that count is 12.
How transpose exchanges the axes
For this matrix, input.transpose() places input value [moment][sensor] at output position [sensor][moment]. Sensor A becomes the first row, [2, 3, 4]. Sensor B becomes [20, 18, 16]. Each selected sensor keeps its readings in local moment order.
For tensors with more than two dimensions, the permutation argument specifies the new axis order. For example, input.transpose([1, 0]) explicitly exchanges the two axes in this example. Without a permutation, TensorFlow.js reverses the axis order.
The runnable TensorFlow.js example
Install the pinned runtime with npm install @tensorflow/tfjs@4.22.0. Save the following code as compare.mjs and run node compare.mjs. A browser project can use the same module through its bundler.
import * as tf from "@tensorflow/tfjs";
await tf.ready();
// Local moments 0, 1, 2. Sensor order: A, B, C, D.
const packet = [
[
2,
20,
5,
8
],
[
3,
18,
7,
8
],
[
4,
16,
6,
9
]
];
const comparison = tf.tidy(() => {
const input = tf.tensor2d(packet);
return {
reshaped: input.reshape([4, 3]).arraySync(),
transposed: input.transpose().arraySync(),
};
});
console.log(comparison);The callback returns ordinary JavaScript arrays from arraySync(). tf.tidy() disposes the tensors created inside that callback because the result contains no tensors. If you return a tensor instead, you keep responsibility for disposing it when you finish using it.
This small example uses synchronous reads so you can inspect the values directly. Reading tensor data synchronously can block the browser while a backend finishes its work. Larger applications can use asynchronous reads and dispose tensors explicitly afterward. tf.tidy() requires a synchronous callback.
Shape alone cannot identify a sensor
A shape such as [4, 3] tells you dimension sizes. The recording header supplies the sensor order and moment order. Keep that metadata with the readings as you prepare model inputs or label a chart.
This grouping task uses ordinary data transformation. It does not train a model or establish a physical anomaly. It practices a part of ML preparation: preserving what each value means while arranging the input for the next operation.
The free tensor-shapes activity
In the interactive lesson, you predict a row, compare the operations, and apply the idea to a second excerpt. Deep Orbit's first five lessons are free, with no account required. The full course continues into model building, training, evaluation, and deployment.
Deep Orbit course overview and course pricing describe the available course and paid continuation.
The JavaScript ML learning path shows where data preparation fits. The independent project case follows recorded inputs through a model decision and an inspected mistake.
Sources and example provenance
The input and outputs use the worked excerpt in Tensorcraft's tensor-shapes lesson. The example describes authored fictional data. TensorFlow.js 4.22.0 produced both outputs during verification.