Tensor Object

The public tensor API is implemented by eazygrad._tensor._Tensor. Most users construct tensors through eazygrad.tensor() or eazygrad.from_numpy(), then interact with the returned object through the methods documented here.

class eazygrad._tensor._Tensor(array: Any, requires_grad: bool, dtype: Any = None)[source]

Bases: object

Dense tensor object used throughout EaZyGrad.

_Tensor plays the same role as torch.Tensor in PyTorch: it stores array data, tracks whether gradients are required, and records graph edges when differentiable operations are applied.

Notes

This class is intentionally lightweight and educational. Most users should construct tensors through the factory functions in eazygrad, such as eazygrad.tensor() or eazygrad.from_numpy(), rather than calling _Tensor directly.

T() _Tensor[source]

Swap the last two dimensions of the tensor.

Returns:

_Tensor – Tensor with the trailing two axes transposed.

backward(vector: np.ndarray | None = None, retain_graph: bool = False) None[source]

Backpropagate gradients from this tensor.

Parameters:
  • vector (numpy.ndarray, optional) – Gradient of a scalar objective with respect to this tensor. This is required when the tensor is not scalar.

  • retain_graph (bool, default=False) – Whether to keep traversed graph nodes after the backward pass.

Returns:

None

Notes

If vector is omitted, the tensor must be scalar and a gradient of 1 is used.

clear_grad() None[source]

Clear the stored gradient of the tensor.

Returns:

None

detach() _Tensor[source]

Return a tensor detached from the computation graph.

Notes

This method is currently not implemented because the present graph ownership model can leak memory when detached tensors are created.

double() _Tensor[source]

Cast the tensor to numpy.float64.

Returns:

_Tensor – Tensor cast to float64.

float() _Tensor[source]

Cast the tensor to numpy.float32.

Returns:

_Tensor – Tensor cast to float32.

int() _Tensor[source]

Cast the tensor to numpy.int32.

Returns:

_Tensor – Tensor cast to int32.

long() _Tensor[source]

Cast the tensor to numpy.int64.

Returns:

_Tensor – Tensor cast to int64.

matmul(other: _Tensor) _Tensor[source]

Matrix-multiply this tensor with another tensor.

Parameters:

other (_Tensor) – Right-hand side tensor.

Returns:

_Tensor – Result of the matrix multiplication.

Notes

This method follows NumPy’s matmul broadcasting rules and requires both operands to be at least 1-dimensional.

See also

torch.matmul

mean(dim: int | tuple[int, ...] | None = None, keepdims: bool = False) _Tensor[source]

Compute the mean of the tensor along one or more axes.

Parameters:
  • dim (int or tuple of int, optional) – Axis or axes to reduce. If omitted, all dimensions are reduced.

  • keepdims (bool, default=False) – Whether reduced dimensions are retained with size 1.

Returns:

_Tensor – Tensor containing the reduced mean.

See also

torch.mean

numpy(force: bool = True) numpy.ndarray[source]

Return the tensor contents as a NumPy array copy.

Parameters:

force (bool, default=True) – Compatibility argument. Only True is supported.

Returns:

numpy.ndarray – Copy of the underlying tensor data.

Notes

Unlike torch.Tensor.numpy(), EaZyGrad always returns a copy and does not expose shared storage back to NumPy.

plot_dag(full_graph: bool = False) None[source]

Render the computation graph rooted at this tensor.

Parameters:

full_graph (bool, default=False) – Whether to render the full global graph. Only the rooted subgraph is currently supported.

Returns:

None

reshape(*shape: int) _Tensor[source]

Return a reshaped view of the tensor.

Parameters:

*shape (int) – Target shape. At most one dimension may be -1.

Returns:

_Tensor – Reshaped tensor view.

Notes

The returned tensor shares storage with the input whenever NumPy can provide a view.

See also

torch.reshape

property shape: tuple[int, ...]

Shape of the underlying tensor array.

Type:

tuple of int

squeeze(*dim: int) _Tensor[source]

Remove singleton dimensions from the tensor.

Parameters:

*dim (int, optional) – Specific singleton dimensions to remove. If omitted, all singleton dimensions are removed.

Returns:

_Tensor – Tensor with squeezed dimensionality.

See also

torch.squeeze

sum(dim: int | tuple[int, ...] | None = None, keepdims: bool = False) _Tensor[source]

Compute the sum of the tensor along one or more axes.

Parameters:
  • dim (int or tuple of int, optional) – Axis or axes to reduce. If omitted, all dimensions are reduced.

  • keepdims (bool, default=False) – Whether reduced dimensions are retained with size 1.

Returns:

_Tensor – Tensor containing the reduced sum.

See also

torch.sum

swapdims(dim1: int, dim2: int) _Tensor[source]

Swap two dimensions of the tensor.

Parameters:
  • dim1 (int) – First dimension.

  • dim2 (int) – Second dimension.

Returns:

_Tensor – Tensor with the two dimensions exchanged.

See also

torch.swapdims

to(dtype: Any) _Tensor[source]

Cast the tensor to a supported dtype.

Parameters:

dtype (numpy.dtype or type) – Target dtype. Supported values are numpy.float32, numpy.float64, numpy.int32, and numpy.int64.

Returns:

_Tensor – Tensor cast to the requested dtype.

See also

torch.Tensor.to

unsqueeze(*dim: int) _Tensor[source]

Insert one or more singleton dimensions.

Parameters:

*dim (int) – Positions where singleton dimensions are inserted. If omitted, a singleton dimension is inserted at axis 0.

Returns:

_Tensor – Tensor with expanded dimensionality.

See also

torch.unsqueeze