torch.frombuffer
function frombuffer(buffer: TypedArray | number[], options: TensorOptions = {}): TensorCreate a tensor from a buffer.
Interprets a buffer as a 1-D tensor. Accepts either a typed array or a plain array of byte values (like Python's bytes/bytearray).
Parameters
bufferTypedArray | number[]- Typed array (Float32Array, Int32Array, etc.) or plain array of bytes
optionsTensorOptionsoptional
Returns
Tensor– 1-D tensor containing the buffer dataExamples
const buf = new Float32Array([1, 2, 3, 4]);
const t = torch.frombuffer(buf); // Shape: [4]
// From raw bytes (like Python bytes)
// [0, 0, 128, 63, 0, 0, 0, 64] are bytes for float32 values 1.0 and 2.0
const t2 = torch.frombuffer([0, 0, 128, 63, 0, 0, 0, 64]); // Shape: [2], values: [1.0, 2.0]