tensor
sorix.tensor ¶
Device ¶
Represents a computing device in Sorix, matching PyTorch's torch.device.
Source code in sorix/tensor.py
Size ¶
Bases: tuple
A tuple subclass that represents the shape of a Tensor, matching PyTorch's torch.Size.
DType ¶
no_grad ¶
Tensor ¶
Initializes a new Tensor.
Parameters:
-
data(TensorData) –Numerical data (numpy array, list, scalar, etc.).
-
device(str, default:'cpu') –Computing device ('cpu' or 'cuda').
-
requires_grad(bool, default:False) –Whether to track gradients for this tensor.
-
dtype(Any, default:None) –Data type for the tensor elements.
Source code in sorix/tensor.py
to ¶
Moves the tensor to the specified device.
Parameters:
-
device(Union[str, Device]) –'cpu', 'cuda', 'cuda:0', etc.
Source code in sorix/tensor.py
cpu ¶
gpu ¶
add_ ¶
sub_ ¶
mul_ ¶
div_ ¶
fill_ ¶
zero_ ¶
add ¶
Element-wise addition.
Parameters:
Returns:
-
Tensor–A new tensor with the sum.
Examples:
Source code in sorix/tensor.py
sub ¶
Element-wise subtraction.
Parameters:
Returns:
-
Tensor–A new tensor with the result.
Examples:
Source code in sorix/tensor.py
mul ¶
Element-wise multiplication.
Parameters:
Returns:
-
Tensor–A new tensor with the product.
Examples:
Source code in sorix/tensor.py
matmul ¶
Matrix multiplication.
Parameters:
-
other(Union[Tensor, ndarray]) –The tensor or array to multiply by.
Returns:
-
Tensor–A new tensor with the matrix product.
Examples:
Source code in sorix/tensor.py
tanh ¶
Hyperbolic tangent activation.
Source code in sorix/tensor.py
pow ¶
Raises tensor to the power of n.
Source code in sorix/tensor.py
sigmoid ¶
Sigmoid activation.
Source code in sorix/tensor.py
softmax ¶
Softmax activation along an axis/dim.
Source code in sorix/tensor.py
div ¶
Element-wise division.
Source code in sorix/tensor.py
mean ¶
Computes mean along axis/dim.
Source code in sorix/tensor.py
sum ¶
Computes sum along axis/dim.
Source code in sorix/tensor.py
abs ¶
reshape ¶
Reshapes the tensor to a new shape.
Source code in sorix/tensor.py
view ¶
transpose ¶
Transposes the tensor axes.
Source code in sorix/tensor.py
flatten ¶
expand_dims ¶
Adds a new dimension at the specified axis. Matches np.expand_dims.
Source code in sorix/tensor.py
unsqueeze ¶
squeeze ¶
Removes dimensions of size 1.
Source code in sorix/tensor.py
permute ¶
repeat ¶
Repeats the tensor along the specified dimensions.
Source code in sorix/tensor.py
unbind ¶
Removes a tensor dimension. Returns a tuple of all slices along that dimension.
Source code in sorix/tensor.py
split ¶
Splits the tensor into chunks.
Source code in sorix/tensor.py
chunk ¶
Splits a tensor into a specific number of chunks.
backward ¶
Computes the gradient of current tensor w.r.t. graph leaves.
The graph is traversed in reverse topological order to propagate gradients. If the tensor is non-scalar, a gradient must be provided.
Parameters:
-
gradient(Optional[Union[Tensor, ndarray, Any]], default:None) –The gradient of this tensor, usually the dL/d(this_tensor). Must match the shape of this tensor.
-
retain_graph(bool, default:True) –If False, the graph used to compute the grads will be freed.
-
create_graph(bool, default:False) –If True, graph of the gradient will be constructed, allowing to compute higher-order derivative products.
Source code in sorix/tensor.py
astype ¶
Casts tensor to a new data type.
float ¶
double ¶
half ¶
int ¶
long ¶
bool ¶
detach ¶
size ¶
Returns the size of the tensor, matching PyTorch's .size() method.
dim ¶
t ¶
Expects self to be <= 2-D tensor and transposes dimensions 0 and 1.
numpy ¶
Returns the data as a NumPy array.
If the tensor is on the GPU, it will be copied to the host.
Returns:
-
ndarray–The numerical data as a NumPy ndarray.
Source code in sorix/tensor.py
item ¶
Returns the scalar value of a 1-element tensor.
Examples:
set_grad_enabled ¶
is_grad_enabled ¶
get_xp ¶
Returns the appropriate array module (numpy or cupy) for the given arguments.
tensor ¶
Factory function to create a Sorix Tensor.
Examples: