asarray

Function asarray 

Source
pub fn asarray<Args, Inp>(param: Args) -> Args::Out
where Args: AsArrayAPI<Inp>,
Expand description

Convert the input to an array.

This function is overloaded.

Row/Column Major Notice

When passing shape into this function, the layout of output tensor will be different on default orders (RowMajor and ColMajor) of device.

Note that this function always returns a dynamic-dimensional (IxD) tensor. To convert it into a fixed-dimensional tensor, you can use .into_dim::<D>() method on the output tensor without explicit data copy.

§Overloads Table

§Output owned tensor Tensor

Input vector Vec<T> as raw data:

  • asarray((input: Vec<T>, layout: Layout<D>, device: &B)) -> Tensor<T, B, IxD>
  • asarray((input: Vec<T>, shape: D, device: &B)) -> Tensor<T, B, IxD>
  • asarray((input: Vec<T>, device: &B)) -> Tensor<T, B, IxD>
  • asarray((input: Vec<T>, layout: Layout<D>)) -> Tensor<T, DeviceCpu, IxD>
  • asarray((input: Vec<T>, shape: D)) -> Tensor<T, DeviceCpu, IxD>
  • asarray(input: Vec<T>) -> Tensor<T, DeviceCpu, IxD>

Input scalar T as raw data:

  • asarray((input: T, device: &B)) -> Tensor<T, B, IxD>
  • asarray(input: T) -> Tensor<T, DeviceCpu, IxD>

