Overview
The Tensor class is the fundamental data structure in Neurenix, similar to tensors in PyTorch or TensorFlow. It provides a multi-dimensional array with automatic device management and gradient tracking.
Class Definition
Parameters
data Union[np.ndarray, List, Tuple, Tensor, None]
The data to initialize the tensor with. Can be a NumPy array, a list, a tuple, another Tensor, or None (for uninitialized tensor).
The shape of the tensor. If None, inferred from data.
dtype Optional[Union[DType, str]]
The data type of the tensor. If None, inferred from data. Options: ‘float32’, ‘float64’, ‘int32’, ‘int64’, ‘bool’.
The device to store the tensor on. If None, uses the default device.
Whether to track gradients for this tensor.
Properties
shape
Get the shape of the tensor.
The shape of the tensor as a tuple of integers.
dtype
Get the data type of the tensor.
device
Get the device where the tensor is stored.
requires_grad
Check if the tensor requires gradients.
grad
Get the gradient of the tensor.
Methods
numpy
Convert the tensor to a NumPy array. This operation will copy the tensor data from the device to the CPU if necessary.
A NumPy array with the tensor data.
Move the tensor to the specified device.
If True, the copy will be performed asynchronously. Only has an effect for CUDA/ROCm devices.
A new tensor on the target device.
hot_swap_device
Hot-swap the tensor to a different device without creating a new tensor. This method changes the device of the tensor in-place.
reshape
Reshape the tensor to the given shape.
The new shape of the tensor.
A new tensor with the given shape.
transpose
Transpose the tensor along the given dimensions.
matmul
Matrix multiplication with another tensor.
mean
Compute the mean along the specified dimension.
sum
Compute the sum along the specified dimension.
clone
Create a clone of this tensor.
backward
Compute gradients through the computation graph.
Activation Functions
relu
Apply the rectified linear unit function element-wise.
sigmoid
Apply the sigmoid function element-wise.
tanh
Apply the hyperbolic tangent function element-wise.
softmax
Apply the softmax function along the specified dimension.
log_softmax
Apply the log softmax function along the specified dimension.
leaky_relu
Apply the leaky rectified linear unit function element-wise.
gelu
Apply the Gaussian error linear unit function element-wise.
Static Methods
zeros
Create a tensor filled with zeros.
ones
Create a tensor filled with ones.
randn
Create a tensor filled with random numbers from a normal distribution.
stack
Stack tensors along a new dimension.
cat
Concatenate tensors along an existing dimension.
no_grad
Context manager to disable gradient computation.
Example Usage