pub struct Tensor<T> { /* private fields */ }
Expand description
Encapsulates a matrix for transfer between nodes
Implementations§
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
pub fn from_vec(dimensions: Vec2, vector: Vec<T>) -> Tensor<T>
Sourcepub fn to_flattened(self) -> Vec<T>
pub fn to_flattened(self) -> Vec<T>
Gives ownership to the matrix
buffer
§Example
let tensor = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect()));
let tensor = tensor.to_flattened();
let result = vec![0, 1, 2, 3, 4, 5];
for (&i, &j) in tensor.iter().zip(result.iter()) {
assert_eq!(i, j);
}
pub fn buffer(&self) -> &Vec<T>
Sourcepub fn get_convolutions()
pub fn get_convolutions()
max_num_strides = (dim - width) / stride + 1
Source§impl<T> Tensor<T>where
T: Copy,
impl<T> Tensor<T>where
T: Copy,
Sourcepub fn transpose(&self) -> Tensor<T>
pub fn transpose(&self) -> Tensor<T>
Transpose Tensor
§Example
let tensor = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect()));
let tensor2 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect()));
let tensor2 = tensor2.transpose();
let tensor3 = tensor * tensor2;
let tensor3 = tensor3.to_flattened();
assert_eq!(tensor3[2], 14);
pub fn get(&self, vec2: Vec2) -> T
Source§impl<'a, T> Tensor<T>
impl<'a, T> Tensor<T>
Sourcepub fn product(&self, rhs: &'a Tensor<T>) -> Tensor<T>
pub fn product(&self, rhs: &'a Tensor<T>) -> Tensor<T>
Hadamard Product of Tensors by reference
§Arguments
self
- this tensor referencerhs
- another tensor reference
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let tensor2 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).rev().collect()));
let tensor3 = tensor1.product(&tensor2);
let tensor1 = tensor1.to_flattened();
let tensor2 = tensor2.to_flattened();
let tensor3 = tensor3.to_flattened();
assert_eq!(tensor1[2], 2.0);
assert_eq!(tensor2[2], 3.0);
assert_eq!(tensor3[2], 6.0);
Trait Implementations§
Source§impl<'a, 'b, T> Add<&'b T> for &'a Tensor<T>
impl<'a, 'b, T> Add<&'b T> for &'a Tensor<T>
Source§fn add(self, rhs: &'b T) -> Tensor<T>
fn add(self, rhs: &'b T) -> Tensor<T>
Add Tensor
and a constant
§Arguments
self
- this tensor referencerhs
- a constant reference
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let float = 1.0;
let tensor2 = &tensor1 + &float;
let tensor1 = tensor1.to_flattened();
let tensor2 = tensor2.to_flattened();
assert_eq!(tensor1[0], 0.0);
assert_eq!(tensor2[0], 1.0);
assert_eq!(float, 1.0);
Source§impl<'a, 'b, T> Add<&'b Tensor<T>> for &'a Tensor<T>
impl<'a, 'b, T> Add<&'b Tensor<T>> for &'a Tensor<T>
Source§fn add(self, rhs: &'b Tensor<T>) -> Tensor<T>
fn add(self, rhs: &'b Tensor<T>) -> Tensor<T>
Add Tensor
s by reference
§Arguments
self
- this tensor referencerhs
- another tensor reference
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let tensor2 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).rev().collect()));
let tensor3 = &tensor1 + &tensor2;
let tensor1 = tensor1.to_flattened();
let tensor2 = tensor2.to_flattened();
let tensor3 = tensor3.to_flattened();
assert_eq!(tensor1[0], 0.0);
assert_eq!(tensor2[0], 5.0);
assert_eq!(tensor3[0], 5.0);
Source§impl<T> Add<T> for Tensor<T>
impl<T> Add<T> for Tensor<T>
Source§fn add(self, rhs: T) -> Tensor<T>
fn add(self, rhs: T) -> Tensor<T>
Add Tensor
and a constant
§Arguments
self
- thisTensor
rhs
- a constant
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let float = 1.0;
let tensor2 = tensor1 + float;
let tensor2 = tensor2.to_flattened();
assert_eq!(tensor2[0], 1.0);
Source§impl<T> Add for Tensor<T>
impl<T> Add for Tensor<T>
Source§fn add(self, rhs: Tensor<T>) -> Tensor<T>
fn add(self, rhs: Tensor<T>) -> Tensor<T>
Add Tensor
s
§Arguments
self
- this tensorrhs
- another tensor
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let tensor2 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).rev().collect()));
let tensor3 = tensor1 + tensor2;
let tensor3 = tensor3.to_flattened();
assert_eq!(tensor3[0], 5.0);
Source§impl<'a, 'b, T> Mul<&'b T> for &'a Tensor<T>
impl<'a, 'b, T> Mul<&'b T> for &'a Tensor<T>
Source§fn mul(self, rhs: &'b T) -> Tensor<T>
fn mul(self, rhs: &'b T) -> Tensor<T>
Multiply Tensor
and a constant
§Arguments
self
- this tensor referencerhs
- a constant reference
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let float = 2.0;
let tensor2 = &tensor1 * &float;
let tensor1 = tensor1.to_flattened();
let tensor2 = tensor2.to_flattened();
assert_eq!(tensor1[2], 2.0);
assert_eq!(tensor2[2], 4.0);
assert_eq!(float, 2.0);
Source§impl<'a, 'b, T> Mul<&'b Tensor<T>> for &'a Tensor<T>
impl<'a, 'b, T> Mul<&'b Tensor<T>> for &'a Tensor<T>
Source§fn mul(self, rhs: &'b Tensor<T>) -> Tensor<T>
fn mul(self, rhs: &'b Tensor<T>) -> Tensor<T>
Multiply Tensor
s` by reference
§Arguments
self
- this tensor referencerhs
- another tensor reference
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let tensor2 = ktensor::Tensor::new(ktensor::math::Vec2(3, 2), ktensor::math::Matrix::new(ktensor::math::Vec2(3, 2), (0..6).map(|i| i as f64).rev().collect()));
let tensor3 = &tensor1 * &tensor2;
let tensor1 = tensor1.to_flattened();
let tensor2 = tensor2.to_flattened();
let tensor3 = tensor3.to_flattened();
assert_eq!(tensor3.len(), 4);
assert_eq!(tensor1[0], 0.0);
assert_eq!(tensor2[0], 5.0);
assert_eq!(tensor3[0], 5.0);
Source§impl<T> Mul<T> for Tensor<T>
impl<T> Mul<T> for Tensor<T>
Source§fn mul(self, rhs: T) -> Tensor<T>
fn mul(self, rhs: T) -> Tensor<T>
Multiply Tensor
and a constant
§Arguments
self
- this tensorrhs
- a constant
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let float = 2.0;
let tensor2 = tensor1 * float;
let tensor2 = tensor2.to_flattened();
assert_eq!(tensor2[2], 4.0);
Source§impl<T> Mul for Tensor<T>
impl<T> Mul for Tensor<T>
Source§fn mul(self, rhs: Tensor<T>) -> Tensor<T>
fn mul(self, rhs: Tensor<T>) -> Tensor<T>
Multiply Tensor
s`
§Arguments
self
- this tensorrhs
- another tensor
§Example
let tensor1 = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).map(|i| i as f64).collect()));
let tensor2 = ktensor::Tensor::new(ktensor::math::Vec2(3, 2), ktensor::math::Matrix::new(ktensor::math::Vec2(3, 2), (0..6).map(|i| i as f64).rev().collect()));
let tensor3 = tensor1 * tensor2;
let tensor3 = tensor3.to_flattened();
assert_eq!(tensor3.len(), 4);
assert_eq!(tensor3[0], 5.0);
Auto Trait Implementations§
impl<T> Freeze for Tensor<T>
impl<T> RefUnwindSafe for Tensor<T>where
T: RefUnwindSafe,
impl<T> Send for Tensor<T>where
T: Send,
impl<T> Sync for Tensor<T>where
T: Sync,
impl<T> Unpin for Tensor<T>where
T: Unpin,
impl<T> UnwindSafe for Tensor<T>where
T: UnwindSafe,
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