Learn · 1
Tensors
A tensor is an array with a shape, living in GPU memory. Everything below runs in this page — edit any block and press Run, or ⌘/Ctrl + Enter.
Acquiring the device
await init() comes before anything else. Getting a WebGPU adapter is
asynchronous and there is no way around it — call it once, then forget it.
If it fails, borch stops there rather than falling back to something slower.
Making one
Tensor.from(data, shape) takes a flat array and the shape you want.
The factory names are torch's: zeros, ones,
arange, randn, eye, linspace.
Reading values is awaited
This is the second place borch differs from torch. The numbers live in GPU memory,
and getting them back to JavaScript is a copy — so item() and
toArray() return promises. Forward and backward passes are
synchronous; only reading crosses back.
Reductions that torch splits in two are split here too:
amax() gives the value, max(dim) gives
{ values, indices }.
Operations
Elementwise operations broadcast the way torch's do. Note that operators take
tensors, not numbers — JavaScript has no operator overloading, so a scalar is
written Tensor.full([], 5), and powScalar(k) exists for
the common case.
Shapes and views
reshape, transpose, slice and friends give
you a view onto the same storage where torch would — the conformance suite checks
that 13 of these share storage exactly as torch does, because a copy where torch
shares is a bug you only notice much later.
A tensor, drawn
A tensor is just numbers with a shape. Here are 64 of them as an 8×8 grid, drawn straight from the values — brighter is larger. Every image, weight and activation in this book is a grid like this.
What to remember
- A tensor has a shape, a dtype and a device (the GPU).
- Factories:
zeros,ones,arange,randn,eye,linspace. - Reading a value crosses back from the GPU, so
item()andtoArray()are awaited. - Elementwise ops broadcast;
reshape,transpose,slicegive views that share storage.