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 ¶
div_ ¶
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
add_ ¶
In-place addition. Mutates self and returns it.
Parameters:
Raises:
-
RuntimeError–If
self.requires_gradisTrueand grad tracking is enabled (would corrupt the autograd graph).
Source code in sorix/tensor.py
sub_ ¶
In-place subtraction. Mutates self and returns it.
Raises:
-
RuntimeError–If in-place would corrupt the autograd graph.
Source code in sorix/tensor.py
mul_ ¶
In-place element-wise multiplication. Mutates self and returns it.
Raises:
-
RuntimeError–If in-place would corrupt the autograd graph.
Source code in sorix/tensor.py
fill_ ¶
Fills self with value in-place and returns it.
Raises:
-
RuntimeError–If in-place would corrupt the autograd graph.
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
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 | |
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 ¶
tensor ¶
Factory function to create a Sorix Tensor.
Examples: