Struct burn_tensor::Tensor
source · Expand description
A tensor or a n-dimensional array.
Implementations§
source§impl<B> Tensor<B, 1>where
B: Backend,
impl<B> Tensor<B, 1>where
B: Backend,
sourcepub fn arange(range: Range<usize>) -> Tensor<B::IntegerBackend, 1>
pub fn arange(range: Range<usize>) -> Tensor<B::IntegerBackend, 1>
Returns a new integer tensor on the default device which values are generated from the given range.
sourcepub fn arange_device(
range: Range<usize>,
device: B::Device
) -> Tensor<B::IntegerBackend, 1>
pub fn arange_device(
range: Range<usize>,
device: B::Device
) -> Tensor<B::IntegerBackend, 1>
Returns a new integer tensor on the specified device which values are generated from the given range.
source§impl<B> Tensor<B, 1>where
B: Backend,
impl<B> Tensor<B, 1>where
B: Backend,
sourcepub fn single_value(&self) -> B::Elem
pub fn single_value(&self) -> B::Elem
Returns the first value of the tensor.
source§impl<const D: usize, B> Tensor<B, D>where
B: Backend,
impl<const D: usize, B> Tensor<B, D>where
B: Backend,
pub fn from_primitive(tensor: B::TensorPrimitive<D>) -> Self
pub fn into_primitive(self) -> B::TensorPrimitive<D>
sourcepub fn reshape<const D2: usize, S: Into<Shape<D2>>>(
&self,
shape: S
) -> Tensor<B, D2>
pub fn reshape<const D2: usize, S: Into<Shape<D2>>>(
&self,
shape: S
) -> Tensor<B, D2>
Reshape the tensor to have the given shape.
Panics
If the tensor can not be reshape to the given shape.
sourcepub fn erf(&self) -> Self
pub fn erf(&self) -> Self
Applies the error function element wise.
y = erf(x)
sourcepub fn dims(&self) -> [usize; D]
pub fn dims(&self) -> [usize; D]
Returns the dimensions of the current tensor.
Equivalent to tensor.shape().dims.
sourcepub fn to_data(&self) -> Data<B::Elem, D>
pub fn to_data(&self) -> Data<B::Elem, D>
Returns the data of the current tensor without taking ownership.
sourcepub fn from_data_device(data: Data<B::Elem, D>, device: B::Device) -> Self
pub fn from_data_device(data: Data<B::Elem, D>, device: B::Device) -> Self
Create a tensor from the given data on the given device.
sourcepub fn from_floats<A: Into<Data<f32, D>>>(floats: A) -> Self
pub fn from_floats<A: Into<Data<f32, D>>>(floats: A) -> Self
Create a tensor from floats (f32).
Example
use burn_tensor::backend::Backend;
use burn_tensor::Tensor;
fn example<B: Backend>() {
let _ = Tensor::<B, 1>::from_floats([1.0, 2.0]);
let _ = Tensor::<B, 2>::from_floats([[1.0, 2.0], [3.0, 4.0]]);
}sourcepub fn zeros_like(&self) -> Self
pub fn zeros_like(&self) -> Self
Returns a new tensor with the same shape and device as the current tensor filled with zeros.
sourcepub fn ones_like(&self) -> Self
pub fn ones_like(&self) -> Self
Returns a new tensor with the same shape and device as the current tensor filled with ones.
sourcepub fn random_like(&self, distribution: Distribution<B::Elem>) -> Self
pub fn random_like(&self, distribution: Distribution<B::Elem>) -> Self
Returns a new tensor with the same shape and device as the current tensor filled random values sampled from the given distribution.
sourcepub fn one_hot(index: usize, num_classes: usize) -> Self
pub fn one_hot(index: usize, num_classes: usize) -> Self
Create a one hot tensor.
Example
use burn_tensor::backend::Backend;
use burn_tensor::Tensor;
fn example<B: Backend>() {
let one_hot = Tensor::<B, 1>::one_hot(2, 10);
println!("{}", one_hot.to_data());
// [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
}sourcepub fn add_scalar<E: ElementConversion>(&self, other: E) -> Self
pub fn add_scalar<E: ElementConversion>(&self, other: E) -> Self
Applies element wise addition operation with a scalar.
y = x + s
sourcepub fn sub(&self, other: &Self) -> Self
pub fn sub(&self, other: &Self) -> Self
Applies element wise substraction operation.
y = x2 - x1
sourcepub fn sub_scalar<E: ElementConversion>(&self, other: E) -> Self
pub fn sub_scalar<E: ElementConversion>(&self, other: E) -> Self
Applies element wise substraction operation with a scalar.
y = x - s
sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Applies the transpose operation.
On matrix and higher dimension tensor, it swap the last two dimensions.
Panics
If the tensor is of 1 dimension or less.
sourcepub fn matmul(&self, other: &Self) -> Self
pub fn matmul(&self, other: &Self) -> Self
Applies the matrix multiplication operation.
C = AB
Panics
If the two tensors dont’ have a compatible shape.
sourcepub fn mul(&self, other: &Self) -> Self
pub fn mul(&self, other: &Self) -> Self
Applies element wise multiplication operation.
y = x2 * x1
sourcepub fn mul_scalar<E: ElementConversion>(&self, other: E) -> Self
pub fn mul_scalar<E: ElementConversion>(&self, other: E) -> Self
Applies element wise multiplication operation with scalar.
y = x2 * x1
sourcepub fn div_scalar<E: ElementConversion>(&self, other: E) -> Self
pub fn div_scalar<E: ElementConversion>(&self, other: E) -> Self
Applies element wise division operation with scalar.
y = x2 / x1
sourcepub fn mean(&self) -> Tensor<B, 1>
pub fn mean(&self) -> Tensor<B, 1>
Aggregate all elements in the tensor with the mean operation.
sourcepub fn mean_dim(&self, dim: usize) -> Self
pub fn mean_dim(&self, dim: usize) -> Self
Aggregate all elements along the given dimension or axis in the tensor with the mean operation.
sourcepub fn sum_dim(&self, dim: usize) -> Self
pub fn sum_dim(&self, dim: usize) -> Self
Aggregate all elements along the given dimension or axis in the tensor with the sum operation.
sourcepub fn var_bias(&self, dim: usize) -> Self
pub fn var_bias(&self, dim: usize) -> Self
Calculate the variance along the given dimension without applying the Bessel’s correction.
sourcepub fn var_mean(&self, dim: usize) -> (Self, Self)
pub fn var_mean(&self, dim: usize) -> (Self, Self)
Calculate the variance along the given dimension and also returns the mean.
sourcepub fn var_mean_bias(&self, dim: usize) -> (Self, Self)
pub fn var_mean_bias(&self, dim: usize) -> (Self, Self)
Calculate the variance along the given dimension without applying the Bessel’s correction and also returns the mean.
sourcepub fn equal(&self, other: &Self) -> BoolTensor<B, D>
pub fn equal(&self, other: &Self) -> BoolTensor<B, D>
Applies element wise equal comparison and returns a boolean tensor.
Panics
If the two tensors don’t have the same shape.
sourcepub fn greater(&self, other: &Self) -> BoolTensor<B, D>
pub fn greater(&self, other: &Self) -> BoolTensor<B, D>
Applies element wise greater comparison and returns a boolean tensor.
Panics
If the two tensors don’t have the same shape.
sourcepub fn greater_equal(&self, other: &Self) -> BoolTensor<B, D>
pub fn greater_equal(&self, other: &Self) -> BoolTensor<B, D>
Applies element wise greater-equal comparison and returns a boolean tensor.
Panics
If the two tensors don’t have the same shape.
sourcepub fn lower(&self, other: &Self) -> BoolTensor<B, D>
pub fn lower(&self, other: &Self) -> BoolTensor<B, D>
Applies element wise lower comparison and returns a boolean tensor.
Panics
If the two tensors don’t have the same shape.
sourcepub fn lower_equal(&self, other: &Self) -> BoolTensor<B, D>
pub fn lower_equal(&self, other: &Self) -> BoolTensor<B, D>
Applies element wise lower-equal comparison and returns a boolean tensor.
Panics
If the two tensors don’t have the same shape.
sourcepub fn equal_scalar<E: ElementConversion>(&self, other: E) -> BoolTensor<B, D>
pub fn equal_scalar<E: ElementConversion>(&self, other: E) -> BoolTensor<B, D>
Applies element wise equal comparison and returns a boolean tensor.
sourcepub fn greater_scalar<E: ElementConversion>(&self, other: E) -> BoolTensor<B, D>
pub fn greater_scalar<E: ElementConversion>(&self, other: E) -> BoolTensor<B, D>
Applies element wise greater comparison and returns a boolean tensor.
sourcepub fn greater_equal_scalar<E: ElementConversion>(
&self,
other: E
) -> BoolTensor<B, D>
pub fn greater_equal_scalar<E: ElementConversion>(
&self,
other: E
) -> BoolTensor<B, D>
Applies element wise greater-equal comparison and returns a boolean tensor.
sourcepub fn lower_scalar<E: ElementConversion>(&self, other: E) -> BoolTensor<B, D>
pub fn lower_scalar<E: ElementConversion>(&self, other: E) -> BoolTensor<B, D>
Applies element wise lower comparison and returns a boolean tensor.
sourcepub fn lower_equal_scalar<E: ElementConversion>(
&self,
other: E
) -> BoolTensor<B, D>
pub fn lower_equal_scalar<E: ElementConversion>(
&self,
other: E
) -> BoolTensor<B, D>
Applies element wise lower-equal comparison and returns a boolean tensor.
sourcepub fn random<S: Into<Shape<D>>>(
shape: S,
distribution: Distribution<B::Elem>
) -> Self
pub fn random<S: Into<Shape<D>>>(
shape: S,
distribution: Distribution<B::Elem>
) -> Self
Create a random tensor of the given shape where each element is sampled from the given distribution.
sourcepub fn zeros<S: Into<Shape<D>>>(shape: S) -> Self
pub fn zeros<S: Into<Shape<D>>>(shape: S) -> Self
Create a tensor of the given shape where each element is zero.
sourcepub fn zeros_device<S: Into<Shape<D>>>(shape: S, device: B::Device) -> Self
pub fn zeros_device<S: Into<Shape<D>>>(shape: S, device: B::Device) -> Self
Create a tensor of the given shape where each element is zero.
sourcepub fn ones<S: Into<Shape<D>>>(shape: S) -> Self
pub fn ones<S: Into<Shape<D>>>(shape: S) -> Self
Create a tensor of the given shape where each element is one.
sourcepub fn ones_device<S: Into<Shape<D>>>(shape: S, device: B::Device) -> Self
pub fn ones_device<S: Into<Shape<D>>>(shape: S, device: B::Device) -> Self
Create a tensor of the given shape where each element is one.
sourcepub fn index<const D2: usize>(&self, indexes: [Range<usize>; D2]) -> Self
pub fn index<const D2: usize>(&self, indexes: [Range<usize>; D2]) -> Self
Returns a tensor containing the elements selected from the given ranges.
Panics
If a range exceeds the number of elements on a dimension.
Example
use burn_tensor::backend::Backend;
use burn_tensor::{Tensor, Shape};
fn example<B: Backend>() {
let tensor = Tensor::<B, 3>::ones(Shape::new([2, 3, 3]));
let tensor_indexed = tensor.index([0..1, 0..3, 1..2]);
println!("{:?}", tensor_indexed.shape());
// Shape { dims: [1, 3, 2] }
}sourcepub fn index_assign<const D2: usize>(
&self,
indexes: [Range<usize>; D2],
values: &Self
) -> Self
pub fn index_assign<const D2: usize>(
&self,
indexes: [Range<usize>; D2],
values: &Self
) -> Self
Returns a copy of the current tensor with the selected elements changed to the new ones at the selected indexes.
Panics
- If a range exceeds the number of elements on a dimension.
- If the given values don’t match the given ranges.
Example
use burn_tensor::backend::Backend;
use burn_tensor::{Tensor, Shape};
fn example<B: Backend>() {
let tensor = Tensor::<B, 3>::ones(Shape::new([2, 3, 3]));
let values = Tensor::<B, 3>::zeros(Shape::new([1, 1, 1]));
let tensor_indexed = tensor.index_assign([0..1, 0..1, 0..1], &values);
println!("{:?}", tensor_indexed.shape());
// Shape { dims: [2, 3, 3] }
}sourcepub fn mask_fill<E: ElementConversion>(
&self,
mask: &BoolTensor<B, D>,
value: E
) -> Self
pub fn mask_fill<E: ElementConversion>(
&self,
mask: &BoolTensor<B, D>,
value: E
) -> Self
Fill each element with the given value based on the given mask.
sourcepub fn to_full_precision(&self) -> Tensor<B::FullPrecisionBackend, D>
pub fn to_full_precision(&self) -> Tensor<B::FullPrecisionBackend, D>
Returns a tensor with full precision based on the selected backend.
sourcepub fn from_full_precision(tensor: Tensor<B::FullPrecisionBackend, D>) -> Self
pub fn from_full_precision(tensor: Tensor<B::FullPrecisionBackend, D>) -> Self
Returns a tensor on the selected backend from a full precision tensor.
sourcepub fn argmax(&self, dim: usize) -> Tensor<B::IntegerBackend, D>
pub fn argmax(&self, dim: usize) -> Tensor<B::IntegerBackend, D>
Applies the argmax function along the given dimension and returns an integer tensor.
Example
use burn_tensor::backend::Backend;
use burn_tensor::{Tensor, Shape};
fn example<B: Backend>() {
let tensor = Tensor::<B, 3>::ones(Shape::new([2, 3, 3]));
let tensor = tensor.argmax(1);
println!("{:?}", tensor.shape());
// Shape { dims: [2, 1, 3] }
}sourcepub fn argmin(&self, dim: usize) -> Tensor<B::IntegerBackend, D>
pub fn argmin(&self, dim: usize) -> Tensor<B::IntegerBackend, D>
Applies the argmin function along the given dimension and returns an integer tensor.
Example
use burn_tensor::backend::Backend;
use burn_tensor::{Tensor, Shape};
fn example<B: Backend>() {
let tensor = Tensor::<B, 3>::ones(Shape::new([2, 3, 3]));
let tensor = tensor.argmin(1);
println!("{:?}", tensor.shape());
// Shape { dims: [2, 1, 3] }
}sourcepub fn cat(tensors: Vec<Self>, dim: usize) -> Self
pub fn cat(tensors: Vec<Self>, dim: usize) -> Self
Concatenates all tensors into a new one along the given dimension.
Panics
If all tensors don’t have the same shape.
sourcepub fn detach(self) -> Self
pub fn detach(self) -> Self
Detach the current tensor from the autodiff graph. This function does nothing when autodiff is not enabled. This can be used in batchers or elsewere to ensure that previous operations are not considered in the autodiff graph.
sourcepub fn unsqueeze<const D2: usize>(&self) -> Tensor<B, D2>
pub fn unsqueeze<const D2: usize>(&self) -> Tensor<B, D2>
Unsqueeze the current tensor. Create new dimensions to fit the given size.
Panics
If the output size is higher than the current tensor.
Example
use burn_tensor::backend::Backend;
use burn_tensor::{Tensor, Shape};
fn example<B: Backend>() {
let tensor = Tensor::<B, 2>::ones(Shape::new([3, 3]));
let tensor = tensor.unsqueeze::<4>();
println!("{:?}", tensor.shape());
// Shape { dims: [1, 1, 3, 3] }
}source§impl<const D: usize, B: ADBackend> Tensor<B, D>
impl<const D: usize, B: ADBackend> Tensor<B, D>
pub fn backward(&self) -> B::Gradients
pub fn grad(&self, grads: &B::Gradients) -> Option<Tensor<B::InnerBackend, D>>
pub fn inner(&self) -> Tensor<B::InnerBackend, D>
pub fn update(&mut self, other_inner: Tensor<B::InnerBackend, D>)
pub fn from_inner(inner: Tensor<B::InnerBackend, D>) -> Self
Trait Implementations§
source§impl<B: Clone + Backend, const D: usize> Clone for Tensor<B, D>where
B::TensorPrimitive<D>: Clone,
impl<B: Clone + Backend, const D: usize> Clone for Tensor<B, D>where
B::TensorPrimitive<D>: Clone,
source§impl<B: Debug + Backend, const D: usize> Debug for Tensor<B, D>where
B::TensorPrimitive<D>: Debug,
impl<B: Debug + Backend, const D: usize> Debug for Tensor<B, D>where
B::TensorPrimitive<D>: Debug,
Auto Trait Implementations§
impl<B, const D: usize> RefUnwindSafe for Tensor<B, D>where
<B as Backend>::TensorPrimitive<D>: RefUnwindSafe,
impl<B, const D: usize> Send for Tensor<B, D>
impl<B, const D: usize> Sync for Tensor<B, D>
impl<B, const D: usize> Unpin for Tensor<B, D>where
<B as Backend>::TensorPrimitive<D>: Unpin,
impl<B, const D: usize> UnwindSafe for Tensor<B, D>where
<B as Backend>::TensorPrimitive<D>: UnwindSafe,
Blanket Implementations§
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.