Struct Tensor
pub struct Tensor(/* private fields */);
Expand description
The core struct for manipulating tensors.
use candle_core::{Tensor, DType, Device};
let a = Tensor::arange(0f32, 6f32, &Device::Cpu)?.reshape((2, 3))?;
let b = Tensor::arange(0f32, 12f32, &Device::Cpu)?.reshape((3, 4))?;
let c = a.matmul(&b)?;
Tensors are reference counted with Arc
so cloning them is cheap.
Implementations§
§impl Tensor
impl Tensor
pub fn conv1d(
&self,
kernel: &Tensor,
padding: usize,
stride: usize,
dilation: usize,
groups: usize,
) -> Result<Tensor, Error>
pub fn conv1d( &self, kernel: &Tensor, padding: usize, stride: usize, dilation: usize, groups: usize, ) -> Result<Tensor, Error>
Applies a 1D convolution over the input tensor.
pub fn conv_transpose1d(
&self,
kernel: &Tensor,
padding: usize,
output_padding: usize,
stride: usize,
dilation: usize,
groups: usize,
) -> Result<Tensor, Error>
pub fn conv_transpose1d( &self, kernel: &Tensor, padding: usize, output_padding: usize, stride: usize, dilation: usize, groups: usize, ) -> Result<Tensor, Error>
Applies a 1D transposed convolution over the input tensor.
§impl Tensor
impl Tensor
pub fn apply_op1_no_bwd<C>(&self, c: &C) -> Result<Tensor, Error>where
C: CustomOp1,
pub fn apply_op1_no_bwd<C>(&self, c: &C) -> Result<Tensor, Error>where
C: CustomOp1,
Applies a unary custom op without backward support
pub fn apply_op2_no_bwd<C>(&self, rhs: &Tensor, c: &C) -> Result<Tensor, Error>where
C: CustomOp2,
pub fn apply_op2_no_bwd<C>(&self, rhs: &Tensor, c: &C) -> Result<Tensor, Error>where
C: CustomOp2,
Applies a binary custom op without backward support
pub fn apply_op3_no_bwd<C>(
&self,
t2: &Tensor,
t3: &Tensor,
c: &C,
) -> Result<Tensor, Error>where
C: CustomOp3,
pub fn apply_op3_no_bwd<C>(
&self,
t2: &Tensor,
t3: &Tensor,
c: &C,
) -> Result<Tensor, Error>where
C: CustomOp3,
Applies a ternary custom op without backward support
pub fn apply_op1_arc(
&self,
c: Arc<Box<dyn CustomOp1 + Sync + Send>>,
) -> Result<Tensor, Error>
pub fn apply_op1_arc( &self, c: Arc<Box<dyn CustomOp1 + Sync + Send>>, ) -> Result<Tensor, Error>
Applies a unary custom op.
pub fn apply_op1<C>(&self, c: C) -> Result<Tensor, Error>
pub fn apply_op2_arc(
&self,
rhs: &Tensor,
c: Arc<Box<dyn CustomOp2 + Sync + Send>>,
) -> Result<Tensor, Error>
pub fn apply_op2_arc( &self, rhs: &Tensor, c: Arc<Box<dyn CustomOp2 + Sync + Send>>, ) -> Result<Tensor, Error>
Applies a binary custom op.
pub fn apply_op2<C>(&self, r: &Tensor, c: C) -> Result<Tensor, Error>
pub fn apply_op3_arc(
&self,
t2: &Tensor,
t3: &Tensor,
c: Arc<Box<dyn CustomOp3 + Sync + Send>>,
) -> Result<Tensor, Error>
pub fn apply_op3_arc( &self, t2: &Tensor, t3: &Tensor, c: Arc<Box<dyn CustomOp3 + Sync + Send>>, ) -> Result<Tensor, Error>
Applies a ternary custom op.
pub fn apply_op3<C>( &self, t2: &Tensor, t3: &Tensor, c: C, ) -> Result<Tensor, Error>
§impl Tensor
impl Tensor
pub fn inplace_op1<C>(&self, c: &C) -> Result<(), Error>where
C: InplaceOp1,
pub fn inplace_op1<C>(&self, c: &C) -> Result<(), Error>where
C: InplaceOp1,
Applies a unary custom op in place.
pub fn inplace_op2<C>(&self, rhs: &Tensor, c: &C) -> Result<(), Error>where
C: InplaceOp2,
pub fn inplace_op2<C>(&self, rhs: &Tensor, c: &C) -> Result<(), Error>where
C: InplaceOp2,
Applies a unary custom op in place (for the first tensor).
§impl Tensor
impl Tensor
pub fn read_npy<T>(path: T) -> Result<Tensor, Error>
pub fn read_npy<T>(path: T) -> Result<Tensor, Error>
Reads a npy file and return the stored multi-dimensional array as a tensor.
pub fn read_npz<T>(path: T) -> Result<Vec<(String, Tensor)>, Error>
pub fn read_npz<T>(path: T) -> Result<Vec<(String, Tensor)>, Error>
Reads a npz file and returns the stored multi-dimensional arrays together with their names.
pub fn read_npz_by_name<T>(
path: T,
names: &[&str],
) -> Result<Vec<Tensor>, Error>
pub fn read_npz_by_name<T>( path: T, names: &[&str], ) -> Result<Vec<Tensor>, Error>
Reads a npz file and returns the stored multi-dimensional arrays for some specified names.
§impl Tensor
impl Tensor
pub fn arg_sort_last_dim(&self, asc: bool) -> Result<Tensor, Error>
pub fn arg_sort_last_dim(&self, asc: bool) -> Result<Tensor, Error>
Returns the indices that sort the tensor along the last dimension.
If asc
is true
, sorting is in ascending order. Otherwise sorting is performed in
descending order. The sort is unstable so there is no guarantees on the final order when it
comes to ties.
pub fn sort_last_dim(&self, asc: bool) -> Result<(Tensor, Tensor), Error>
pub fn sort_last_dim(&self, asc: bool) -> Result<(Tensor, Tensor), Error>
Sorts the tensor along the last dimension, returns the sorted tensor together with the sorted indexes.
If asc
is true
, sorting is in ascending order. Otherwise sorting is performed in
descending order. The sort is unstable so there is no guarantees on the final order when it
comes to ties.
§impl Tensor
impl Tensor
pub fn ones<S>(shape: S, dtype: DType, device: &Device) -> Result<Tensor, Error>where
S: Into<Shape>,
pub fn ones<S>(shape: S, dtype: DType, device: &Device) -> Result<Tensor, Error>where
S: Into<Shape>,
Creates a new tensor filled with ones.
use candle_core::{Tensor, DType, Device};
let a = Tensor::ones((2, 3), DType::F32, &Device::Cpu)?;
let b = Tensor::from_slice(&[1.0f32, 1.0, 1.0, 1.0, 1.0, 1.0], (2, 3), &Device::Cpu)?;
// a == b
pub fn ones_like(&self) -> Result<Tensor, Error>
pub fn ones_like(&self) -> Result<Tensor, Error>
Creates a new tensor filled with ones with same shape, dtype, and device as the other tensor.
use candle_core::{Tensor, DType, Device};
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let b = a.ones_like()?;
// b == a + 1
pub fn zeros<S>(
shape: S,
dtype: DType,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
pub fn zeros<S>(
shape: S,
dtype: DType,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
Creates a new tensor filled with zeros.
use candle_core::{Tensor, DType, Device};
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let b = Tensor::from_slice(&[0.0f32, 0.0, 0.0, 0.0, 0.0, 0.0], (2, 3), &Device::Cpu)?;
// a == b
pub fn zeros_like(&self) -> Result<Tensor, Error>
pub fn zeros_like(&self) -> Result<Tensor, Error>
Creates a new tensor filled with ones with same shape, dtype, and device as the other tensor.
use candle_core::{Tensor, DType, Device};
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let b = a.zeros_like()?;
// b is on CPU f32.
pub unsafe fn empty<S>(
shape: S,
dtype: DType,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
pub unsafe fn empty<S>(
shape: S,
dtype: DType,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
Creates a new tensor filled with uninitialized memory.
§Safety
This returns uninitialized memory.
use candle_core::{Tensor, DType, Device};
let a = unsafe { Tensor::empty((2, 3), DType::F32, &Device::Cpu)? };
// a == b
pub unsafe fn empty_like(&self) -> Result<Tensor, Error>
pub unsafe fn empty_like(&self) -> Result<Tensor, Error>
Creates a new tensor filled with uninitialized memory of the same shape, dtype, and device as the other tensor.
§Safety
This returns uninitialized memory.
use candle_core::{Tensor, DType, Device};
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let b = unsafe { a.empty_like()? };
pub fn rand<S, T>(lo: T, up: T, s: S, device: &Device) -> Result<Tensor, Error>where
S: Into<Shape>,
T: FloatDType,
pub fn rand<S, T>(lo: T, up: T, s: S, device: &Device) -> Result<Tensor, Error>where
S: Into<Shape>,
T: FloatDType,
Creates a new tensor initialized with values sampled uniformly between lo
and up
.
pub fn rand_like(&self, lo: f64, up: f64) -> Result<Tensor, Error>
pub fn randn_like(&self, mean: f64, stdev: f64) -> Result<Tensor, Error>
pub fn randn<S, T>(
mean: T,
std: T,
s: S,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
T: FloatDType,
pub fn randn<S, T>(
mean: T,
std: T,
s: S,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
T: FloatDType,
Creates a new tensor initialized with values sampled from a normal distribution with the
specified mean
and standard deviation std
.
pub fn new<A>(array: A, device: &Device) -> Result<Tensor, Error>where
A: NdArray,
pub fn new<A>(array: A, device: &Device) -> Result<Tensor, Error>where
A: NdArray,
Creates a new tensor on the specified device using the content and shape of the input.
pub fn full<D, S>(value: D, shape: S, device: &Device) -> Result<Tensor, Error>where
D: WithDType,
S: Into<Shape>,
pub fn full<D, S>(value: D, shape: S, device: &Device) -> Result<Tensor, Error>where
D: WithDType,
S: Into<Shape>,
Returns a new tensor with all the elements having the same specified value. Note that
the tensor is not contiguous so you would have to call .contiguous()
on it if needed.
use candle_core::{Tensor, Device};
let a = Tensor::full(3.5, (2, 4), &Device::Cpu)?;
assert_eq!(a.to_vec2::<f64>()?, &[
[3.5, 3.5, 3.5, 3.5],
[3.5, 3.5, 3.5, 3.5],
]);
pub fn from_iter<D>(
iter: impl IntoIterator<Item = D>,
device: &Device,
) -> Result<Tensor, Error>where
D: WithDType,
pub fn from_iter<D>(
iter: impl IntoIterator<Item = D>,
device: &Device,
) -> Result<Tensor, Error>where
D: WithDType,
Creates a new 1D tensor from an iterator.
use candle_core::{Tensor, Device};
let a = Tensor::from_iter( [1.0, 2.0, 3.0, 4.0].into_iter(), &Device::Cpu)?;
assert_eq!(a.to_vec1::<f64>()?, &[1.0, 2.0, 3.0, 4.0]);
pub fn arange<D>(start: D, end: D, device: &Device) -> Result<Tensor, Error>where
D: WithDType,
pub fn arange<D>(start: D, end: D, device: &Device) -> Result<Tensor, Error>where
D: WithDType,
Creates a new 1D tensor with values from the interval [start, end)
taken with a common
difference 1
from start
.
use candle_core::{Tensor, Device};
let a = Tensor::arange(2., 5., &Device::Cpu)?;
assert_eq!(a.to_vec1::<f64>()?, &[2., 3., 4.]);
pub fn arange_step<D>(
start: D,
end: D,
step: D,
device: &Device,
) -> Result<Tensor, Error>where
D: WithDType,
pub fn arange_step<D>(
start: D,
end: D,
step: D,
device: &Device,
) -> Result<Tensor, Error>where
D: WithDType,
Creates a new 1D tensor with values from the interval [start, end)
taken with a common
difference step
from start
.
use candle_core::{Tensor, Device};
let a = Tensor::arange_step(2.0, 4.0, 0.5, &Device::Cpu)?;
assert_eq!(a.to_vec1::<f64>()?, &[2.0, 2.5, 3.0, 3.5]);
pub fn from_vec<S, D>(
data: Vec<D>,
shape: S,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
D: WithDType,
pub fn from_vec<S, D>(
data: Vec<D>,
shape: S,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
D: WithDType,
Creates a new tensor initialized with values from the input vector. The number of elements in this vector must be the same as the number of elements defined by the shape. If the device is cpu, no data copy is made.
use candle_core::{Tensor, Device};
let a = Tensor::from_vec(vec!{1., 2., 3., 4., 5., 6.}, (2, 3), &Device::Cpu)?;
assert_eq!(a.to_vec2::<f64>()?, &[
[1., 2., 3.],
[4., 5., 6.]
]);
pub fn from_slice<S, D>(
array: &[D],
shape: S,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
D: WithDType,
pub fn from_slice<S, D>(
array: &[D],
shape: S,
device: &Device,
) -> Result<Tensor, Error>where
S: Into<Shape>,
D: WithDType,
Creates a new tensor initialized with values from the input slice. The number of elements in this vector must be the same as the number of elements defined by the shape.
use candle_core::{Tensor, Device};
let values = vec![1., 2., 3., 4., 5., 6., 7., 8.];
let a = Tensor::from_slice(&values[1..7], (2, 3), &Device::Cpu)?;
assert_eq!(a.to_vec2::<f64>()?, &[
[2., 3., 4.],
[5., 6., 7.]
]);
pub fn track_op(&self) -> bool
pub fn track_op(&self) -> bool
Returns true if the computation graph should track this op, that is if it is a variable or if it has some variable as dependencies.
pub fn add(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn mul(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn sub(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn div(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn maximum<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn minimum<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn broadcast_add(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_mul(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_sub(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_div(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_maximum(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_minimum(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_eq(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_ne(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_lt(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_le(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_gt(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_ge(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn recip(&self) -> Result<Tensor, Error>
pub fn neg(&self) -> Result<Tensor, Error>
pub fn exp(&self) -> Result<Tensor, Error>
pub fn log(&self) -> Result<Tensor, Error>
pub fn sin(&self) -> Result<Tensor, Error>
pub fn cos(&self) -> Result<Tensor, Error>
pub fn tanh(&self) -> Result<Tensor, Error>
pub fn abs(&self) -> Result<Tensor, Error>
pub fn sqr(&self) -> Result<Tensor, Error>
pub fn sqrt(&self) -> Result<Tensor, Error>
pub fn gelu(&self) -> Result<Tensor, Error>
pub fn gelu_erf(&self) -> Result<Tensor, Error>
pub fn erf(&self) -> Result<Tensor, Error>
pub fn relu(&self) -> Result<Tensor, Error>
pub fn silu(&self) -> Result<Tensor, Error>
pub fn ceil(&self) -> Result<Tensor, Error>
pub fn floor(&self) -> Result<Tensor, Error>
pub fn round(&self) -> Result<Tensor, Error>
pub fn sign(&self) -> Result<Tensor, Error>
pub fn round_to(&self, decimals: i32) -> Result<Tensor, Error>
pub fn round_to(&self, decimals: i32) -> Result<Tensor, Error>
Round element of the input tensor to the nearest integer.
If the number of decimals is negative, it specifies the number of positions to the left of the decimal point.
pub fn to_scalar<S>(&self) -> Result<S, Error>where
S: WithDType,
pub fn to_scalar<S>(&self) -> Result<S, Error>where
S: WithDType,
Retrieves the single scalar value hold in the tensor. If the tensor contains multiple dimensions, an error is returned instead.
pub fn repeat<S>(&self, shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
pub fn repeat<S>(&self, shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
Repeat this tensor along the specified dimensions.
pub fn meshgrid<A>(args: &[A], xy_indexing: bool) -> Result<Vec<Tensor>, Error>
pub fn meshgrid<A>(args: &[A], xy_indexing: bool) -> Result<Vec<Tensor>, Error>
Creates grids of coordinates specified by the 1D inputs.
§Arguments
args
- A slice of 1D tensors.xy_indexing
- Whether to use xy indexing or ij indexing. If xy is selected, the first dimension corresponds to the cardinality of the second input and the second dimension corresponds to the cardinality of the first input. If ij is selected, the dimensions are in the same order as the cardinality of the inputs.
§Examples
use candle_core::{Tensor, Device, Shape};
let x = Tensor::new(&[1f32, 2., 3.], &Device::Cpu)?;
let y = Tensor::new(&[4f32, 5., 6.], &Device::Cpu)?;
let grids_xy = Tensor::meshgrid(&[&x, &y], true)?;
assert_eq!(grids_xy.len(), 2);
assert_eq!(grids_xy[0].dims(), &[3, 3]);
assert_eq!(grids_xy[0].to_vec2::<f32>()?, &[[1., 2., 3.], [1., 2., 3.], [1., 2., 3.]]);
assert_eq!(grids_xy[1].to_vec2::<f32>()?, &[[4., 4., 4.], [5., 5., 5.], [6., 6., 6.]]);
let grids_ij = Tensor::meshgrid(&[&x, &y], false)?;
assert_eq!(grids_ij[0].to_vec2::<f32>()?, &[[1., 1., 1.], [2., 2., 2.], [3., 3., 3.]]);
assert_eq!(grids_ij[1].to_vec2::<f32>()?, &[[4., 5., 6.], [4., 5., 6.], [4., 5., 6.]]);
§Errors
- Will return
Err
ifargs
contains less than 2 tensors.
pub fn affine(&self, mul: f64, add: f64) -> Result<Tensor, Error>
pub fn affine(&self, mul: f64, add: f64) -> Result<Tensor, Error>
This operation multiplies the input tensor by mul
then adds add
and return the result.
The input values mul
and add
are casted to the appropriate type so some rounding might
be performed.
use candle_core::{Tensor, Device};
let a = Tensor::new(&[[0f32, 1.], [2., 3.]], &Device::Cpu)?;
let a = a.affine(4., -2.)?;
assert_eq!(a.to_vec2::<f32>()?, &[[-2.0, 2.0], [6.0, 10.0]]);
pub fn elu(&self, alpha: f64) -> Result<Tensor, Error>
pub fn elu(&self, alpha: f64) -> Result<Tensor, Error>
Applies the Exponential Linear Unit (ELU) function on each element of the input tensor.
pub fn chunk<D>(&self, chunks: usize, dim: D) -> Result<Vec<Tensor>, Error>where
D: Dim,
pub fn chunk<D>(&self, chunks: usize, dim: D) -> Result<Vec<Tensor>, Error>where
D: Dim,
Split a tensor into the specified number of chunks, this may return less chunks than specified.
pub fn narrow<D>(
&self,
dim: D,
start: usize,
len: usize,
) -> Result<Tensor, Error>where
D: Dim,
pub fn narrow<D>(
&self,
dim: D,
start: usize,
len: usize,
) -> Result<Tensor, Error>where
D: Dim,
Returns a new tensor that is a narrowed version of the input, the dimension dim
ranges from start
to start + len
.
use candle_core::{Tensor, Device};
let a = Tensor::new(&[
[0f32, 1., 2.],
[3. , 4., 5.],
[6. , 7., 8.]
], &Device::Cpu)?;
let b = a.narrow(0, 1, 2)?;
assert_eq!(b.shape().dims(), &[2, 3]);
assert_eq!(b.to_vec2::<f32>()?, &[
[3., 4., 5.],
[6., 7., 8.]
]);
let c = a.narrow(1, 1, 1)?;
assert_eq!(c.shape().dims(), &[3, 1]);
assert_eq!(c.to_vec2::<f32>()?, &[
[1.],
[4.],
[7.]
]);
pub fn roll<D>(&self, shift: i32, dim: D) -> Result<Tensor, Error>where
D: Dim + Clone,
pub fn roll<D>(&self, shift: i32, dim: D) -> Result<Tensor, Error>where
D: Dim + Clone,
Roll the tensor input along the given dimension. Elements that are shifted beyond the last position are re-introduced at the first position.
let tensor = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let tensor = tensor.roll(1, 0)?;
assert_eq!(tensor.to_vec2::<f32>()?, &[[4., 5.], [0., 1.], [2., 3.]]);
let tensor = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let tensor = tensor.roll(-1, 0)?;
assert_eq!(tensor.to_vec2::<f32>()?, &[[2., 3.], [4., 5.], [0., 1.]]);
pub fn sum_keepdim<D>(&self, sum_dims: D) -> Result<Tensor, Error>where
D: Dims,
pub fn sum_keepdim<D>(&self, sum_dims: D) -> Result<Tensor, Error>where
D: Dims,
Returns the sum of all elements in the input tensor. The sum is performed over all the input dimensions.
The resulting tensor has a shape that is similar to the shape of the input tensor, except
that the number of elements for each dimension index in sum_dims
is 1.
use candle_core::{Tensor, Device};
let a = Tensor::new(&[[0f32, 1.], [2., 3.]], &Device::Cpu)?;
let s = a.sum_keepdim(0)?;
assert_eq!(s.to_vec2::<f32>()?, &[[2., 4.]]);
let s = a.sum_keepdim(1)?;
assert_eq!(s.to_vec2::<f32>()?, &[[1.], [5.]]);
let s = a.sum_keepdim((0, 1))?;
assert_eq!(s.to_vec2::<f32>()?, &[[6.]]);
pub fn sum<D>(&self, sum_dims: D) -> Result<Tensor, Error>where
D: Dims,
pub fn sum<D>(&self, sum_dims: D) -> Result<Tensor, Error>where
D: Dims,
Returns the sum of all elements in the input tensor. The sum is performed over all the
input dimensions and compared to sum_keepdim
these dimensions are squeezed rather than
kept.
pub fn mean_keepdim<D>(&self, mean_dims: D) -> Result<Tensor, Error>where
D: Dims,
pub fn mean_keepdim<D>(&self, mean_dims: D) -> Result<Tensor, Error>where
D: Dims,
Returns the mean of all elements in the input tensor. The mean is performed over all the input dimensions.
The resulting tensor has a shape that is similar to the shape of the input tensor, except
that the number of elements for each dimension index in mean_dims
is 1.
use candle_core::{Tensor, Device};
let a = Tensor::new(&[[0f32, 1.], [2., 3.]], &Device::Cpu)?;
let s = a.mean_keepdim(0)?;
assert_eq!(s.to_vec2::<f32>()?, &[[1., 2.]]);
let s = a.mean_keepdim(1)?;
assert_eq!(s.to_vec2::<f32>()?, &[[0.5], [2.5]]);
let s = a.mean_keepdim((0, 1))?;
assert_eq!(s.to_vec2::<f32>()?, &[[1.5]]);
pub fn mean<D>(&self, mean_dims: D) -> Result<Tensor, Error>where
D: Dims,
pub fn mean<D>(&self, mean_dims: D) -> Result<Tensor, Error>where
D: Dims,
Returns the mean of all elements in the input tensor. The mean is performed over all the
input dimensions and compared to mean_keepdim
these dimensions are squeezed rather than
kept.
pub fn var_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn var_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Returns the unbiased variance over the selected dimension.
pub fn var<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn var<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Returns the unbiased variance over the selected dimension.
pub fn max_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn max_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Gathers the maximum value across the selected dimension. The resulting shape has the same number of dimensions as the original tensor and the select dimension has a single element.
pub fn max<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn max<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Similar to max_keepdim
but the target dimension is squeezed.
pub fn min_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn min_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Gathers the minimum value across the selected dimension. The resulting shape has the same number of dimensions as the original tensor and the select dimension has a single element.
pub fn min<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn min<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Similar to min_keepdim
but the target dimension is squeezed.
pub fn argmax_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn argmax<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn argmax<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Similar to argmax_keepdim
but the target dimension is squeezed.
pub fn argmin_keepdim<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn argmin<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn argmin<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Similar to argmin_keepdim
but the target dimension is squeezed.
pub fn cmp<T>(&self, rhs: T, op: CmpOp) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn cmp<T>(&self, rhs: T, op: CmpOp) -> Result<Tensor, Error>where
T: TensorOrScalar,
Element-wise comparison between two tensors, e.g. equality, greater than, … The actual
comparison operation is specified by the op
argument.
The returned tensor has the same shape as the original tensors and uses u8
elements.
pub fn ne<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn ne<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
Element-wise non-equality.
pub fn lt<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn lt<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
Element-wise comparison with lower-than, the returned tensor uses value 1 where self < rhs
and 0 otherwise.
pub fn gt<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn gt<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
Element-wise comparison with greater-than, the returned tensor uses value 1 where self > rhs
and 0 otherwise.
pub fn ge<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn ge<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
Element-wise comparison with greater-equal, the returned tensor uses value 1 where self >= rhs
and 0 otherwise.
pub fn le<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
pub fn le<T>(&self, rhs: T) -> Result<Tensor, Error>where
T: TensorOrScalar,
Element-wise comparison with lower-equal, the returned tensor uses value 1 where self <= rhs
and 0 otherwise.
pub fn clamp<T1, T2>(&self, min: T1, max: T2) -> Result<Tensor, Error>where
T1: TensorOrScalar,
T2: TensorOrScalar,
pub fn clamp<T1, T2>(&self, min: T1, max: T2) -> Result<Tensor, Error>where
T1: TensorOrScalar,
T2: TensorOrScalar,
Clamp the tensor values to be between min
and max
.
pub fn interpolate1d(&self, target_size: usize) -> Result<Tensor, Error>
pub fn interpolate1d(&self, target_size: usize) -> Result<Tensor, Error>
Interpolate the input tensor to the target_size
size, taking the value of the nearest element.
The input tensor should have three dimensions, (batch, channels, l)
, the returned
tensor also has three dimensions, (batch, channels, target_size)
.
pub fn upsample_nearest1d(&self, target_size: usize) -> Result<Tensor, Error>
pub fn upsample_nearest1d(&self, target_size: usize) -> Result<Tensor, Error>
Alias for interpolate1d
.
pub fn interpolate2d(
&self,
target_h: usize,
target_w: usize,
) -> Result<Tensor, Error>
pub fn interpolate2d( &self, target_h: usize, target_w: usize, ) -> Result<Tensor, Error>
Interpolate the input tensor to the (target_h, target_w)
size, taking the value of the
nearest element.
The input tensor should have four dimensions, (batch, channels, h, w)
, the returned
tensor also has four dimensions, (batch, channels, target_h, target_w)
.
pub fn upsample_nearest2d(
&self,
target_h: usize,
target_w: usize,
) -> Result<Tensor, Error>
pub fn upsample_nearest2d( &self, target_h: usize, target_w: usize, ) -> Result<Tensor, Error>
Alias for interpolate2d
.
pub fn avg_pool2d<T>(&self, sz: T) -> Result<Tensor, Error>where
T: ToUsize2,
pub fn avg_pool2d<T>(&self, sz: T) -> Result<Tensor, Error>where
T: ToUsize2,
2D average pooling over an input tensor with multiple channels.
The input tensor should have four dimensions, (batch, channels, h, w)
, the returned
tensor also has four dimensions, (batch, channels, h', w')
. The pooling is performed on
the two last dimensions using a kernel of size sz
. The returned element is the average
value over the kernel window.
pub fn avg_pool2d_with_stride<T>(
&self,
kernel_size: T,
stride: T,
) -> Result<Tensor, Error>where
T: ToUsize2,
pub fn avg_pool2d_with_stride<T>(
&self,
kernel_size: T,
stride: T,
) -> Result<Tensor, Error>where
T: ToUsize2,
Same as avg_pool2d
but with a stride
that can be set to a value different from the
kernel size.
pub fn max_pool2d<T>(&self, sz: T) -> Result<Tensor, Error>where
T: ToUsize2,
pub fn max_pool2d<T>(&self, sz: T) -> Result<Tensor, Error>where
T: ToUsize2,
2D max pooling over an input tensor with multiple channels.
The input tensor should have four dimensions, (batch, channels, h, w)
, the returned
tensor also has four dimensions, (batch, channels, h', w')
. The pooling is performed on
the two last dimensions using a kernel of size sz
, the returned element is the maximum
value over the kernel window.
pub fn max_pool2d_with_stride<T>(
&self,
kernel_size: T,
stride: T,
) -> Result<Tensor, Error>where
T: ToUsize2,
pub fn max_pool2d_with_stride<T>(
&self,
kernel_size: T,
stride: T,
) -> Result<Tensor, Error>where
T: ToUsize2,
Same as max_pool2d
but with a stride
that can be set to a value different from the
kernel size.
pub fn matmul(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn matmul(&self, rhs: &Tensor) -> Result<Tensor, Error>
Returns the matrix-multiplication of the input tensor with the other provided tensor.
§Arguments
self
- A tensor with dimensionsb1, b2, ..., bi, m, k
.rhs
- A tensor with dimensionsb1, b2, ..., bi, k, n
.
The resulting tensor has dimensions b1, b2, ..., bi, m, n
.
pub fn broadcast_matmul(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_matmul(&self, rhs: &Tensor) -> Result<Tensor, Error>
Matrix-multiplication with broadcasting support.
Compared to matmul
the two matrixes are allowed to have different dimensions as long as
they are compatible for broadcast. E.g. if self
has shape (j, 1, n, k)
and rhs
has
shape (l, k, m)
, the output will have shape (j, l, n, m)
.
pub fn matmul_with_alpha_beta(
&self,
rhs: &Tensor,
c: &mut Tensor,
scale: Option<f64>,
) -> Result<(), Error>
pub fn matmul_with_alpha_beta( &self, rhs: &Tensor, c: &mut Tensor, scale: Option<f64>, ) -> Result<(), Error>
Returns the matrix-multiplication of the input tensor with the other provided tensor. The result is scaled
and then added to the output tensor, the bias tensor c
.
If scale
is None, then the output is as follows:
c := c + axb
Else:
c := c + scale * (axb)
This function is faster than a matmul followed by some scaling multiply because the scaling is fused in the GEMM kernel.
This is incompatible with gradient tracking. No gradients will be tracked on this operation. However, this also means
there is an allocation saved as the output is in c
.
§Arguments
self
- A tensor with dimensionsb1, b2, ..., bi, m, k
.rhs
- A tensor with dimensionsb1, b2, ..., bi, k, n
.c
- A tensor with dimensionsb1, b2, ..., bi, m, n
, into which the result is accumulated and added to.scale
- Factor to multiplyself
xrhs
by
pub fn matmul_with_alpha(
&self,
rhs: &Tensor,
scale: Option<f64>,
) -> Result<Tensor, Error>
pub fn matmul_with_alpha( &self, rhs: &Tensor, scale: Option<f64>, ) -> Result<Tensor, Error>
Returns the matrix-multiplication of the input tensor with the other provided tensor. The result is scaled.
This function is faster than a matmul followed by some scaling multiply because the scaling is fused in the GEMM kernel.
The output is as follows:
scale * (axb)
This is incompatible with gradient tracking. No gradients will be tracked on this operation.
§Arguments
self
- A tensor with dimensionsb1, b2, ..., bi, m, k
.rhs
- A tensor with dimensionsb1, b2, ..., bi, k, n
.scale
- Factor to multiplyself
xrhs
by.
pub fn broadcast_matmul_with_alpha(
&self,
rhs: &Tensor,
scale: Option<f64>,
) -> Result<Tensor, Error>
pub fn broadcast_matmul_with_alpha( &self, rhs: &Tensor, scale: Option<f64>, ) -> Result<Tensor, Error>
Matrix-multiplication with broadcasting support and fused scaling.
Compared to matmul
the two matrixes are allowed to have different dimensions as long as
they are compatible for broadcast. E.g. if self
has shape (j, 1, n, k)
and rhs
has
shape (l, k, m)
, the output will have shape (j, l, n, m)
.
pub fn where_cond(
&self,
on_true: &Tensor,
on_false: &Tensor,
) -> Result<Tensor, Error>
pub fn where_cond( &self, on_true: &Tensor, on_false: &Tensor, ) -> Result<Tensor, Error>
Returns a tensor with the same shape as the input tensor, the values are taken from
on_true
if the input tensor value is not zero, and on_false
at the positions where the
input tensor is equal to zero.
pub fn embedding(&self, ids: &Tensor) -> Result<Tensor, Error>
pub fn embedding(&self, ids: &Tensor) -> Result<Tensor, Error>
Returns a tensor with the values from the self
tensor at the index corresponding to the
values hold in the ids
tensor.
§Arguments
self
- A tensor with dimensionsv, h
.ids
- A tensor with dimensionss
and with integer values between 0 and v (exclusive).
The resulting tensor has dimensions s, h
. s
is called the sequence length, v
the
vocabulary size, and h
the hidden size.
use candle_core::{Tensor, Device};
let values = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let ids = Tensor::new(&[2u32, 1u32, 2u32], &Device::Cpu)?;
let emb = values.embedding(&ids)?;
assert_eq!(emb.to_vec2::<f32>()?, &[[4., 5.], [2., 3.], [4., 5.]]);
pub fn strided_index(&self) -> StridedIndex<'_>
pub fn strided_index(&self) -> StridedIndex<'_>
Returns an iterator over position of the elements in the storage when ranging over the index tuples in lexicographic order.
pub fn strided_blocks(&self) -> StridedBlocks<'_>
pub fn strided_blocks(&self) -> StridedBlocks<'_>
Similar to strided_index
but returns the position of the start of each contiguous block
as well as the length of the contiguous blocks. For a contiguous tensor, the index iterator
will only return the start offset and the size would be the number of elements in the
tensor.
pub fn to_vec1<S>(&self) -> Result<Vec<S>, Error>where
S: WithDType,
pub fn to_vec1<S>(&self) -> Result<Vec<S>, Error>where
S: WithDType,
Returns the data contained in a 1D tensor as a vector of scalar values.
pub fn to_vec2<S>(&self) -> Result<Vec<Vec<S>>, Error>where
S: WithDType,
pub fn to_vec2<S>(&self) -> Result<Vec<Vec<S>>, Error>where
S: WithDType,
Returns the data contained in a 2D tensor as a vector of vector of scalar values.
pub fn to_vec3<S>(&self) -> Result<Vec<Vec<Vec<S>>>, Error>where
S: WithDType,
pub fn to_vec3<S>(&self) -> Result<Vec<Vec<Vec<S>>>, Error>where
S: WithDType,
Returns the data contained in a 3D tensor.
pub fn shape(&self) -> &Shape
pub fn shape(&self) -> &Shape
The tensor shape, i.e. dimension sizes on each axis.
pub fn dim<D>(&self, dim: D) -> Result<usize, Error>where
D: Dim,
pub fn dim<D>(&self, dim: D) -> Result<usize, Error>where
D: Dim,
The dimension size for a specified dimension index.
pub fn layout(&self) -> &Layout
pub fn layout(&self) -> &Layout
The layout of the input tensor, this stores both the shape of the tensor as well as the strides and the start offset to apply to the underlying storage.
pub fn stride(&self) -> &[usize]
pub fn rank(&self) -> usize
pub fn rank(&self) -> usize
The number of dimensions for this tensor, 0 for a scalar tensor, 1 for a 1D tensor, etc.
pub fn elem_count(&self) -> usize
pub fn elem_count(&self) -> usize
The number of elements stored in this tensor.
pub fn id(&self) -> TensorId
pub fn id(&self) -> TensorId
The unique identifier for this tensor.
pub fn is_variable(&self) -> bool
pub fn is_variable(&self) -> bool
Whether this tensor is a variable or not. A variable is a tensor for which gradient is tracked and on which backpropagation can be performed.
pub fn sum_all(&self) -> Result<Tensor, Error>
pub fn sum_all(&self) -> Result<Tensor, Error>
Computes the sum of all the elements in this tensor and returns a tensor holding this scalar with zero dimensions.
use candle_core::{Tensor, Device};
let tensor = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let tensor = tensor.sum_all()?;
assert_eq!(tensor.to_scalar::<f32>()?, 15.);
pub fn mean_all(&self) -> Result<Tensor, Error>
pub fn flatten<D1, D2>(
&self,
start_dim: D1,
end_dim: D2,
) -> Result<Tensor, Error>where
D1: Dim,
D2: Dim,
pub fn flatten<D1, D2>(
&self,
start_dim: D1,
end_dim: D2,
) -> Result<Tensor, Error>where
D1: Dim,
D2: Dim,
Flattens the input tensor on the dimension indexes from start_dim
to end_dim
(both
inclusive).
pub fn flatten_to<D>(&self, end_dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn flatten_to<D>(&self, end_dim: D) -> Result<Tensor, Error>where
D: Dim,
Flattens the input tensor on the dimension indexes from 0
to end_dim
(inclusive).
pub fn flatten_from<D>(&self, start_dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn flatten_from<D>(&self, start_dim: D) -> Result<Tensor, Error>where
D: Dim,
Flattens the input tensor on the dimension indexes from start_dim
(inclusive) to the last
dimension.
pub fn flatten_all(&self) -> Result<Tensor, Error>
pub fn flatten_all(&self) -> Result<Tensor, Error>
Flattens the input tensor by reshaping it into a one dimension tensor.
use candle_core::{Tensor, Device};
let tensor = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let tensor = tensor.flatten_all()?;
assert_eq!(tensor.to_vec1::<f32>()?, &[0., 1., 2., 3., 4., 5.]);
pub fn get(&self, i: usize) -> Result<Tensor, Error>
pub fn get(&self, i: usize) -> Result<Tensor, Error>
Returns the sub-tensor fixing the index at i
on the first dimension.
use candle_core::{Tensor, Device};
let tensor = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let t = tensor.get(0)?;
assert_eq!(t.to_vec1::<f32>()?, &[0., 1.]);
let t = tensor.get(1)?;
assert_eq!(t.to_vec1::<f32>()?, &[2., 3.]);
pub fn get_on_dim<D>(&self, dim: D, index: usize) -> Result<Tensor, Error>where
D: Dim,
pub fn get_on_dim<D>(&self, dim: D, index: usize) -> Result<Tensor, Error>where
D: Dim,
Returns the sub-tensor fixing the index at index
on the dimension dim
.
use candle_core::{Tensor, Device};
let tensor = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let t = tensor.get_on_dim(1, 0)?;
assert_eq!(t.to_vec1::<f32>()?, &[0., 2., 4.]);
let t = tensor.get_on_dim(1, 1)?;
assert_eq!(t.to_vec1::<f32>()?, &[1., 3., 5.]);
let t = tensor.get_on_dim(0, 1)?;
assert_eq!(t.to_vec1::<f32>()?, &[2., 3.]);
pub fn t(&self) -> Result<Tensor, Error>
pub fn t(&self) -> Result<Tensor, Error>
Returns a tensor that is a transposed version of the input, the two last dimensions of the input are swapped.
use candle_core::{Tensor, Device};
let tensor = Tensor::new(&[[0f32, 1.], [2., 3.], [4., 5.]], &Device::Cpu)?;
let tensor = tensor.t()?;
assert_eq!(tensor.to_vec2::<f32>()?, &[[0.0, 2.0, 4.0], [1.0, 3.0, 5.0]]);
pub fn transpose<D1, D2>(&self, dim1: D1, dim2: D2) -> Result<Tensor, Error>where
D1: Dim,
D2: Dim,
pub fn transpose<D1, D2>(&self, dim1: D1, dim2: D2) -> Result<Tensor, Error>where
D1: Dim,
D2: Dim,
Returns a tensor that is a transposed version of the input, the given dimensions are swapped.
pub fn permute<D>(&self, dims: D) -> Result<Tensor, Error>where
D: Dims,
pub fn permute<D>(&self, dims: D) -> Result<Tensor, Error>where
D: Dims,
Returns a tensor with the same data as the input where the dimensions have been permuted. dims must be a permutation, i.e. include each dimension index exactly once.
use candle_core::{Tensor, Device};
let tensor = Tensor::arange(0u32, 120u32, &Device::Cpu)?.reshape((2, 3, 4, 5))?;
assert_eq!(tensor.dims(), &[2, 3, 4, 5]);
let tensor = tensor.permute((2, 3, 1, 0))?;
assert_eq!(tensor.dims(), &[4, 5, 3, 2]);
pub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Returns true if the data is stored in a C contiguous (aka row major) way.
pub fn is_fortran_contiguous(&self) -> bool
pub fn is_fortran_contiguous(&self) -> bool
Returns true if the data is stored in a Fortran contiguous (aka column major) way.
pub fn copy(&self) -> Result<Tensor, Error>
pub fn copy(&self) -> Result<Tensor, Error>
Compared to clone, this copies the actual storage but may fail because of running out of memory.
pub fn detach(&self) -> Tensor
pub fn detach(&self) -> Tensor
Returns a new tensor detached from the current graph, gradient are not propagated through this new node. The storage of this tensor is shared with the initial tensor.
If the tensor is already detached from the computation graph, the same tensor is returned.
pub fn to_device(&self, device: &Device) -> Result<Tensor, Error>
pub fn to_device(&self, device: &Device) -> Result<Tensor, Error>
If the target device is the same as the tensor device, only a shallow copy is performed.
pub fn broadcast_left<S>(&self, left_shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
pub fn broadcast_left<S>(&self, left_shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
Returns a new tensor duplicating data from the original tensor. New dimensions are inserted on the left.
pub fn broadcast_as<S>(&self, shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
pub fn broadcast_as<S>(&self, shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
Broadcast the input tensor to the target shape. This returns an error if the input shape is not compatible with the target shape.
If the input shape is i_1, i_2, ... i_k
, the target shape has to have k
dimensions or
more and shape j_1, ..., j_l, t_1, t_2, ..., t_k
. The dimensions j_1
to j_l
can have
any value, the dimension t_a
must be equal to i_a
if i_a
is different from 1. If
i_a
is equal to 1, any value can be used.
pub fn expand<S>(&self, shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
pub fn expand<S>(&self, shape: S) -> Result<Tensor, Error>where
S: Into<Shape>,
An alias for broadcast_as.
pub fn to_dtype(&self, dtype: DType) -> Result<Tensor, Error>
pub fn to_dtype(&self, dtype: DType) -> Result<Tensor, Error>
Casts the input tensor to the target dtype
.
use candle_core::{Tensor, Device};
let tensor = Tensor::new(3.14159265358979f64, &Device::Cpu)?;
assert_eq!(tensor.to_scalar::<f64>()?, 3.14159265358979);
let tensor = tensor.to_dtype(candle_core::DType::F32)?;
assert_eq!(tensor.to_scalar::<f32>()?, 3.1415927);
pub fn contiguous(&self) -> Result<Tensor, Error>
pub fn contiguous(&self) -> Result<Tensor, Error>
Returns a tensor that is in row major order. This is the same as the original tensor if it was already contiguous, otherwise a copy is triggered.
pub fn force_contiguous(&self) -> Result<Tensor, Error>
pub fn force_contiguous(&self) -> Result<Tensor, Error>
Returns a tensor that is in row major order. This always makes a copy.
pub fn reshape<S>(&self, s: S) -> Result<Tensor, Error>where
S: ShapeWithOneHole,
pub fn reshape<S>(&self, s: S) -> Result<Tensor, Error>where
S: ShapeWithOneHole,
Reshape returns a tensor with the target shape provided that the number of elements of the original tensor is the same. If the input tensor is contiguous, this is a view on the original data. Otherwise this uses a new storage and copies the data over, the returned tensor is always contiguous.
The shape can be specified using a tuple of usize
and at most one ()
in which case
the behavior is the same as when using -1
in PyTorch: this dimension size is adjusted so
as to match the number of elements in the tensor.
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let c = a.reshape((1, 6))?;
assert_eq!(c.shape().dims(), &[1, 6]);
let c = a.reshape((3, 2))?;
assert_eq!(c.shape().dims(), &[3, 2]);
let c = a.reshape((2, (), 1))?;
assert_eq!(c.shape().dims(), &[2, 3, 1]);
pub fn squeeze<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn squeeze<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Creates a new tensor with the specified dimension removed if its size was one.
let a = Tensor::zeros((2, 3, 1), DType::F32, &Device::Cpu)?;
let c = a.squeeze(2)?;
assert_eq!(c.shape().dims(), &[2, 3]);
let c = a.squeeze(D::Minus1)?;
assert_eq!(c.shape().dims(), &[2, 3]);
pub fn unsqueeze<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn unsqueeze<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Creates a new tensor with a dimension of size one inserted at the specified position.
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let c = a.unsqueeze(0)?;
assert_eq!(c.shape().dims(), &[1, 2, 3]);
let c = a.unsqueeze(D::Minus1)?;
assert_eq!(c.shape().dims(), &[2, 3, 1]);
pub fn stack<A, D>(args: &[A], dim: D) -> Result<Tensor, Error>
pub fn stack<A, D>(args: &[A], dim: D) -> Result<Tensor, Error>
Stacks two or more tensors along a particular dimension.
All tensors must have the same rank, and the output has one additional rank
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let b = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let c = Tensor::stack(&[&a, &b], 0)?;
assert_eq!(c.shape().dims(), &[2, 2, 3]);
let c = Tensor::stack(&[&a, &b], 2)?;
assert_eq!(c.shape().dims(), &[2, 3, 2]);
pub fn pad_with_zeros<D>(
&self,
dim: D,
left: usize,
right: usize,
) -> Result<Tensor, Error>where
D: Dim,
pub fn pad_with_zeros<D>(
&self,
dim: D,
left: usize,
right: usize,
) -> Result<Tensor, Error>where
D: Dim,
Pad the input tensor using 0s along dimension dim
. This adds left
elements before the
input tensor values and right
elements after.
pub fn pad_with_same<D>(
&self,
dim: D,
left: usize,
right: usize,
) -> Result<Tensor, Error>where
D: Dim,
pub fn pad_with_same<D>(
&self,
dim: D,
left: usize,
right: usize,
) -> Result<Tensor, Error>where
D: Dim,
Pad the input tensor using same values along dimension dim
. This adds left
elements before the
input tensor values and right
elements after.
pub fn apply<M>(&self, m: &M) -> Result<Tensor, Error>where
M: Module,
pub fn apply<M>(&self, m: &M) -> Result<Tensor, Error>where
M: Module,
Run the forward
method of m
on self
.
pub fn apply_t<M>(&self, m: &M, train: bool) -> Result<Tensor, Error>where
M: ModuleT,
pub fn apply_t<M>(&self, m: &M, train: bool) -> Result<Tensor, Error>where
M: ModuleT,
Run the forward
method of m
on self
.
pub fn storage_and_layout(&self) -> (RwLockReadGuard<'_, Storage>, &Layout)
pub fn storage_and_layout(&self) -> (RwLockReadGuard<'_, Storage>, &Layout)
The storage used by this tensor, together with the layout to use to access it safely.
pub fn normalize_axis(&self, axis: i64) -> Result<usize, Error>
pub fn normalize_axis(&self, axis: i64) -> Result<usize, Error>
Normalize a ‘relative’ axis value: positive values are kept, negative values means counting the dimensions from the back.
pub fn tril2(n: usize, dtype: DType, device: &Device) -> Result<Tensor, Error>
pub fn tril2(n: usize, dtype: DType, device: &Device) -> Result<Tensor, Error>
Returns a lower triangular matrix of ones of size n by n.
pub fn triu2(n: usize, dtype: DType, device: &Device) -> Result<Tensor, Error>
pub fn triu2(n: usize, dtype: DType, device: &Device) -> Result<Tensor, Error>
Returns an upper triangular matrix of ones of size n by n.
pub fn eye(n: usize, dtype: DType, device: &Device) -> Result<Tensor, Error>
pub fn eye(n: usize, dtype: DType, device: &Device) -> Result<Tensor, Error>
Returns a matrix with a diagonal of ones of size n by n.
pub fn cumsum<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn cumsum<D>(&self, dim: D) -> Result<Tensor, Error>where
D: Dim,
Returns the cumulative sum of elements of the input tensor summed over the specified dimension.
This operation is most efficient when dim is the last dimension of the tensor.
pub fn log_sum_exp<D>(&self, sum_dims: D) -> Result<Tensor, Error>where
D: Dims,
pub fn log_sum_exp<D>(&self, sum_dims: D) -> Result<Tensor, Error>where
D: Dims,
Returns log(sum(exp(tensor), dim)).
pub fn broadcast_pow(&self, rhs: &Tensor) -> Result<Tensor, Error>
pub fn broadcast_pow(&self, rhs: &Tensor) -> Result<Tensor, Error>
Broadcasting version of pow
.
§impl Tensor
impl Tensor
pub fn cat<A, D>(args: &[A], dim: D) -> Result<Tensor, Error>
pub fn cat<A, D>(args: &[A], dim: D) -> Result<Tensor, Error>
Concatenates two or more tensors along a particular dimension.
All tensors must of the same rank, and the output will have the same rank
let a = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let b = Tensor::zeros((2, 3), DType::F32, &Device::Cpu)?;
let c = Tensor::cat(&[&a, &b], 0)?;
assert_eq!(c.shape().dims(), &[4, 3]);
let c = Tensor::cat(&[&a, &b], 1)?;
assert_eq!(c.shape().dims(), &[2, 6]);
pub fn slice_set<D>(
&self,
src: &Tensor,
dim: D,
offset: usize,
) -> Result<(), Error>where
D: Dim,
pub fn slice_set<D>(
&self,
src: &Tensor,
dim: D,
offset: usize,
) -> Result<(), Error>where
D: Dim,
Set the values on self
using values from src
. The copy starts at the specified
offset
for the target dimension dim
on self
.
self
and src
must have the same shape except on dimension dim
where the self
size
has to be greater than or equal to offset
plus the src
size.
Note that this modifies self
in place and as such is not compatibel with
back-propagation.
§impl Tensor
impl Tensor
pub fn slice_assign(
&self,
ranges: &[&dyn RangeBound],
src: &Tensor,
) -> Result<Tensor, Error>
pub fn slice_assign( &self, ranges: &[&dyn RangeBound], src: &Tensor, ) -> Result<Tensor, Error>
Returns a copy of self
where the values within ranges
have been replaced with the
content of src
. This is analogous to slice asignment in torch
.
§Example
use candle_core::{Device, Tensor};
let dev = Device::Cpu;
let tensor = Tensor::arange(0u32, 4 * 5, &dev)?.reshape((4, 5))?;
let src = Tensor::arange(100u32, (2 * 3) + 100, &dev)?.reshape((3, 2))?;
let out = tensor.slice_assign(&[&(..3), &(3..5)], &src)?;
assert_eq!(
out.to_vec2::<u32>()?,
&[
[0, 1, 2, 100, 101],
[5, 6, 7, 102, 103],
[10, 11, 12, 104, 105],
[15, 16, 17, 18, 19]
]
);
pub fn scatter_add<D>(
&self,
indexes: &Tensor,
source: &Tensor,
dim: D,
) -> Result<Tensor, Error>where
D: Dim,
pub fn slice_scatter<D>(
&self,
src: &Tensor,
dim: D,
start: usize,
) -> Result<Tensor, Error>where
D: Dim,
pub fn slice_scatter<D>(
&self,
src: &Tensor,
dim: D,
start: usize,
) -> Result<Tensor, Error>where
D: Dim,
Embeds the values of the src
tensor into the self
tensor on the specified dimension.
pub fn slice_scatter0(
&self,
src: &Tensor,
start: usize,
) -> Result<Tensor, Error>
pub fn slice_scatter0( &self, src: &Tensor, start: usize, ) -> Result<Tensor, Error>
Embeds the values of the src
tensor into the self
tensor on the first dimension.
pub fn index_add<D>(
&self,
indexes: &Tensor,
source: &Tensor,
dim: D,
) -> Result<Tensor, Error>where
D: Dim,
pub fn index_add<D>(
&self,
indexes: &Tensor,
source: &Tensor,
dim: D,
) -> Result<Tensor, Error>where
D: Dim,
Accumulate element from source
at indexes indexes
and add them to self
.
pub fn gather<D>(&self, indexes: &Tensor, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn gather<D>(&self, indexes: &Tensor, dim: D) -> Result<Tensor, Error>where
D: Dim,
Gather values across the target dimension.
§Arguments
self
- The input tensor.indexes
- The indices of elements to gather, this should have the same shape asself
but can have a different number of elements on the target dimension.dim
- the target dimension.
The resulting tensor has the same shape as indexes
and use values from self
indexed on
dimension dim
by the values in indexes
.
pub fn index_select<D>(&self, indexes: &Tensor, dim: D) -> Result<Tensor, Error>where
D: Dim,
pub fn index_select<D>(&self, indexes: &Tensor, dim: D) -> Result<Tensor, Error>where
D: Dim,
Select values for the input tensor at the target indexes across the specified dimension.
The indexes
is argument is an int tensor with a single dimension.
The output has the same number of dimension as the self
input. The target dimension of
the output has length the length of indexes
and the values are taken from self
using
the index from indexes
. Other dimensions have the same number of elements as the input
tensor.
Trait Implementations§
source§impl<'a> ApplyTensorTransforms<'a> for Tensor
impl<'a> ApplyTensorTransforms<'a> for Tensor
§impl<A> IndexOp<(A,)> for Tensorwhere
A: Into<TensorIndexer>,
impl<A> IndexOp<(A,)> for Tensorwhere
A: Into<TensorIndexer>,
§fn i(&self, _: (A,)) -> Result<Tensor, Error>
fn i(&self, _: (A,)) -> Result<Tensor, Error>
use candle_core::{Tensor, DType, Device, IndexOp};
let a = Tensor::new(&[
[0f32, 1.],
[2. , 3.],
[4. , 5.]
], &Device::Cpu)?;
let b = a.i((0,))?;
assert_eq!(b.shape().dims(), &[2]);
assert_eq!(b.to_vec1::<f32>()?, &[0., 1.]);
let c = a.i((..2,))?;
assert_eq!(c.shape().dims(), &[2, 2]);
assert_eq!(c.to_vec2::<f32>()?, &[
[0., 1.],
[2., 3.]
]);
let d = a.i((1..,))?;
assert_eq!(d.shape().dims(), &[2, 2]);
assert_eq!(d.to_vec2::<f32>()?, &[
[2., 3.],
[4., 5.]
]);
§impl<A, B> IndexOp<(A, B)> for Tensor
impl<A, B> IndexOp<(A, B)> for Tensor
§fn i(&self, _: (A, B)) -> Result<Tensor, Error>
fn i(&self, _: (A, B)) -> Result<Tensor, Error>
use candle_core::{Tensor, DType, Device, IndexOp};
let a = Tensor::new(&[[0f32, 1., 2.], [3., 4., 5.], [6., 7., 8.]], &Device::Cpu)?;
let b = a.i((1, 0))?;
assert_eq!(b.to_vec0::<f32>()?, 3.);
let c = a.i((..2, 1))?;
assert_eq!(c.shape().dims(), &[2]);
assert_eq!(c.to_vec1::<f32>()?, &[1., 4.]);
let d = a.i((2.., ..))?;
assert_eq!(c.shape().dims(), &[2]);
assert_eq!(c.to_vec1::<f32>()?, &[1., 4.]);
§impl<A, B, C, D> IndexOp<(A, B, C, D)> for Tensor
impl<A, B, C, D> IndexOp<(A, B, C, D)> for Tensor
§fn i(&self, _: (A, B, C, D)) -> Result<Tensor, Error>
fn i(&self, _: (A, B, C, D)) -> Result<Tensor, Error>
see [TensorIndex#method.i]
§impl<A, B, C, D, E> IndexOp<(A, B, C, D, E)> for Tensor
impl<A, B, C, D, E> IndexOp<(A, B, C, D, E)> for Tensor
§fn i(&self, _: (A, B, C, D, E)) -> Result<Tensor, Error>
fn i(&self, _: (A, B, C, D, E)) -> Result<Tensor, Error>
see [TensorIndex#method.i]
§impl<A, B, C, D, E, F> IndexOp<(A, B, C, D, E, F)> for Tensor
impl<A, B, C, D, E, F> IndexOp<(A, B, C, D, E, F)> for Tensor
§fn i(&self, _: (A, B, C, D, E, F)) -> Result<Tensor, Error>
fn i(&self, _: (A, B, C, D, E, F)) -> Result<Tensor, Error>
see [TensorIndex#method.i]
§impl<A, B, C, D, E, F, G> IndexOp<(A, B, C, D, E, F, G)> for Tensor
impl<A, B, C, D, E, F, G> IndexOp<(A, B, C, D, E, F, G)> for Tensor
§fn i(&self, _: (A, B, C, D, E, F, G)) -> Result<Tensor, Error>
fn i(&self, _: (A, B, C, D, E, F, G)) -> Result<Tensor, Error>
see [TensorIndex#method.i]
§impl<T> IndexOp<T> for Tensorwhere
T: Into<TensorIndexer>,
impl<T> IndexOp<T> for Tensorwhere
T: Into<TensorIndexer>,
§fn i(&self, index: T) -> Result<Tensor, Error>
fn i(&self, index: T) -> Result<Tensor, Error>
use candle_core::{Tensor, DType, Device, IndexOp};
let a = Tensor::new(&[
[0., 1.],
[2., 3.],
[4., 5.]
], &Device::Cpu)?;
let b = a.i(0)?;
assert_eq!(b.shape().dims(), &[2]);
assert_eq!(b.to_vec1::<f64>()?, &[0., 1.]);
let c = a.i(..2)?;
assert_eq!(c.shape().dims(), &[2, 2]);
assert_eq!(c.to_vec2::<f64>()?, &[
[0., 1.],
[2., 3.]
]);
let d = a.i(1..)?;
assert_eq!(d.shape().dims(), &[2, 2]);
assert_eq!(d.to_vec2::<f64>()?, &[
[2., 3.],
[4., 5.]
]);
§impl TensorOrScalar for &Tensor
impl TensorOrScalar for &Tensor
fn to_tensor_scalar(self) -> Result<TensorScalar, Error>
Auto Trait Implementations§
impl Freeze for Tensor
impl !RefUnwindSafe for Tensor
impl Send for Tensor
impl Sync for Tensor
impl Unpin for Tensor
impl !UnwindSafe for Tensor
Blanket Implementations§
§impl<'short, T, Target> AsGeneralizedRef<'short, &'short Target> for T
impl<'short, T, Target> AsGeneralizedRef<'short, &'short Target> for T
fn as_generalized_ref(&'short self) -> &'short Target
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Downcast for Twhere
T: AsAny + ?Sized,
impl<T> Downcast for Twhere
T: AsAny + ?Sized,
§fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
Any
.§fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
Any
.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more