Input tensor as raw data and change its layout:

  • asarray((input: &TensorAny<R, T, B, D>, order: TensorIterOrder)) -> Tensor<T, B, D>
  • asarray((input: TensorView<'_, T, B, D>, order: TensorIterOrder)) -> Tensor<T, B, D>
  • asarray((input: Tensor<T, B, D>, order: TensorIterOrder)) -> Tensor<T, B, D>
  • asarray(input: &TensorAny<R, T, B, D>) -> Tensor<T, B, D>
  • asarray(input: TensorView<'_, T, B, D>) -> Tensor<T, B, D>
  • asarray(input: Tensor<T, B, D>) -> Tensor<T, B, D>

§Output tensor view TensorView

  • asarray((input: &[T], layout: Layout<D>, device: &B)) -> TensorView<'a, T, B, IxD>
  • asarray((input: &[T], shape: D, device: &B)) -> TensorView<'a, T, B, IxD>
  • asarray((input: &[T], device: &B)) -> TensorView<'a, T, B, IxD>
  • asarray((input: &[T], layout: Layout<D>)) -> TensorView<'a, T, DeviceCpu, IxD>
  • asarray((input: &[T], shape: D)) -> TensorView<'a, T, DeviceCpu, IxD>
  • asarray(input: &[T]) -> TensorView<'a, T, DeviceCpu, IxD>

Also, overloads for &Vec<T> that behave the same as &[T].

§Output mutable tensor view TensorMut

All overloads for &[T] and &Vec<T> above also have mutable versions for &mut [T] and &mut Vec<T>, which output TensorMut<'a, T, B, IxD> and TensorMut<'a, T, DeviceCpu, IxD>, respectively.

§Examples

§Vector as input

The most usual usage is to convert a vector into a tensor. You can also specify the shape / layout and device.

The following example assumes that the device’s default order is row-major. The input shape [2, 3] corresponds to a row-major layout [2, 3].c().

use rstsr::prelude::*;
let mut device = DeviceCpu::default();
device.set_default_order(RowMajor);

// vector as input, row-major layout by default
let input = vec![1, 2, 3, 4, 5, 6];
let a = rt::asarray((input, [2, 3], &device));
println!("{a:?}");
// [[1, 2, 3],
//  [4, 5, 6]]
// 2-Dim (dyn), contiguous: Cc, shape: [2, 3], stride: [3, 1], offset: 0

If you want to use column-major layout, you can specify the layout explicitly. But be cautious that row-major and column-major layouts will lead to different arrangements of data.

// vector as input, column-major layout
let input = vec![1, 2, 3, 4, 5, 6];
let a = rt::asarray((input, [2, 3].f(), &device));
println!("{a:?}");
// [[ 1 3 5]
//  [ 2 4 6]]
// 2-Dim (dyn), contiguous: Ff, shape: [2, 3], stride: [1, 2], offset: 0

Also, if the device’s default order is column-major, the shape input ([2, 3]) will also lead to a column-major layout ([2, 3].f()):

// vector as input, column layout by default
device.set_default_order(ColMajor);
let input = vec![1, 2, 3, 4, 5, 6];
let a = rt::asarray((input, [2, 3], &device));
println!("{a:?}");
// [[ 1 3 5]
//  [ 2 4 6]]
// 2-Dim (dyn), contiguous: Ff, shape: [2, 3], stride: [1, 2], offset: 0

Finally, you can omit the device argument to use the default CPU device, and omit the layout/shape to get an 1-D tensor:

let input = vec![1, 2, 3, 4, 5, 6];
let a = rt::asarray(input);;
println!("{a:?}");
// [ 1 2 3 4 5 6]
// 1-Dim (dyn), contiguous: Cc, shape: [6], stride: [1], offset: 0

§&[T] or &mut [T] as input

You can also convert a slice into a tensor view. Please note, asarray accepts &[T] and &Vec<T>, but do not accept other slice-like types such as &[T; N]. You may need to convert them by .as_ref() first.

// Slice &[T] as input
let input = &[1, 2, 3, 4, 5, 6];
let a = rt::asarray((input.as_ref(), [2, 3].c(), &device));
println!("{a:?}");
// [[ 1 2 3]
//  [ 4 5 6]]

Also, mutable slices &mut [T] and &mut Vec<T> are supported. You can modify the original data via the output tensor mutable view.

// Slice &mut [T] as input
let mut input = vec![1, 2, 3, 4, 5, 6];
let mut a = rt::asarray((&mut input, [2, 3].c(), &device));
// change `input` via tensor view `a`
a[[0, 0]] = 10;
println!("{a:2?}");
// [[ 10  2  3]
//  [  4  5  6]]
println!("{input:?}");
// [10, 2, 3, 4, 5, 6]

You can also specify a sub-view via layout:

let input = (0..30).collect::<Vec<i32>>();
let layout = Layout::new([3, 2], [2, 7], 5).unwrap();
let a = rt::asarray((&input, layout, &device));
println!("{a:2?}");
// [[  5 12]
//  [  7 14]
//  [  9 16]]

Finally, you can also omit the device argument to use the default CPU device.

§Scalar as input

You can also convert a scalar into a tensor with zero dimensions.

let a = rt::asarray((42, &device));
println!("{a:?}");
// 42
// 0-Dim (dyn), contiguous: CcFf, shape: [], stride: [], offset: 0

§Tensor or its view as input

You can convert a tensor or its view into a new tensor with specified iteration order (layout).

This is similar to the optional argument order in NumPy’s np.asarray function. The converted tensor behaves exactly the same to the input tensor, but may have different memory layout.

To specify the order, you may pass TensorIterOrder along with the input tensor:

// Generate a strided, non-row-or-col-prefer tensor view
let a_raw = rt::arange((96, &device)).into_shape([4, 6, 4]);
let a = a_raw.i((..2, slice!(2, 6, 2), 2..)).into_transpose([1, 0, 2]);
println!("{a:2?}");
// [[[10, 11], [34, 35]], [[18, 19], [42, 43]]]
// shape: [2, 2, 2], stride: [8, 24, 1], offset: 10

// shrink useful memory space, with preserved layout
let b = rt::asarray((&a, TensorIterOrder::K));
println!("{b:2?}");
// shape: [2, 2, 2], stride: [2, 4, 1], offset: 0

// convert to row-major layout
let b = rt::asarray((&a, TensorIterOrder::C));
println!("{b:2?}");
// shape: [2, 2, 2], stride: [4, 2, 1], offset: 0

// convert to column-major layout
let b = rt::asarray((&a, TensorIterOrder::F));
println!("{b:2?}");
// shape: [2, 2, 2], stride: [1, 2, 4], offset: 0

§See also

§Similar function from other crates/libraries

§Variants of this function

  • asarray_f: Fallible version of this function.