pub struct Tensor<T> { /* private fields */ }
Expand description
The main Tensor struct with data and shape order by [… , z, y, x]
Implementations§
Source§impl<T: Default + Clone> Tensor<T>
impl<T: Default + Clone> Tensor<T>
Sourcepub fn new(_shape: &[u32]) -> Tensor<T>
pub fn new(_shape: &[u32]) -> Tensor<T>
Creates a new tensor with shape and default values of each element
§Example
use flashlight_tensor::prelude::*;
//a =
//[0.0, 0.0]
//[0.0, 0.0]
let a: Tensor<f32> = Tensor::new(&[2, 2]);
assert_eq!(a.get_data(), &vec!{0.0, 0.0, 0.0, 0.0});
Sourcepub fn from_data(_data: &[T], _shape: &[u32]) -> Option<Self>
pub fn from_data(_data: &[T], _shape: &[u32]) -> Option<Self>
Creates a new tensor from data with certain size, or None if data does not fit in shape
§Example
use flashlight_tensor::prelude::*;
//a =
//[1.0, 2.0]
//[3.0, 4.0]
let a: Tensor<f32> = Tensor::from_data(&vec!{1.0, 2.0, 3.0, 4.0}, &[2, 2]).unwrap();
assert_eq!(a.get_data(), &vec!{1.0, 2.0, 3.0, 4.0});
Sourcepub fn fill(fill_data: T, _shape: &[u32]) -> Self
pub fn fill(fill_data: T, _shape: &[u32]) -> Self
Creates a new tensor filled with one element with certain size
§Example
use flashlight_tensor::prelude::*;
//a =
//[1.0, 1.0]
//[1.0, 1.0]
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
assert_eq!(a.get_data(), &vec!{1.0, 1.0, 1.0, 1.0});
Sourcepub fn get_data(&self) -> &Vec<T>
pub fn get_data(&self) -> &Vec<T>
Returns reference to data in tensor
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//b = &{1.0, 1.0, 1.0, 1.0}
let b = a.get_data();
assert_eq!(a.get_data(), &vec!{1.0, 1.0, 1.0, 1.0});
Sourcepub fn get_shape(&self) -> &Vec<u32>
pub fn get_shape(&self) -> &Vec<u32>
Returns reference to shape in tensor
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//b = &{2, 2}
let b = a.get_shape();
assert_eq!(a.get_shape(), &vec!{2, 2});
Sourcepub fn append(&self, tens2: &Tensor<T>) -> Option<Self>
pub fn append(&self, tens2: &Tensor<T>) -> Option<Self>
returns new tensor with data of first tensor + data of second tensor with size[0] = tensor1.size[0] + tensor2.size[0] only when tensor1.size[1..] == tensor2.size[1..]
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
let b: Tensor<f32> = Tensor::fill(2.0, &[2, 2]);
//c.data = {1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0}
//c.shape = {4, 2}
let c: Tensor<f32> = a.append(&b).unwrap();
assert_eq!(c.get_data(), &vec!{1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0});
assert_eq!(c.get_shape(), &vec!{4, 2});
Sourcepub fn count_data(&self) -> usize
pub fn count_data(&self) -> usize
counts elements in tensor
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//count = 4
let count = a.count_data();
assert_eq!(a.count_data(), 4);
Sourcepub fn set_shape(&mut self, new_shape: &[u32])
pub fn set_shape(&mut self, new_shape: &[u32])
Change the size of tensor if the full size of new_shape is equal to data.len() stored in tensor.
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::fill(1.0, &[4]);
a.set_shape(&[1, 4]);
assert_eq!(a.get_shape(), &vec!{1, 4});
Sourcepub fn set_data(&mut self, new_data: &[T])
pub fn set_data(&mut self, new_data: &[T])
Change the data of tensor if the new data has length equal to current data length
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::fill(1.0, &[4]);
a.set_data(&[2.0, 3.0, 4.0, 5.0]);
assert_eq!(a.get_data(), &vec!{2.0, 3.0, 4.0, 5.0});
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn value(&self, pos: &[u32]) -> Option<&T>
pub fn value(&self, pos: &[u32]) -> Option<&T>
returns an element on position
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//b = 1.0
let b = a.value(&[0, 0]).unwrap();
assert_eq!(b, &1.0);
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_add(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_add(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
Add content of one tensor to another None if different sizes
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//b =
//[2.0, 2.0]
//[2.0, 2.0]
let b: Tensor<f32> = a.tens_add(&a).unwrap();
assert_eq!(b.get_data(), &vec!{2.0, 2.0, 2.0, 2.0})
Sourcepub fn tens_add_mut(&mut self, tens2: &Tensor<T>)
pub fn tens_add_mut(&mut self, tens2: &Tensor<T>)
Add content of one tensor to another None if different sizes
!Mutates a tensor
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
let b: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//a =
//[2.0, 2.0]
//[2.0, 2.0]
a.tens_add_mut(&b);
assert_eq!(a.get_data(), &vec!{2.0, 2.0, 2.0, 2.0})
Sourcepub fn add(&self, val: T) -> Tensor<T>
pub fn add(&self, val: T) -> Tensor<T>
Add value to each value of tensor
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//b =
//[3.0, 3.0]
//[3.0, 3.0]
let b: Tensor<f32> = a.add(2.0);
assert_eq!(b.get_data(), &vec!{3.0, 3.0, 3.0, 3.0})
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_div(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_div(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
Divide content of one tensor with another None if different sizes
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(2.0, &[2, 2]);
//b =
//[1.0, 1.0]
//[1.0, 1.0]
let b: Tensor<f32> = a.tens_div(&a).unwrap();
assert_eq!(b.get_data(), &vec!{1.0, 1.0, 1.0, 1.0})
Sourcepub fn tens_div_mut(&mut self, tens2: &Tensor<T>)
pub fn tens_div_mut(&mut self, tens2: &Tensor<T>)
Divide content of one tensor with another None if different sizes
!Mutates the tensor
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::fill(2.0, &[2, 2]);
let mut b: Tensor<f32> = Tensor::fill(2.0, &[2, 2]);
//a =
//[1.0, 1.0]
//[1.0, 1.0]
a.tens_div_mut(&b);
assert_eq!(a.get_data(), &vec!{1.0, 1.0, 1.0, 1.0})
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_mul(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_mul(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
multiply content of one tensor with another None if different sizes
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(2.0, &[2, 2]);
//b =
//[4.0, 4.0]
//[4.0, 4.0]
let b: Tensor<f32> = a.tens_mul(&a).unwrap();
assert_eq!(b.get_data(), &vec!{4.0, 4.0, 4.0, 4.0})
Sourcepub fn tens_mul_mut(&mut self, tens2: &Tensor<T>)
pub fn tens_mul_mut(&mut self, tens2: &Tensor<T>)
multiply content of one tensor with another None if different sizes
!Mutates the tensor
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::fill(2.0, &[2, 2]);
let b: Tensor<f32> = Tensor::fill(2.0, &[2, 2]);
//a =
//[4.0, 4.0]
//[4.0, 4.0]
a.tens_mul_mut(&b);
assert_eq!(a.get_data(), &vec!{4.0, 4.0, 4.0, 4.0})
Sourcepub fn mul(&self, val: T) -> Tensor<T>
pub fn mul(&self, val: T) -> Tensor<T>
Multiply each tensor value by scalar
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//b =
//[2.0, 2.0]
//[2.0, 2.0]
let b: Tensor<f32> = a.mul(2.0);
assert_eq!(b.get_data(), &vec!{2.0, 2.0, 2.0, 2.0})
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_sub(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_sub(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
Subtract content of one tensor from another None if different sizes
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//b =
//[0.0, 0.0]
//[0.0, 0.0]
let b: Tensor<f32> = a.tens_sub(&a).unwrap();
assert_eq!(b.get_data(), &vec!{0.0, 0.0, 0.0, 0.0})
Sourcepub fn tens_sub_mut(&mut self, tens2: &Tensor<T>)
pub fn tens_sub_mut(&mut self, tens2: &Tensor<T>)
Subtract content of one tensor from another None if different sizes
!Mutates the tensor
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
let b: Tensor<f32> = Tensor::fill(1.0, &[2, 2]);
//a =
//[0.0, 0.0]
//[0.0, 0.0]
a.tens_sub_mut(&b);
assert_eq!(a.get_data(), &vec!{0.0, 0.0, 0.0, 0.0})
Source§impl Tensor<f32>
impl Tensor<f32>
Sourcepub fn nlog(&self) -> Tensor<f32>
pub fn nlog(&self) -> Tensor<f32>
Each element transformed to natural log of that element
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::from_data(&[1.0, 10.0, 100.0], &[3]).unwrap();
//b =
//[1.0, 2.0, 3.0]
let b: Tensor<f32> = a.nlog();
assert_eq!(b.get_data(), &vec!{0.0, 1.0, 2.0})
Sourcepub fn nlog_mut(&mut self)
pub fn nlog_mut(&mut self)
Each element transformed to natural log of that element
!Mutates the tensor
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::from_data(&[1.0, 10.0, 100.0], &[3]).unwrap();
// =
//[1.0, 2.0, 3.0]
a.nlog_mut();
assert_eq!(a.get_data(), &vec!{0.0, 1.0, 2.0})
Sourcepub fn log(&self, x: f32) -> Tensor<f32>
pub fn log(&self, x: f32) -> Tensor<f32>
Each element transformed to log of x of that element
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::from_data(&[1.0, 10.0, 100.0], &[3]).unwrap();
//b =
//[1.0, 2.0, 3.0]
let b: Tensor<f32> = a.log(10.0);
assert_eq!(b.get_data(), &vec!{0.0, 1.0, 2.0})
Sourcepub fn log_mut(&mut self, x: f32)
pub fn log_mut(&mut self, x: f32)
Each element transformed to log of x of that element
!Mutates the tensor
§Example
use flashlight_tensor::prelude::*;
let mut a: Tensor<f32> = Tensor::from_data(&[1.0, 10.0, 100.0], &[3]).unwrap();
// =
//[1.0, 2.0, 3.0]
a.log_mut(10.0);
assert_eq!(a.get_data(), &vec!{0.0, 1.0, 2.0})
Source§impl<T: Default + Clone> Tensor<T>
impl<T: Default + Clone> Tensor<T>
Sourcepub fn matrix(&self, pos: &[u32]) -> Option<Tensor<T>>
pub fn matrix(&self, pos: &[u32]) -> Option<Tensor<T>>
Get matrix on position or None
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
let sizes: Vec<u32> = vec!{2, 2, 2};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected_data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0};
let result = tensor.matrix(&[0]).unwrap();
assert_eq!(result.get_data(), &expected_data);
Sourcepub fn matrix_row(&self, row: u32) -> Option<Tensor<T>>
pub fn matrix_row(&self, row: u32) -> Option<Tensor<T>>
Get row when tensor have 2 dimensions or None
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0};
let sizes: Vec<u32> = vec!{2,2};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected: Tensor<f32> = Tensor::from_data(&vec!{1.0, 2.0}, &vec!{1, 2}).unwrap();
let result = tensor.matrix_row(0).unwrap();
assert_eq!(result.get_data(), expected.get_data());
assert_eq!(result.get_shape(), expected.get_shape());
Sourcepub fn matrix_col(&self, col: u32) -> Option<Tensor<T>>
pub fn matrix_col(&self, col: u32) -> Option<Tensor<T>>
Get collumn when tensor have 2 dimensions or None
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0};
let sizes: Vec<u32> = vec!{2,2};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected: Tensor<f32> = Tensor::from_data(&vec!{2.0, 4.0}, &vec!{2, 1}).unwrap();
let result = tensor.matrix_col(1).unwrap();
assert_eq!(result.get_data(), expected.get_data());
assert_eq!(result.get_shape(), expected.get_shape());
Sourcepub fn matrix_transpose(&self) -> Option<Tensor<T>>
pub fn matrix_transpose(&self) -> Option<Tensor<T>>
Transpose matrix RxC to CxR
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
let sizes: Vec<u32> = vec!{2,3};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected_data: Vec<f32> = vec!{1.0, 4.0, 2.0, 5.0, 3.0, 6.0};
let expected_sizes: Vec<u32> = vec!{3, 2};
let result = tensor.matrix_transpose().unwrap();
assert_eq!(result.get_data(), &expected_data);
assert_eq!(result.get_shape(), &expected_sizes);
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn matrix_to_string(&self) -> Option<String>
pub fn matrix_to_string(&self) -> Option<String>
Returns string when tensor is 2 dimensional
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0};
let sizes: Vec<u32> = vec!{2, 2};
let expected: String = "|1, 2|\n|3, 4|".to_string();
let tensor = Tensor::from_data(&data, &sizes).unwrap();
let result = tensor.matrix_to_string().unwrap();
assert_eq!(result, expected);
Source§impl Tensor<f32>
impl Tensor<f32>
Sourcepub fn matrix_mul(&self, tens2: &Tensor<f32>) -> Option<Tensor<f32>>
pub fn matrix_mul(&self, tens2: &Tensor<f32>) -> Option<Tensor<f32>>
Persorms matrix multiplication on matrix with another matrix
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
let sizes1: Vec<u32> = vec!{3,2};
let sizes2: Vec<u32> = vec!{2,3};
let tensor1: Tensor<f32> = Tensor::from_data(&data, &sizes1).unwrap();
let tensor2: Tensor<f32> = Tensor::from_data(&data, &sizes2).unwrap();
let expected_data: Vec<f32> = vec!{9.0, 12.0, 15.0, 19.0, 26.0, 33.0, 29.0, 40.0, 51.0};
let expected_sizes: Vec<u32> = vec!{3,3};
let result: Tensor<f32> = tensor1.matrix_mul(&tensor2).unwrap();
assert_eq!(result.get_data(), &expected_data);
assert_eq!(result.get_shape(), &expected_sizes);
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn matrix_col_sum(&self) -> Option<Tensor<T>>
pub fn matrix_col_sum(&self) -> Option<Tensor<T>>
Returns a sum of of all collumns merged into one in matrix
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
let sizes: Vec<u32> = vec!{3,2};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected_data: Vec<f32> = vec!{3.0, 7.0, 11.0};
let expected_sizes: Vec<u32> = vec!{3,1};
let result: Tensor<f32> = tensor.matrix_col_sum().unwrap();
assert_eq!(result.get_data(), &expected_data);
assert_eq!(result.get_shape(), &expected_sizes);
Sourcepub fn matrix_row_sum(&self) -> Option<Tensor<T>>
pub fn matrix_row_sum(&self) -> Option<Tensor<T>>
Returns a sum of of all rows merged into one in matrix
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
let sizes: Vec<u32> = vec!{3,2};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected_data: Vec<f32> = vec!{9.0, 12.0};
let expected_sizes: Vec<u32> = vec!{1,2};
let result: Tensor<f32> = tensor.matrix_row_sum().unwrap();
assert_eq!(result.get_data(), &expected_data);
assert_eq!(result.get_shape(), &expected_sizes);
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn matrix_col_prod(&self) -> Option<Tensor<T>>
pub fn matrix_col_prod(&self) -> Option<Tensor<T>>
Returns a product of of all collumns merged into one in matrix
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
let sizes: Vec<u32> = vec!{3,2};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected_data: Vec<f32> = vec!{2.0, 12.0, 30.0};
let expected_sizes: Vec<u32> = vec!{3,1};
let result: Tensor<f32> = tensor.matrix_col_prod().unwrap();
assert_eq!(result.get_data(), &expected_data);
assert_eq!(result.get_shape(), &expected_sizes);
Sourcepub fn matrix_row_prod(&self) -> Option<Tensor<T>>
pub fn matrix_row_prod(&self) -> Option<Tensor<T>>
Returns a difference of of all rows merged into one in matrix
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
let sizes: Vec<u32> = vec!{3,2};
let tensor: Tensor<f32> = Tensor::from_data(&data, &sizes).unwrap();
let expected_data: Vec<f32> = vec!{15.0, 48.0};
let expected_sizes: Vec<u32> = vec!{1,2};
let result: Tensor<f32> = tensor.matrix_row_prod().unwrap();
assert_eq!(result.get_data(), &expected_data);
assert_eq!(result.get_shape(), &expected_sizes);
Source§impl<T: Default + Clone> Tensor<T>
impl<T: Default + Clone> Tensor<T>
Sourcepub fn vector(&self, pos: &[u32]) -> Option<Tensor<T>>
pub fn vector(&self, pos: &[u32]) -> Option<Tensor<T>>
Get vector from Tensor on position
§Example
use flashlight_tensor::prelude::*;
let data: Vec<f32> = vec!{1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
let sizes: Vec<u32> = vec!{2, 3};
let tensor = Tensor::from_data(&data, &sizes).unwrap();
let vector = tensor.vector(&[0]).unwrap();
let expected_data: Vec<f32> = vec!{1.0, 2.0, 3.0};
let expected_sizes: Vec<u32> = vec!{3};
assert_eq!(vector.get_data(), &expected_data);
assert_eq!(vector.get_shape(), &expected_sizes);
Source§impl Tensor<f32>
impl Tensor<f32>
Sourcepub fn dot_product(&self, tens2: &Tensor<f32>) -> Option<f32>
pub fn dot_product(&self, tens2: &Tensor<f32>) -> Option<f32>
Get dot product from tensors if tensors have one dimenstion and have same size
§Example
use flashlight_tensor::prelude::*;
let data1: Vec<f32> = vec!{1.0, 2.0, 3.0};
let data2: Vec<f32> = vec!{3.0, 2.0, 1.0};
let expected: f32 = 10.0; // 3.0 + 4.0 + 3.0
let tensor1 = Tensor::from_data(&data1, &[3]).unwrap();
let tensor2 = Tensor::from_data(&data2, &[3]).unwrap();
let result = tensor1.dot_product(&tensor2).unwrap();
assert_eq!(result, expected);
Source§impl Tensor<f32>
impl Tensor<f32>
Sourcepub fn relu(&self) -> Tensor<f32>
pub fn relu(&self) -> Tensor<f32>
Returns a tensor with data transformed using ReLU function
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::from_data(&[-20.0, 0.0, 20.0], &[3]).unwrap();
let b = a.relu();
assert_eq!(b.get_data(), &vec!{0.0, 0.0, 20.0});
Sourcepub fn relu_der(&self) -> Tensor<f32>
pub fn relu_der(&self) -> Tensor<f32>
Returns a tensor with data transformed using derivative of ReLU function
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::from_data(&[-10.0, 0.0, 10.0], &[3]).unwrap();
let b = a.relu_der();
assert_eq!(b.get_data(), &vec!{0.0, 1.0, 1.0});
Source§impl Tensor<f32>
impl Tensor<f32>
Sourcepub fn sigmoid(&self) -> Tensor<f32>
pub fn sigmoid(&self) -> Tensor<f32>
Returns a tensor with data transformed using sigmoid function
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::from_data(&[-200.0, 0.0, 200.0], &[3]).unwrap();
let b = a.sigmoid();
assert_eq!(b.get_data(), &vec!{0.0, 0.5, 1.0});
Sourcepub fn sigmoid_der(&self) -> Tensor<f32>
pub fn sigmoid_der(&self) -> Tensor<f32>
Returns a tensor with data transformed using sigmoid function
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::from_data(&[-200.0, 0.0, 200.0], &[3]).unwrap();
let b = a.sigmoid_der();
assert_eq!(b.get_data(), &vec!{0.0, 0.25, 0.0});
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_broadcast_add(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_broadcast_add(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
broadcast add data of second vector to first vector
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 1, 2]);
let b: Tensor<f32> = Tensor::fill(2.0, &[2, 2, 1]);
let b: Tensor<f32> = a.tens_broadcast_add(&b).unwrap();
assert_eq!(b.get_data(), &vec!{3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0});
assert_eq!(b.get_shape(), &vec!{2, 2, 2});
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_broadcast_sub(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_broadcast_sub(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
broadcast subtract data of second vector to first vector
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(1.0, &[2, 1, 2]);
let b: Tensor<f32> = Tensor::fill(2.0, &[2, 2, 1]);
let b: Tensor<f32> = a.tens_broadcast_sub(&b).unwrap();
assert_eq!(b.get_data(), &vec!{-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0});
assert_eq!(b.get_shape(), &vec!{2, 2, 2});
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_broadcast_mul(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_broadcast_mul(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
broadcast multiply data of second vector to first vector
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(2.0, &[2, 1, 2]);
let b: Tensor<f32> = Tensor::fill(2.0, &[2, 2, 1]);
let b: Tensor<f32> = a.tens_broadcast_mul(&b).unwrap();
assert_eq!(b.get_data(), &vec!{4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0});
assert_eq!(b.get_shape(), &vec!{2, 2, 2});
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn tens_broadcast_div(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
pub fn tens_broadcast_div(&self, tens2: &Tensor<T>) -> Option<Tensor<T>>
broadcast divide data of second vector to first vector
§Example
use flashlight_tensor::prelude::*;
let a: Tensor<f32> = Tensor::fill(4.0, &[2, 1, 2]);
let b: Tensor<f32> = Tensor::fill(2.0, &[2, 2, 1]);
let b: Tensor<f32> = a.tens_broadcast_div(&b).unwrap();
assert_eq!(b.get_data(), &vec!{2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0});
assert_eq!(b.get_shape(), &vec!{2, 2, 2});