Struct Tensor

Source
pub struct Tensor<T> { /* private fields */ }
Expand description

Encapsulates a matrix for transfer between nodes

Implementations§

Source§

impl<T> Tensor<T>

Source

pub fn new(dimensions: Vec2, matrix: Matrix<T>) -> Tensor<T>

returns a new Tensor

§Arguments
  • dimensions - dimensions of the matrix
  • matrix - Matrix encapsulated by the Tensor
§Example
let tensor = ktensor::Tensor::new(ktensor::math::Vec2(2, 3), ktensor::math::Matrix::new(ktensor::math::Vec2(2, 3), (0..6).collect()));
Source

pub fn from_vec(dimensions: Vec2, vector: Vec<T>) -> Tensor<T>

Source

pub fn dim(&self) -> Vec2

Returns the dimensions of the Tensor

Source

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);
}
Source

pub fn buffer(&self) -> &Vec<T>

Source

pub fn get_convolutions()

max_num_strides = (dim - width) / stride + 1

Source§

impl<T> Tensor<T>
where T: Copy,

Source

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);
Source

pub fn get(&self, vec2: Vec2) -> T

Source§

impl Tensor<f64>

Source

pub fn from_gaussian(dimensions: Vec2, input_dim: usize) -> Tensor<f64>

generates a random valued Tensor with a standard deviation of 1

§Arguments
  • dimensions - dimensions of Tensor
§Example
let tensor = ktensor::Tensor::<f64>::from_gaussian(ktensor::math::Vec2(5, 5), 2);
Source§

impl Tensor<f32>

Source

pub fn from_gaussian(dimensions: Vec2, input_dim: usize) -> Tensor<f32>

generates a random valued matrix with a standard deviation of 1

§Arguments
  • dimensions - dimensions of Tensor
§Example
let tensor = ktensor::Tensor::<f32>::from_gaussian(ktensor::math::Vec2(5, 5), 2);
Source§

impl<'a, T> Tensor<T>
where T: Mul<Output = T> + Copy,

Source

pub fn product(&self, rhs: &'a Tensor<T>) -> Tensor<T>

Hadamard Product of Tensors by reference

§Arguments
  • self - this tensor reference
  • rhs - 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>
where T: Add<Output = T> + Copy,

Source§

fn add(self, rhs: &'b T) -> Tensor<T>

Add Tensor and a constant

§Arguments
  • self - this tensor reference
  • rhs - 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§

type Output = Tensor<T>

The resulting type after applying the + operator.
Source§

impl<'a, 'b, T> Add<&'b Tensor<T>> for &'a Tensor<T>
where T: Add<Output = T> + Copy,

Source§

fn add(self, rhs: &'b Tensor<T>) -> Tensor<T>

Add Tensors by reference

§Arguments
  • self - this tensor reference
  • rhs - 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§

type Output = Tensor<T>

The resulting type after applying the + operator.
Source§

impl<T> Add<T> for Tensor<T>
where T: Add<Output = T> + Copy,

Source§

fn add(self, rhs: T) -> Tensor<T>

Add Tensor and a constant

§Arguments
  • self - this Tensor
  • 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§

type Output = Tensor<T>

The resulting type after applying the + operator.
Source§

impl<T> Add for Tensor<T>
where T: Add<Output = T> + Copy,

Source§

fn add(self, rhs: Tensor<T>) -> Tensor<T>

Add Tensors

§Arguments
  • self - this tensor
  • rhs - 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§

type Output = Tensor<T>

The resulting type after applying the + operator.
Source§

impl<T> Clone for Tensor<T>
where T: Copy,

Source§

fn clone(&self) -> Tensor<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, 'b, T> Mul<&'b T> for &'a Tensor<T>
where T: Mul<Output = T> + Copy,

Source§

fn mul(self, rhs: &'b T) -> Tensor<T>

Multiply Tensor and a constant

§Arguments
  • self - this tensor reference
  • rhs - 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§

type Output = Tensor<T>

The resulting type after applying the * operator.
Source§

impl<'a, 'b, T> Mul<&'b Tensor<T>> for &'a Tensor<T>
where T: Mul<Output = T> + Add<Output = T> + Copy,

Source§

fn mul(self, rhs: &'b Tensor<T>) -> Tensor<T>

Multiply Tensors` by reference

§Arguments
  • self - this tensor reference
  • rhs - 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§

type Output = Tensor<T>

The resulting type after applying the * operator.
Source§

impl<T> Mul<T> for Tensor<T>
where T: Mul<Output = T> + Copy,

Source§

fn mul(self, rhs: T) -> Tensor<T>

Multiply Tensor and a constant

§Arguments
  • self - this tensor
  • 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 = 2.0;
let tensor2 = tensor1 * float;
let tensor2 = tensor2.to_flattened();
assert_eq!(tensor2[2], 4.0);
Source§

type Output = Tensor<T>

The resulting type after applying the * operator.
Source§

impl<T> Mul for Tensor<T>
where T: Mul<Output = T> + Add<Output = T> + Copy,

Source§

fn mul(self, rhs: Tensor<T>) -> Tensor<T>

Multiply Tensors`

§Arguments
  • self - this tensor
  • rhs - 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);
Source§

type Output = Tensor<T>

The resulting type after applying the * operator.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.