Struct burn_tensor::Tensor

source ·
pub struct Tensor<B: Backend, const D: usize> { /* private fields */ }
Expand description

A tensor or a n-dimensional array.

Implementations§

Returns a new integer tensor on the default device which values are generated from the given range.

Returns a new integer tensor on the specified device which values are generated from the given range.

Returns the first value of the tensor.

Reshape the tensor to have the given shape.

Panics

If the tensor can not be reshape to the given shape.

Returns a new tensor on the given device.

Returns the device of the current tensor.

Applies element wise exponential operation.

y = e^x

Applies element wise natural log operation ln.

y = log(x)

Applies the error function element wise.

y = erf(x)

Applies element wise power operation.

y = x^a

Applies element wise root square operation.

Returns the shape of the current tensor.

Returns the dimensions of the current tensor.

Equivalent to tensor.shape().dims.

Returns the data of the current tensor.

Returns the data of the current tensor without taking ownership.

Create a tensor from the given data.

Create a tensor from the given data on the given device.

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]]);
}

Returns a new tensor with the same shape and device as the current tensor filled with zeros.

Returns a new tensor with the same shape and device as the current tensor filled with ones.

Returns a new tensor with the same shape and device as the current tensor filled random values sampled from the given distribution.

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]
}

Applies element wise addition operation.

y = x2 + x1

Applies element wise addition operation with a scalar.

y = x + s

Applies element wise substraction operation.

y = x2 - x1

Applies element wise substraction operation with a scalar.

y = x - s

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.

Swap two dimensions.

Panics

If the dimensions exceed the shape of than the tensor.

Applies the matrix multiplication operation.

C = AB

Panics

If the two tensors dont’ have a compatible shape.

Switch sign of each element in the tensor.

y = -x

Applies element wise multiplication operation.

y = x2 * x1

Applies element wise multiplication operation with scalar.

y = x2 * x1

Applies element wise division operation.

y = x2 / x1

Applies element wise division operation with scalar.

y = x2 / x1

Aggregate all elements in the tensor with the mean operation.

Aggregate all elements in the tensor with the sum operation.

Aggregate all elements along the given dimension or axis in the tensor with the mean operation.

Aggregate all elements along the given dimension or axis in the tensor with the sum operation.

Calculate the variance along the given dimension.

Calculate the variance along the given dimension without applying the Bessel’s correction.

Calculate the variance along the given dimension and also returns the mean.

Calculate the variance along the given dimension without applying the Bessel’s correction and also returns the mean.

Applies element wise equal comparison and returns a boolean tensor.

Panics

If the two tensors don’t have the same shape.

Applies element wise greater comparison and returns a boolean tensor.

Panics

If the two tensors don’t have the same shape.

Applies element wise greater-equal comparison and returns a boolean tensor.

Panics

If the two tensors don’t have the same shape.

Applies element wise lower comparison and returns a boolean tensor.

Panics

If the two tensors don’t have the same shape.

Applies element wise lower-equal comparison and returns a boolean tensor.

Panics

If the two tensors don’t have the same shape.

Applies element wise equal comparison and returns a boolean tensor.

Applies element wise greater comparison and returns a boolean tensor.

Applies element wise greater-equal comparison and returns a boolean tensor.

Applies element wise lower comparison and returns a boolean tensor.

Applies element wise lower-equal comparison and returns a boolean tensor.

Create a random tensor of the given shape where each element is sampled from the given distribution.

Create a tensor of the given shape where each element is zero.

Create a tensor of the given shape where each element is zero.

Create a tensor of the given shape where each element is one.

Create a tensor of the given shape where each element is one.

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] }
}

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] }
}

Fill each element with the given value based on the given mask.

Returns a tensor with full precision based on the selected backend.

Returns a tensor on the selected backend from a full precision tensor.

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] }
}

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] }
}

Concatenates all tensors into a new one along the given dimension.

Panics

If all tensors don’t have the same shape.

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.

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] }
}

Repeat the tensor along the given dimension.

Panics

If the selected dimension more than one item.

Trait Implementations§

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.