-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from c0dearm/tensor-refactor
fix!: refactored shape of tensors and traits for easier expression of…
- Loading branch information
Showing
19 changed files
with
1,020 additions
and
983 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use crate::tensor::{variable::Variable, Tensor}; | ||
|
||
/// Creates a variable tensor filled with the given value | ||
#[must_use] | ||
#[inline] | ||
pub fn fill<const B: u64, const C: u64, const H: u64, const W: u64>( | ||
v: f32, | ||
) -> Tensor<B, C, H, W, Variable> { | ||
Variable::from(arrayfire::constant!(v; H,W,C,B)).into() | ||
} | ||
|
||
/// Creates a variable tensor with the main diagonal filled with the given value, 0 everywhere else | ||
#[must_use] | ||
#[inline] | ||
pub fn eye<const B: u64, const C: u64, const H: u64, const W: u64>( | ||
v: f32, | ||
) -> Tensor<B, C, H, W, Variable> { | ||
Variable::from(v * arrayfire::identity::<f32>(arrayfire::dim4!(H, W, C, B))).into() | ||
} | ||
|
||
/// Creates a variable tensor with random values taken from a uniform distribution between [0,1] | ||
#[must_use] | ||
#[inline] | ||
pub fn randu<const B: u64, const C: u64, const H: u64, const W: u64>( | ||
) -> Tensor<B, C, H, W, Variable> { | ||
Variable::from(arrayfire::randu!(H, W, C, B)).into() | ||
} | ||
|
||
/// Creates a variable tensor with random values taken from a normal distribution centered at 0 | ||
#[must_use] | ||
#[inline] | ||
pub fn randn<const B: u64, const C: u64, const H: u64, const W: u64>( | ||
) -> Tensor<B, C, H, W, Variable> { | ||
Variable::from(arrayfire::randn!(H, W, C, B)).into() | ||
} | ||
|
||
/// Creates a variable tensor from the given array of values | ||
#[must_use] | ||
#[inline] | ||
pub fn custom<const B: u64, const C: u64, const H: u64, const W: u64>( | ||
values: &[f32], | ||
) -> Tensor<B, C, H, W, Variable> { | ||
Variable::from(arrayfire::Array::new(values, arrayfire::dim4!(H, W, C, B))).into() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{custom, eye, fill, randn, randu}; | ||
use crate::tensor::traits::Tensed; | ||
use crate::tests::equal_data; | ||
use arrayfire::{all_true_all, constant, dim4, identity, le}; | ||
|
||
#[test] | ||
fn test_fill() { | ||
let x = fill::<1, 2, 3, 4>(2.0); | ||
assert!(equal_data(x.data(), constant!(2.0; 3,4,2,1))); | ||
} | ||
|
||
#[test] | ||
fn test_eye() { | ||
let x = eye::<1, 2, 3, 4>(2.0); | ||
assert!(equal_data( | ||
x.data(), | ||
identity::<f32>(dim4!(3, 4, 2, 1)) * 2.0f32 | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_randu() { | ||
let x = randu::<1, 2, 3, 4>(); | ||
assert!(all_true_all(&le(&x.data(), &constant!(1.0; 3,4,2,1), false)).0) | ||
} | ||
|
||
#[test] | ||
fn test_randn() { | ||
let x = randn::<1, 2, 3, 4>(); | ||
assert!(all_true_all(&le(&x.data(), &constant!(5.0; 3,4,2,1), false)).0) | ||
} | ||
|
||
#[test] | ||
fn test_custom() { | ||
let x = custom::<1, 1, 1, 1>(&[1.0]); | ||
assert!(equal_data(x.data(), constant!(1.0;1,1,1,1))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.