compute.ndarray.squeeze¶
Summary¶
compute.ndarray.squeeze removes length-one axes. Use compute.ndarray.expandDims to insert length-one axes. Both create views of the same values.
Syntax¶
squeeze<T extends CPUndarray | GPUndarray>(a: T, axes?: number | ReadonlyArray<number>): T
expandDims<T extends CPUndarray | GPUndarray>(a: T, axis: number | ReadonlyArray<number>): T
const row = nd.expandDims(a, 0);
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live input array. |
axes |
number | ReadonlyArray<number> |
No | For squeeze, distinct input axes of length one. Omission selects all axes of length one. |
axis |
number | ReadonlyArray<number> |
For expandDims |
Distinct insertion positions in the final output rank. |
Negative indices count from the end of the relevant rank. Out-of-range and repeated axes throw, as does squeezing an axis whose length differs from one.
Returns¶
T - Shared-storage view with adjusted shape and byte strides. Squeezing all axes of an array with one element yields a scalar shape [].
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [3], [1, 2, 3]);
const b = nd.expandDims(a, [0, -1]);
const c = nd.squeeze(b);
console.log(b.shape, c.shape); // [1, 3, 1], [3]
c.destroy();
b.destroy();
a.destroy();