pub struct TensorView<'a> { /* private fields */ }
Expand description
A view into the original Tensor
. A view borrows part of (or even the entirety of) the
original Tensor data. It can NOT modify the data itself. It is normally produced by indexing or
slicing a Tensor.
It can be useful for creating a new Tensor from part of another one:
§Examples
use tensor_compute::{Tensor, TensorView, s};
let tensor = Tensor::from_data_and_shape(vec![1., 2., 3., 4.], vec![2, 2]);
let first_row: TensorView = tensor.slice(s![1]);
assert_eq!(first_row.shape(), &[1, 2]);
assert_eq!(first_row.to_cpu().as_contiguous_vec(), &[3., 4.]);
let new_tensor = first_row.make_contiguous(); // creates a new owned tensor from the view
assert_eq!(new_tensor.shape(), &[1, 2]);
assert_eq!(new_tensor.to_cpu().as_contiguous_vec(), &[3., 4.]);
Implementations§
Source§impl<'a> TensorView<'a>
impl<'a> TensorView<'a>
Sourcepub async fn make_contiguous_async(&self) -> Tensor
pub async fn make_contiguous_async(&self) -> Tensor
Same as TensorView::make_contiguous
, but async.
Sourcepub fn make_contiguous(&self) -> Tensor
pub fn make_contiguous(&self) -> Tensor
Copies the TensorView
into a standalone contiguous Tensor
§Examples
use tensor_compute::{Tensor, TensorView, s};
let tensor = Tensor::from_data_and_shape(vec![1., 2., 3., 4., 5., 6., 7., 8.], vec![2, 2, 2]);
let first_row: TensorView = tensor.slice(s![1]);
assert_eq!(first_row.shape(), &[1, 2, 2]);
assert_eq!(first_row.to_cpu().as_contiguous_vec(), &[5., 6., 7., 8.]);
let new_tensor = first_row.make_contiguous(); // creates a new owned tensor from the view
assert_eq!(new_tensor.shape(), &[1, 2, 2]);
assert_eq!(new_tensor.to_cpu().as_contiguous_vec(), &[5., 6., 7., 8.]);
assert!(new_tensor.slice(s![..]).compare(&first_row));
Sourcepub async fn compare_async(&self, other: &Self) -> bool
pub async fn compare_async(&self, other: &Self) -> bool
Same as Tensor::compare_async
Sourcepub fn compare(&self, other: &Self) -> bool
pub fn compare(&self, other: &Self) -> bool
Same as Tensor::compare
Sourcepub fn shape(&self) -> &VecDeque<usize>
pub fn shape(&self) -> &VecDeque<usize>
Same as Tensor::shape
Sourcepub fn strides(&self) -> &VecDeque<usize>
pub fn strides(&self) -> &VecDeque<usize>
Same as Tensor::strides
Sourcepub async fn to_cpu_async(&self) -> CpuTensor
pub async fn to_cpu_async(&self) -> CpuTensor
Same as Tensor::to_cpu_async
Sourcepub fn to_cpu(&self) -> CpuTensor
pub fn to_cpu(&self) -> CpuTensor
Same as Tensor::to_cpu
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for TensorView<'a>
impl<'a> !RefUnwindSafe for TensorView<'a>
impl<'a> Send for TensorView<'a>
impl<'a> Sync for TensorView<'a>
impl<'a> Unpin for TensorView<'a>
impl<'a> !UnwindSafe for TensorView<'a>
Blanket Implementations§
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
Mutably borrows from an owned value. Read more