TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/autograd/llms.txt
Use this file to discover all available pages before exploring further.
tensor package provides Tensor[T], a generic multi-dimensional array that is the data backend for Variable. It handles memory layout, broadcasting, and all raw numeric operations. You will typically interact with tensors indirectly through the variable and function packages.
Import path:
github.com/itsubaki/autograd/tensorTensor struct
Size of each dimension. A scalar tensor has
Shape == nil.Step in
Data for a unit increment along each dimension. Non-contiguous views (created by Transpose, BroadcastTo) have irregular strides and share the underlying Data slice.Flat backing slice. Physical layout is row-major for contiguous tensors.
When
true, calls to Set or AddAt panic. Set automatically on broadcast views.Constructors
New
New
Zeros / Ones / Full
Zeros / Ones / Full
Rand / Randn
Rand / Randn
Rand fills with uniform [0.0, 1.0) values; Randn fills with standard-normal values. Pass a randv2.Source for reproducibility.ZeroLike / OneLike
ZeroLike / OneLike
v filled with zeros or ones.Clone / Reshape / Ravel / Flatten
Clone / Reshape / Ravel / Flatten
Clone always returns a contiguous copy. Reshape returns a view when possible; use -1 in shape to infer one dimension. Ravel returns a view if the tensor is contiguous, otherwise copies. Flatten always copies.Arange / Linspace
Arange / Linspace
Identity / Eye / Scalar
Identity / Eye / Scalar
Identity creates a rectangular identity matrix. Eye creates a square one. Scalar wraps a single value.Methods
NumDims
Size
At
Set
ReadOnly.
AddAt
value to the element at the given indices in place. Panics if ReadOnly.
Seq2
(rowIndex, rowSlice) pairs.
Element-wise functions
F (unary)
float64 → int).
F2 (binary)
v and w. Both tensors are broadcast to a common shape before the operation.
Arithmetic operations
All operations return new tensors and broadcast automatically.| Function | Description |
|---|---|
Add[T](v, w) | v + w |
Sub[T](v, w) | v - w |
Mul[T](v, w) | v * w (element-wise) |
Div[T](v, w) | v / w |
AddC[T](c, v) | c + v |
SubC[T](c, v) | c - v |
MulC[T](c, v) | c * v |
Pow(p, v) | v^p |
Sqrt(v) | √v |
Exp(v) | eᵛ |
Log(v) | ln(v) |
Sin(v) | sin(v) |
Cos(v) | cos(v) |
Tanh(v) | tanh(v) |
Clip[T](v, min, max) | Clamp to [min, max] |
Mask[T](v, f) | 1 where f(x) is true, else 0 |
Reduction operations
| Function | Description |
|---|---|
Sum[T](v, axes...) | Sum over axes (or all elements) |
Max(v, axes...) | Max over axes |
Min(v, axes...) | Min over axes |
Mean[T](v, axes...) | Mean over axes |
Variance(v, axes...) | Variance over axes |
StdDev(v, axes...) | Standard deviation |
Argmax[T](v, axis) | Index of max along axis → *Tensor[int] |
Shape operations
| Function | Description |
|---|---|
Reshape[T](v, shape...) | Change shape (view or copy) |
Transpose[T](v, axes...) | Permute axes (view) |
BroadcastTo[T](v, shape...) | Broadcast to shape (view) |
Broadcast[T](v, w) | Broadcast both to common shape |
SumTo[N](v, shape...) | Sum to target shape |
Squeeze[T](v, axes...) | Remove size-1 dimensions |
Expand[T](v, axis) | Insert size-1 dimension |
KeepDims(shape, axes) | Replace reduced axes with 1 |
Combining and indexing
| Function | Description |
|---|---|
Concat[T](v, axis) | Concatenate along axis |
Split[T](v, size, axis) | Split into parts |
Stack[T](v, axis) | Stack along new axis |
Take[T](v, axis, indices) | Gather elements at indices |
ScatterAdd[T](v, w, axis, indices) | Scatter-add w into v |
Flip[T](v, axes...) | Reverse elements along axes |
Tile[T](v, n, axis) | Repeat v n times along axis |
Repeat[T](v, n, axis) | Repeat each element n times |
Tril[T](v, k...) | Lower-triangular mask |
Type conversion utilities
int or float64.
Comparison and utility
| Function | Description |
|---|---|
Equal(v, w) | Element-wise equality → *Tensor[int] |
IsClose(v, w, tol...) | Element-wise approximate equality |
EqualAll(v, w) | All elements equal |
IsCloseAll(v, w, tol...) | All elements approximately equal |
IsContiguous[T](v) | True if row-major contiguous |
SliceEqual(a, b) | Compare two []int slices |
Contiguous[T](v) | Return contiguous tensor (clone if needed) |
FlatIndex[T](v, indices...) | Flat index from multi-dim indices |
UnravelIndex[T](v, index) | Multi-dim indices from flat index |
MatMul[T](v, w) | Batched matrix multiply (parallel) |
Relationship to Variable
Variable.Data is always a *Tensor[float64]. The variable package wraps tensor operations in Function objects that record the computation graph:
tensor operations when you need to manipulate data without affecting the computation graph, for example inside a Forwarder.Forward implementation.