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:
objectDense 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 aseazygrad.tensor()oreazygrad.from_numpy(), rather than calling _Tensor directly.See also
- 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
vectoris omitted, the tensor must be scalar and a gradient of 1 is used.See also
- 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.
- 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
matmulbroadcasting rules and requires both operands to be at least 1-dimensional.See also
- mean(dim: int | tuple[int, ...] | None = None, keepdims: bool = False) _Tensor[source]¶
Compute the mean of the tensor along one or more axes.
- Parameters:
- Returns:
_Tensor – Tensor containing the reduced mean.
See also
- numpy(force: bool = True) numpy.ndarray[source]¶
Return the tensor contents as a NumPy array copy.
- Parameters:
force (bool, default=True) – Compatibility argument. Only
Trueis 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
- 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
- sum(dim: int | tuple[int, ...] | None = None, keepdims: bool = False) _Tensor[source]¶
Compute the sum of the tensor along one or more axes.
- Parameters:
- Returns:
_Tensor – Tensor containing the reduced sum.
See also
- swapdims(dim1: int, dim2: int) _Tensor[source]¶
Swap two dimensions of the tensor.
- Parameters:
- Returns:
_Tensor – Tensor with the two dimensions exchanged.
See also