pub struct Tensor<B>where
B: Backend,{ /* private fields */ }
Expand description
Tensor is the core object of zyx. It is multidimensional array.
Implementations§
source§impl<B> Tensor<B>where
B: Backend,
impl<B> Tensor<B>where
B: Backend,
sourcepub fn id(&self) -> Id
pub fn id(&self) -> Id
Tensor’s unique identification. All tensors on one backend will always have different ids.
sourcepub fn shape(&self) -> Shape
pub fn shape(&self) -> Shape
Returns the shape of the self tensor.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 1], [4, 1, 3]]);
assert_eq!(x.shape(), [2, 3]);
sourcepub fn numel(&self) -> usize
pub fn numel(&self) -> usize
Returns number of elements in the self tensor.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 1], [4, 1, 3]]);
assert_eq!(x.numel(), 6);
sourcepub fn dtype(&self) -> DType
pub fn dtype(&self) -> DType
Returns the dtype of the self tensor.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 1], [4, 1, 3]]);
assert_eq!(x.dtype(), zyx_opencl::DType::I32);
sourcepub fn rank(&self) -> usize
pub fn rank(&self) -> usize
Returns the rank of the self tensor. This is the number of tensor’s dimensions.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 1], [4, 1, 3]]);
assert_eq!(x.rank(), 2);
sourcepub fn backend(&self) -> B
pub fn backend(&self) -> B
Returns the backend of the self tensor.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 1], [4, 1, 3]]);
let y = x.backend().randn([2, 4, 3], zyx_opencl::DType::F32);
sourcepub fn detach(&self) -> Tensor<B>
pub fn detach(&self) -> Tensor<B>
Detach gradient tape from tensor. This means that resulting tensor is a shallow copy of self, but it’s gradient will be ones. Result of this operation can only be differentiated by itself.
let dev = zyx_opencl::device()?;
let x = dev.tensor([2, 3]);
let y = &x + &x;
let g = y.backward([&x]).pop().unwrap().unwrap();
assert_eq!(g, [2, 2]);
let z = y.detach();
let g = z.backward([&z]).pop().unwrap().unwrap();
assert_eq!(g, [1, 1]);
let g = z.backward([&x]).pop().unwrap();
assert_eq!(g, None);
sourcepub fn to_vec<T>(&self) -> Result<Vec<T>, ZyxError>where
T: Scalar,
pub fn to_vec<T>(&self) -> Result<Vec<T>, ZyxError>where
T: Scalar,
Load tensor from backend into vector
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 1], [4, 1, 3]]);
let xvec: Vec<i32> = x.to_vec()?;
assert_eq!(xvec, vec![2, 3, 1, 4, 1, 3]);
sourcepub fn item<T>(&self) -> Result<T, ZyxError>where
T: Scalar,
pub fn item<T>(&self) -> Result<T, ZyxError>where
T: Scalar,
Returns first element stored in this tensor. Usually used for tensors with exactly one element. Error is returned if self tensor contains zero elements or if backend returns error.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 1], [4, 1, 3]]);
let xitem: i32 = x.item()?;
assert_eq!(xitem, 2);
sourcepub fn backward<'a>(
&'a self,
sources: impl IntoIterator<Item = &'a Tensor<B>>
) -> Vec<Option<Tensor<B>>>where
B: 'a,
pub fn backward<'a>(
&'a self,
sources: impl IntoIterator<Item = &'a Tensor<B>>
) -> Vec<Option<Tensor<B>>>where
B: 'a,
Returns gradients of self w.r.t. sources.
let dev = zyx_opencl::device()?;
let x = dev.tensor([3., 2., 1.]);
let y = x.exp() + &x;
let x_grad = y.backward([&x]).into_iter().next().unwrap().unwrap();
assert_eq!(x_grad, [21.0855369, 8.3890561, 3.7182818]);
sourcepub fn cast(&self, dtype: DType) -> Tensor<B>
pub fn cast(&self, dtype: DType) -> Tensor<B>
Cast self into dtype.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[3, 4, 2], [4, 5, 2]]);
let y = x.cast(DType::F32);
assert_eq!(y.dtype(), DType::F32);
assert_eq!(y, [[3f32, 4., 2.], [4., 5., 2.]]);
sourcepub fn relu(&self) -> Tensor<B>
pub fn relu(&self) -> Tensor<B>
Returns a new tensor with the rectified linear unit function applied to the elements of self.
sourcepub fn ln(&self) -> Tensor<B>
pub fn ln(&self) -> Tensor<B>
Returns a new tensor with the natural logarithm of the elements of self. Due to performance reasons, this function does not check if self fits into domain of ln(x). Result on out of domain numbers is implementation defined (when x <= 0).
sourcepub fn exp(&self) -> Tensor<B>
pub fn exp(&self) -> Tensor<B>
Returns a new tensor with the exponential of the elements of self.
sourcepub fn tanh(&self) -> Tensor<B>
pub fn tanh(&self) -> Tensor<B>
Returns a new tensor with the hyperbolic tangent of the elements of self.
sourcepub fn sqrt(&self) -> Tensor<B>
pub fn sqrt(&self) -> Tensor<B>
Returns a new tensor with the square root of the elements of self. Due to performance reasons, this function does not check if self fits into domain of ln(x). Result on out of domain numbers is implementation defined (when x < 0).
sourcepub fn reciprocal(&self) -> Tensor<B>
pub fn reciprocal(&self) -> Tensor<B>
Returns 1/self
sourcepub fn dropout(&self, probability: impl Scalar) -> Tensor<B>
pub fn dropout(&self, probability: impl Scalar) -> Tensor<B>
Returns a new tensor with each element of self randomly zeroed with given probability.
sourcepub fn abs(&self) -> Tensor<B>
pub fn abs(&self) -> Tensor<B>
Returns a new tensor with the absolute value of the elements of self.
sourcepub fn sigmoid(&self) -> Tensor<B>
pub fn sigmoid(&self) -> Tensor<B>
Returns a new tensor with the sigmoid (logistic function) of the elements of self.
sourcepub fn swish(&self) -> Tensor<B>
pub fn swish(&self) -> Tensor<B>
Returns a new tensor with the swish/silu of the elements of self.
sourcepub fn softplus(&self, beta: impl Scalar, threshold: impl Scalar) -> Tensor<B>
pub fn softplus(&self, beta: impl Scalar, threshold: impl Scalar) -> Tensor<B>
Returns a new tensor with the softplus of the elements of self.
sourcepub fn leaky_relu(&self, neg_slope: impl Scalar) -> Tensor<B>
pub fn leaky_relu(&self, neg_slope: impl Scalar) -> Tensor<B>
Returns a new tensor with the leaky relu of the elements of self.
sourcepub fn elu(&self, alpha: impl Scalar) -> Tensor<B>
pub fn elu(&self, alpha: impl Scalar) -> Tensor<B>
Returns a new tensor with the elu of the elements of self.
sourcepub fn celu(&self, alpha: impl Scalar) -> Tensor<B>
pub fn celu(&self, alpha: impl Scalar) -> Tensor<B>
Returns a new tensor with the celu of the elements of self.
sourcepub fn quick_gelu(&self) -> Tensor<B>
pub fn quick_gelu(&self) -> Tensor<B>
Returns a new tensor with the quick gelu of the elements of self.
sourcepub fn softmax(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn softmax(&self, axes: impl IntoAxes) -> Tensor<B>
Returns a new tensor with the softmax of the elements of self.
sourcepub fn ln_softmax(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn ln_softmax(&self, axes: impl IntoAxes) -> Tensor<B>
Returns a new tensor with the log softmax of the elements of self.
sourcepub fn l1_loss(&self, target: impl IntoTensor<B>) -> Tensor<B>
pub fn l1_loss(&self, target: impl IntoTensor<B>) -> Tensor<B>
Measures the mean absolute error (MAE) between each element in the input self and target.
sourcepub fn mse_loss(&self, target: impl IntoTensor<B>) -> Tensor<B>
pub fn mse_loss(&self, target: impl IntoTensor<B>) -> Tensor<B>
Measures the mean squared error (MSE) between each element in the input self and target.
sourcepub fn cross_entropy_loss(
&self,
target: impl IntoTensor<B>,
axes: impl IntoAxes
) -> Tensor<B>
pub fn cross_entropy_loss( &self, target: impl IntoTensor<B>, axes: impl IntoAxes ) -> Tensor<B>
Computes the cross entropy loss between self logits and target. This function expects self to contain probabilities for each class.
sourcepub fn pow(&self, exponent: impl IntoTensor<B>) -> Tensor<B>
pub fn pow(&self, exponent: impl IntoTensor<B>) -> Tensor<B>
Exponentiation on self
sourcepub fn cmplt(&self, rhs: impl IntoTensor<B>) -> Tensor<B>
pub fn cmplt(&self, rhs: impl IntoTensor<B>) -> Tensor<B>
Elementwise compare less than between self and rhs
sourcepub fn where_(
&self,
if_true: impl IntoTensor<B>,
if_false: impl IntoTensor<B>
) -> Tensor<B>
pub fn where_( &self, if_true: impl IntoTensor<B>, if_false: impl IntoTensor<B> ) -> Tensor<B>
Returns a new tensor with the true values replaced with if_true and the false values replaced with if_false.
sourcepub fn cosine_similarity(
&self,
rhs: impl IntoTensor<B>,
eps: impl IntoTensor<B>
) -> Tensor<B>
pub fn cosine_similarity( &self, rhs: impl IntoTensor<B>, eps: impl IntoTensor<B> ) -> Tensor<B>
Returns cosine_similarity between self and rhs, computed along axes.
sourcepub fn dot(&self, rhs: impl IntoTensor<B>) -> Tensor<B>
pub fn dot(&self, rhs: impl IntoTensor<B>) -> Tensor<B>
Dot product (mathematical multiplication) of self and rhs.
let dev = zyx_opencl::device()?;
let x = dev.tensor([[3, 4, 2], [4, 5, 2]]);
let y = dev.tensor([[3], [1], [4]]);
assert_eq!(x.dot(y), [[21], [25]]);
sourcepub fn pad(
&self,
padding: impl IntoIterator<Item = (i64, i64)>,
value: impl IntoTensor<B>
) -> Tensor<B>
pub fn pad( &self, padding: impl IntoIterator<Item = (i64, i64)>, value: impl IntoTensor<B> ) -> Tensor<B>
Constant padding
This can both add and remove values from tensor. Negative padding removes values, positive padding adds values.
Pad last dimension by (1, 2)
use zyx_opencl;
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3],
[4, 1]]);
let z = x.pad([(1, 2)], 0);
std::println!("{}", z);
assert_eq!(z, [[0, 2, 3, 0, 0],
[0, 4, 1, 0, 0]]);
Pad last dimension by (2, -1) and second last dimension by (1, 1)
let z = x.pad([(2, -1), (1, 1)], 0);
println!("z: {z}");
assert_eq!(z, [[0, 0, 0],
[0, 0, 2],
[0, 0, 4],
[0, 0, 0]]);
§Panics
T must be of the same dtype as Tensor’s dtype, otherwise this function panics.
sourcepub fn transpose(&self) -> Tensor<B>
pub fn transpose(&self) -> Tensor<B>
Swap last two axes of self. If self has rank == 1 and numel == n, then result will have shape /[n, 1/]
sourcepub fn flatten(&self, axes: impl FlattenAxes) -> Tensor<B>
pub fn flatten(&self, axes: impl FlattenAxes) -> Tensor<B>
Flatten. Joins axes into one dimension,
sourcepub fn sum(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn sum(&self, axes: impl IntoAxes) -> Tensor<B>
Reduce self by summing along axes. Shape is not squeezed.
use zyx_opencl;
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3], [4, 1]]);
let z = x.sum(-1);
assert_eq!(z.shape(), [2, 1]);
let z = x.sum(0);
assert_eq!(z.shape(), [1, 2]);
let z = x.sum(..);
assert_eq!(z.shape(), [1, 1]);
sourcepub fn max(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn max(&self, axes: impl IntoAxes) -> Tensor<B>
Reduce self by maximizing along axes. Shape is not squeezed.
use zyx_opencl;
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3], [4, 1]]);
let z = x.max(-1);
assert_eq!(z.shape(), [2, 1]);
let z = x.max(0);
assert_eq!(z.shape(), [1, 2]);
let z = x.max(..);
assert_eq!(z.shape(), [1, 1]);
sourcepub fn mean(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn mean(&self, axes: impl IntoAxes) -> Tensor<B>
Reduce self by calculating mean along axes
sourcepub fn var(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn var(&self, axes: impl IntoAxes) -> Tensor<B>
Reduce self by calculating variance along axes
sourcepub fn std(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn std(&self, axes: impl IntoAxes) -> Tensor<B>
Reduce self by calculating standard deviation along axes
sourcepub fn norm(&self, axes: impl IntoAxes, p: impl Scalar) -> Tensor<B>
pub fn norm(&self, axes: impl IntoAxes, p: impl Scalar) -> Tensor<B>
Reduce self by calculating norm along axes
sourcepub fn product(&self, axes: impl IntoAxes) -> Tensor<B>
pub fn product(&self, axes: impl IntoAxes) -> Tensor<B>
Reduce self by calculating product of elements along axes
sourcepub fn get(&self, index: impl IntoIndex) -> Tensor<B>
pub fn get(&self, index: impl IntoIndex) -> Tensor<B>
Tensor indexing.
Tensors can be indexed by tuples of any combination of values or ranges of i64. If indexing along more than 8 dimensions, use &[Range<i64>] or &[i64]
use zyx_opencl;
let dev = zyx_opencl::device()?;
let x = dev.tensor([[2, 3, 4],
[4, 1, 8]]);
let y: i32 = x.get((-1, -3)).item()?;
assert_eq!(y, 4);