Skip to main content

Tensor API Reference

Full API specification for leanpass.Tensor.

Constructor

Tensor(data, requires_grad=False, name=None)
ParameterTypeDefaultDescription
dataarray-likeInput data (list, tuple, NumPy array, or Tensor)
requires_gradboolFalseWhether to track gradients for this tensor
namestr or NoneNoneOptional label for debugging and graph visualization

Raises: ValueError if data cannot be converted to a float64 NumPy array.

Attributes

data

The underlying NumPy array (np.float64). Read/write.

grad

Gradient array of the same shape as data. None if requires_grad=False. Initialized to zeros when requires_grad=True.

requires_grad

Whether this tensor participates in gradient computation. Read-only after creation.

shape

Tuple — the shape of the underlying data array.

name

Optional string label.

Methods

backward(gradient=None)

Computes gradients by reverse-mode autodiff.

ParameterTypeDefaultDescription
gradientarray-like or NoneNoneInitial gradient (defaults to ones with shape self.data.shape)

Topologically sorts the graph, seeds the gradient, then walks backward calling each node's _backward function.

zero_grad_all()

Resets .grad to zeros for all tensors in the computation graph.

relu()

Returns a new Tensor: max(0, x) element-wise. Gradient-aware.

sigmoid()

Returns a new Tensor: 1 / (1 + exp(-x)) element-wise. Gradient-aware.

softmax(axis=-1)

Returns a new Tensor with softmax applied along the specified axis. Shifts inputs by max for numerical stability before computing exponentials.

ParameterTypeDefaultDescription
axisint-1Axis along which to compute softmax

exp()

Returns a new Tensor: e^x element-wise.

log()

Returns a new Tensor: ln(x) element-wise (natural logarithm). Note: log(0) returns -inf, log(negative) returns nan.

sum(axis=None, keepdims=False)

Returns a new Tensor with the sum of all elements (full reduction, not per-axis).

mean(axis=None, keepdims=False)

Returns a new Tensor with the mean of all elements (full reduction, not per-axis).

matmul(other)

Matrix multiplication equivalent to self @ other. Returns a new Tensor.

Operators

OperatorMethodDescription
+__add__, __radd__Element-wise addition
-__sub__Element-wise subtraction
- (unary)__neg__Element-wise negation
*__mul__, __rmul__Element-wise multiplication
/__truediv__Element-wise division
**__pow__Element-wise power (only exponent 2)
@__matmul__Matrix multiplication

All operators support broadcasting following NumPy semantics.