pub type Tensor<T> = Value<TensorValueType<T>>;
Aliased Type§
struct Tensor<T> { /* private fields */ }
Implementations§
source§impl<T: IntoTensorElementType + Debug> Tensor<T>
impl<T: IntoTensorElementType + Debug> Tensor<T>
sourcepub fn new(allocator: &Allocator, shape: impl ToDimensions) -> Result<Tensor<T>>
pub fn new(allocator: &Allocator, shape: impl ToDimensions) -> Result<Tensor<T>>
Construct a tensor Value
in a given allocator with a given shape and datatype. The data contained in the
value will be zero-allocated on the allocation device.
This can be used to create a tensor with data on a certain device. For example, to create a tensor with pinned (CPU) memory for use with CUDA:
let allocator = Allocator::new(
&session,
MemoryInfo::new(AllocationDevice::CUDAPinned, 0, AllocatorType::Device, MemoryType::CPUInput)?
)?;
let mut img_input = Tensor::<f32>::new(&allocator, [1, 128, 128, 3])?;
sourcepub fn from_array(input: impl IntoValueTensor<Item = T>) -> Result<Tensor<T>>
pub fn from_array(input: impl IntoValueTensor<Item = T>) -> Result<Tensor<T>>
Construct a tensor Value
from an array of data.
Tensor Value
s can be created from:
- (with feature
ndarray
) a shared reference to andarray::CowArray
(&CowArray<'_, T, D>
); - (with feature
ndarray
) a mutable/exclusive reference to anndarray::ArcArray
(&mut ArcArray<T, D>
); - (with feature
ndarray
) an ownedndarray::Array
; - (with feature
ndarray
) a borrowed view of another array, as anndarray::ArrayView
(ArrayView<'_, T, D>
); - a tuple of
(dimensions, data)
where:dimensions
is one ofVec<I>
,[I]
or&[I]
, whereI
isi64
orusize
;- and
data
is one ofVec<T>
,Box<[T]>
,Arc<Box<[T]>>
, or&[T]
.
// Create a tensor from a raw data vector
let value = Value::from_array(([1usize, 2, 3], vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0].into_boxed_slice()))?;
// Create a tensor from an `ndarray::Array`
#[cfg(feature = "ndarray")]
let value = Value::from_array(ndarray::Array4::<f32>::zeros((1, 16, 16, 3)))?;
Creating string tensors requires a separate method; see Value::from_string_array
.
Note that data provided in an ndarray
may be copied in some circumstances:
&CowArray<'_, T, D>
will always be copied regardless of whether it is uniquely owned or borrowed.&mut ArcArray<T, D>
andArray<T, D>
will be copied only if the data is not in a contiguous layout (which is the case after most reshape operations)ArrayView<'_, T, D>
will always be copied.
Raw data provided as a Arc<Box<[T]>>
, Box<[T]>
, or Vec<T>
will never be copied. Raw data is expected to be
in standard, contigous layout.
source§impl<T: IntoTensorElementType + Debug> Tensor<T>
impl<T: IntoTensorElementType + Debug> Tensor<T>
sourcepub fn extract_tensor(&self) -> ArrayViewD<'_, T>
Available on crate feature ndarray
only.
pub fn extract_tensor(&self) -> ArrayViewD<'_, T>
ndarray
only.Extracts the underlying data into a read-only ndarray::ArrayView
.
let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let tensor = Tensor::from_array(array.view())?;
let extracted = tensor.extract_tensor();
assert_eq!(array.into_dyn(), extracted);
sourcepub fn extract_tensor_mut(&mut self) -> ArrayViewMutD<'_, T>
Available on crate feature ndarray
only.
pub fn extract_tensor_mut(&mut self) -> ArrayViewMutD<'_, T>
ndarray
only.Extracts the underlying data into a mutable ndarray::ArrayViewMut
.
let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let mut tensor = Tensor::from_array(array.view())?;
let mut extracted = tensor.extract_tensor_mut();
extracted[[0, 0, 0, 1]] = 0.0;
let mut array = array.into_dyn();
assert_ne!(array, extracted);
array[[0, 0, 0, 1]] = 0.0;
assert_eq!(array, extracted);
sourcepub fn extract_raw_tensor(&self) -> (Vec<i64>, &[T])
pub fn extract_raw_tensor(&self) -> (Vec<i64>, &[T])
Extracts the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and an immutable view into its data.
let array = vec![1_i64, 2, 3, 4, 5];
let tensor = Tensor::from_array(([array.len()], array.clone().into_boxed_slice()))?;
let (extracted_shape, extracted_data) = tensor.extract_raw_tensor();
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
sourcepub fn extract_raw_tensor_mut(&mut self) -> (Vec<i64>, &mut [T])
pub fn extract_raw_tensor_mut(&mut self) -> (Vec<i64>, &mut [T])
Extracts the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and a mutable view into its data.
let array = vec![1_i64, 2, 3, 4, 5];
let tensor = Tensor::from_array(([array.len()], array.clone().into_boxed_slice()))?;
let (extracted_shape, extracted_data) = tensor.extract_raw_tensor();
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
source§impl<T: IntoTensorElementType + Debug> Tensor<T>
impl<T: IntoTensorElementType + Debug> Tensor<T>
sourcepub fn upcast_ref(&self) -> DynTensorRef<'_>
pub fn upcast_ref(&self) -> DynTensorRef<'_>
sourcepub fn upcast_mut(&mut self) -> DynTensorRefMut<'_>
pub fn upcast_mut(&mut self) -> DynTensorRefMut<'_>
Trait Implementations§
source§impl<T: IntoTensorElementType + Clone + Debug, const N: usize> IndexMut<[i64; N]> for Tensor<T>
impl<T: IntoTensorElementType + Clone + Debug, const N: usize> IndexMut<[i64; N]> for Tensor<T>
source§impl<'i, 'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&'i ArrayBase<CowRepr<'v, T>, D>> for Tensor<T>where
'i: 'v,
Available on crate feature ndarray
only.
impl<'i, 'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&'i ArrayBase<CowRepr<'v, T>, D>> for Tensor<T>where
'i: 'v,
ndarray
only.source§impl<T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&mut ArrayBase<OwnedArcRepr<T>, D>> for Tensor<T>
Available on crate feature ndarray
only.
impl<T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&mut ArrayBase<OwnedArcRepr<T>, D>> for Tensor<T>
ndarray
only.source§impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, &[T])> for Tensor<T>
impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, &[T])> for Tensor<T>
source§impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Arc<Box<[T]>>)> for Tensor<T>
impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Arc<Box<[T]>>)> for Tensor<T>
source§impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Box<[T]>)> for Tensor<T>
impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Box<[T]>)> for Tensor<T>
source§impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Vec<T>)> for Tensor<T>
impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Vec<T>)> for Tensor<T>
source§impl<T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<ArrayBase<OwnedRepr<T>, D>> for Tensor<T>
Available on crate feature ndarray
only.
impl<T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<ArrayBase<OwnedRepr<T>, D>> for Tensor<T>
ndarray
only.