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(Optional[bool], default:None) –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
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 | |
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:
cuda ¶
clamp ¶
Clamps the tensor elements and returns a new Tensor.
Parameters:
-
min(Optional[Union[float, int]], default:None) –Lower-bound of the range to clamp to.
-
max(Optional[Union[float, int]], default:None) –Upper-bound of the range to clamp to.
Source code in sorix/tensor.py
clamp_ ¶
Clamps the tensor elements in-place and returns the tensor itself.
Parameters:
-
min(Optional[Union[float, int]], default:None) –Lower-bound of the range to clamp to.
-
max(Optional[Union[float, int]], default:None) –Upper-bound of the range to clamp to.
Source code in sorix/tensor.py
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: