pub struct Frontend { /* private fields */ }
Expand description
A frontend structure.
The frontend contains methods which operate on matrices or calculate functions for the
matrices. Backend methods are called by the frontend to operate the matrices. The frontend is
high-level layer that can be directly used by programmer or a Matrix
structure.
Implementations§
Source§impl Frontend
impl Frontend
Sourcepub fn new() -> Result<Frontend>
pub fn new() -> Result<Frontend>
Creates a frontend with a default backend.
This method also automatically initializes a default backend if the default backend is uninitialized.
Examples found in repository?
24fn main()
25{
26 let args: Vec<String> = env::args().collect();
27 let n: usize = match args.get(1) {
28 Some(s) => {
29 match s.parse::<usize>() {
30 Ok(tmp_n) => tmp_n,
31 Err(err) => {
32 eprintln!("{}", err);
33 exit(1);
34 },
35 }
36 },
37 None => 100,
38 };
39 let m: usize = match args.get(2) {
40 Some(s) => {
41 match s.parse::<usize>() {
42 Ok(tmp_m) => tmp_m,
43 Err(err) => {
44 eprintln!("{}", err);
45 exit(1);
46 },
47 }
48 },
49 None => 100,
50 };
51 let l: usize = match args.get(3) {
52 Some(s) => {
53 match s.parse::<usize>() {
54 Ok(tmp_l) => tmp_l,
55 Err(err) => {
56 eprintln!("{}", err);
57 exit(1);
58 },
59 }
60 },
61 None => 100,
62 };
63 let frontend = match Frontend::new() {
64 Ok(tmp_frontend) => tmp_frontend,
65 Err(err) => {
66 eprintln!("{}", err);
67 exit(1);
68 },
69 };
70 println!("backend: {}", frontend.backend().name());
71 let a = create_matrix(n, l);
72 let b = create_matrix(l, m);
73 let now = Instant::now();
74 let c = a * b;
75 let duration = now.elapsed();
76 let elems = c.elems();
77 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78 println!("sum: {}", sum);
79 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80 match finalize_default_backend() {
81 Ok(()) => (),
82 Err(err) => {
83 eprintln!("{}", err);
84 exit(1);
85 },
86 }
87}
More examples
24fn main()
25{
26 let args: Vec<String> = env::args().collect();
27 let n: usize = match args.get(1) {
28 Some(s) => {
29 match s.parse::<usize>() {
30 Ok(tmp_n) => tmp_n,
31 Err(err) => {
32 eprintln!("{}", err);
33 exit(1);
34 },
35 }
36 },
37 None => 100,
38 };
39 let m: usize = match args.get(2) {
40 Some(s) => {
41 match s.parse::<usize>() {
42 Ok(tmp_m) => tmp_m,
43 Err(err) => {
44 eprintln!("{}", err);
45 exit(1);
46 },
47 }
48 },
49 None => 100,
50 };
51 let l: usize = match args.get(3) {
52 Some(s) => {
53 match s.parse::<usize>() {
54 Ok(tmp_l) => tmp_l,
55 Err(err) => {
56 eprintln!("{}", err);
57 exit(1);
58 },
59 }
60 },
61 None => 100,
62 };
63 let frontend = match Frontend::new() {
64 Ok(tmp_frontend) => tmp_frontend,
65 Err(err) => {
66 eprintln!("{}", err);
67 exit(1);
68 },
69 };
70 println!("backend: {}", frontend.backend().name());
71 let a = create_matrix(n, l);
72 let b = create_matrix(m, l);
73 let now = Instant::now();
74 let c = a * b.transpose();
75 let duration = now.elapsed();
76 let elems = c.elems();
77 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78 println!("sum: {}", sum);
79 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80 match finalize_default_backend() {
81 Ok(()) => (),
82 Err(err) => {
83 eprintln!("{}", err);
84 exit(1);
85 },
86 }
87}
29fn main()
30{
31 let args: Vec<String> = env::args().collect();
32 let n: usize = match args.get(1) {
33 Some(s) => {
34 match s.parse::<usize>() {
35 Ok(tmp_n) => tmp_n,
36 Err(err) => {
37 eprintln!("{}", err);
38 exit(1);
39 },
40 }
41 },
42 None => 100,
43 };
44 let m: usize = match args.get(2) {
45 Some(s) => {
46 match s.parse::<usize>() {
47 Ok(tmp_m) => tmp_m,
48 Err(err) => {
49 eprintln!("{}", err);
50 exit(1);
51 },
52 }
53 },
54 None => 100,
55 };
56 let l: usize = match args.get(3) {
57 Some(s) => {
58 match s.parse::<usize>() {
59 Ok(tmp_l) => tmp_l,
60 Err(err) => {
61 eprintln!("{}", err);
62 exit(1);
63 },
64 }
65 },
66 None => 100,
67 };
68 let frontend = match Frontend::new() {
69 Ok(tmp_frontend) => tmp_frontend,
70 Err(err) => {
71 eprintln!("{}", err);
72 exit(1);
73 },
74 };
75 println!("backend: {}", frontend.backend().name());
76 let a = create_matrix(n, l, false);
77 let b = create_matrix(l, m, false);
78 let c = create_matrix(n, m, true);
79 let now = Instant::now();
80 let d = a * b + c;
81 let duration = now.elapsed();
82 let elems = d.elems();
83 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
84 println!("sum: {}", sum);
85 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
86 match finalize_default_backend() {
87 Ok(()) => (),
88 Err(err) => {
89 eprintln!("{}", err);
90 exit(1);
91 },
92 }
93}
Sourcepub fn new_with_backend(backend: Arc<dyn Backend>) -> Frontend
pub fn new_with_backend(backend: Arc<dyn Backend>) -> Frontend
Creates a frotend with the backend.
Sourcepub fn backend(&self) -> Arc<dyn Backend>
pub fn backend(&self) -> Arc<dyn Backend>
Returns the backend.
Examples found in repository?
24fn main()
25{
26 let args: Vec<String> = env::args().collect();
27 let n: usize = match args.get(1) {
28 Some(s) => {
29 match s.parse::<usize>() {
30 Ok(tmp_n) => tmp_n,
31 Err(err) => {
32 eprintln!("{}", err);
33 exit(1);
34 },
35 }
36 },
37 None => 100,
38 };
39 let m: usize = match args.get(2) {
40 Some(s) => {
41 match s.parse::<usize>() {
42 Ok(tmp_m) => tmp_m,
43 Err(err) => {
44 eprintln!("{}", err);
45 exit(1);
46 },
47 }
48 },
49 None => 100,
50 };
51 let l: usize = match args.get(3) {
52 Some(s) => {
53 match s.parse::<usize>() {
54 Ok(tmp_l) => tmp_l,
55 Err(err) => {
56 eprintln!("{}", err);
57 exit(1);
58 },
59 }
60 },
61 None => 100,
62 };
63 let frontend = match Frontend::new() {
64 Ok(tmp_frontend) => tmp_frontend,
65 Err(err) => {
66 eprintln!("{}", err);
67 exit(1);
68 },
69 };
70 println!("backend: {}", frontend.backend().name());
71 let a = create_matrix(n, l);
72 let b = create_matrix(l, m);
73 let now = Instant::now();
74 let c = a * b;
75 let duration = now.elapsed();
76 let elems = c.elems();
77 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78 println!("sum: {}", sum);
79 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80 match finalize_default_backend() {
81 Ok(()) => (),
82 Err(err) => {
83 eprintln!("{}", err);
84 exit(1);
85 },
86 }
87}
More examples
24fn main()
25{
26 let args: Vec<String> = env::args().collect();
27 let n: usize = match args.get(1) {
28 Some(s) => {
29 match s.parse::<usize>() {
30 Ok(tmp_n) => tmp_n,
31 Err(err) => {
32 eprintln!("{}", err);
33 exit(1);
34 },
35 }
36 },
37 None => 100,
38 };
39 let m: usize = match args.get(2) {
40 Some(s) => {
41 match s.parse::<usize>() {
42 Ok(tmp_m) => tmp_m,
43 Err(err) => {
44 eprintln!("{}", err);
45 exit(1);
46 },
47 }
48 },
49 None => 100,
50 };
51 let l: usize = match args.get(3) {
52 Some(s) => {
53 match s.parse::<usize>() {
54 Ok(tmp_l) => tmp_l,
55 Err(err) => {
56 eprintln!("{}", err);
57 exit(1);
58 },
59 }
60 },
61 None => 100,
62 };
63 let frontend = match Frontend::new() {
64 Ok(tmp_frontend) => tmp_frontend,
65 Err(err) => {
66 eprintln!("{}", err);
67 exit(1);
68 },
69 };
70 println!("backend: {}", frontend.backend().name());
71 let a = create_matrix(n, l);
72 let b = create_matrix(m, l);
73 let now = Instant::now();
74 let c = a * b.transpose();
75 let duration = now.elapsed();
76 let elems = c.elems();
77 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78 println!("sum: {}", sum);
79 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80 match finalize_default_backend() {
81 Ok(()) => (),
82 Err(err) => {
83 eprintln!("{}", err);
84 exit(1);
85 },
86 }
87}
29fn main()
30{
31 let args: Vec<String> = env::args().collect();
32 let n: usize = match args.get(1) {
33 Some(s) => {
34 match s.parse::<usize>() {
35 Ok(tmp_n) => tmp_n,
36 Err(err) => {
37 eprintln!("{}", err);
38 exit(1);
39 },
40 }
41 },
42 None => 100,
43 };
44 let m: usize = match args.get(2) {
45 Some(s) => {
46 match s.parse::<usize>() {
47 Ok(tmp_m) => tmp_m,
48 Err(err) => {
49 eprintln!("{}", err);
50 exit(1);
51 },
52 }
53 },
54 None => 100,
55 };
56 let l: usize = match args.get(3) {
57 Some(s) => {
58 match s.parse::<usize>() {
59 Ok(tmp_l) => tmp_l,
60 Err(err) => {
61 eprintln!("{}", err);
62 exit(1);
63 },
64 }
65 },
66 None => 100,
67 };
68 let frontend = match Frontend::new() {
69 Ok(tmp_frontend) => tmp_frontend,
70 Err(err) => {
71 eprintln!("{}", err);
72 exit(1);
73 },
74 };
75 println!("backend: {}", frontend.backend().name());
76 let a = create_matrix(n, l, false);
77 let b = create_matrix(l, m, false);
78 let c = create_matrix(n, m, true);
79 let now = Instant::now();
80 let d = a * b + c;
81 let duration = now.elapsed();
82 let elems = d.elems();
83 let sum = elems.iter().fold(0.0f32, |x, y| x + y);
84 println!("sum: {}", sum);
85 println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
86 match finalize_default_backend() {
87 Ok(()) => (),
88 Err(err) => {
89 eprintln!("{}", err);
90 exit(1);
91 },
92 }
93}
Sourcepub unsafe fn create_matrix(
&self,
row_count: usize,
col_count: usize,
) -> Result<Matrix>
pub unsafe fn create_matrix( &self, row_count: usize, col_count: usize, ) -> Result<Matrix>
Creates a matrix with unset elements.
Sourcepub fn create_matrix_and_set_zeros(
&self,
row_count: usize,
col_count: usize,
) -> Result<Matrix>
pub fn create_matrix_and_set_zeros( &self, row_count: usize, col_count: usize, ) -> Result<Matrix>
Creates a matrix and sets the matrix elements on zeros.
Sourcepub fn create_matrix_and_set_elems(
&self,
row_count: usize,
col_count: usize,
elems: &[f32],
) -> Result<Matrix>
pub fn create_matrix_and_set_elems( &self, row_count: usize, col_count: usize, elems: &[f32], ) -> Result<Matrix>
Creates a matrix and sets the matrix elements.
Sourcepub fn copy(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn copy(&self, a: &Matrix, b: &Matrix) -> Result<()>
Copies the a
matrix to the b
matrix.
This method indeed copies the a
matrix array to the b
matrix array.
Sourcepub fn get_elems_and_transpose_flag(
&self,
a: &Matrix,
elems: &mut [f32],
is_transposed: &mut bool,
) -> Result<()>
pub fn get_elems_and_transpose_flag( &self, a: &Matrix, elems: &mut [f32], is_transposed: &mut bool, ) -> Result<()>
Copies the matrix elements to the mutable slice and the transpose flag to the object that is referred by the reference.
Sourcepub fn elems_and_transpose_flag(&self, a: &Matrix) -> Result<(Vec<f32>, bool)>
pub fn elems_and_transpose_flag(&self, a: &Matrix) -> Result<(Vec<f32>, bool)>
Returns the elements and the transpose flag of matrix.
Sourcepub fn add(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
pub fn add(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
Adds the b
matrix to the a
matrix and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = matrix![
[5.0, 6.0],
[7.0, 8.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.add(&a, &b, &c).unwrap();
assert_eq!(vec![1.0 + 5.0, 2.0 + 6.0, 3.0 + 7.0, 4.0 + 8.0], c.elems());
Sourcepub fn sub(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
pub fn sub(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
Subtracts the b
matrix from the a
matrix and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = matrix![
[5.0, 6.0],
[7.0, 8.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.sub(&a, &b, &c).unwrap();
assert_eq!(vec![1.0 - 5.0, 2.0 - 6.0, 3.0 - 7.0, 4.0 - 8.0], c.elems());
Sourcepub fn mul(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
pub fn mul(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
Multiplies the a
matrix by the b
matrix and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]
];
let b = matrix![
[7.0, 8.0],
[9.0, 10.0],
[11.0, 12.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.mul(&a, &b, &c).unwrap();
let c11: f32 = 1.0 * 7.0 + 2.0 * 9.0 + 3.0 * 11.0;
let c12: f32 = 1.0 * 8.0 + 2.0 * 10.0 + 3.0 * 12.0;
let c21: f32 = 4.0 * 7.0 + 5.0 * 9.0 + 6.0 * 11.0;
let c22: f32 = 4.0 * 8.0 + 5.0 * 10.0 + 6.0 * 12.0;
assert_eq!(vec![c11, c12, c21, c22], c.elems());
Sourcepub fn mul_elems(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
pub fn mul_elems(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
Multiplies the a
matrix elements by the b
matrix elements and then the result is in the
c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = matrix![
[5.0, 6.0],
[7.0, 8.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.mul_elems(&a, &b, &c).unwrap();
assert_eq!(vec![1.0 * 5.0, 2.0 * 6.0, 3.0 * 7.0, 4.0 * 8.0], c.elems());
Sourcepub fn div_elems(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
pub fn div_elems(&self, a: &Matrix, b: &Matrix, c: &Matrix) -> Result<()>
Divides the a
matrix elements by the b
matrix elements and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = matrix![
[5.0, 6.0],
[7.0, 8.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.div_elems(&a, &b, &c).unwrap();
let elems = c.elems();
assert!((1.0 / 5.0 - elems[0]).abs() < 0.001);
assert!((2.0 / 6.0 - elems[1]).abs() < 0.001);
assert!((3.0 / 7.0 - elems[2]).abs() < 0.001);
assert!((4.0 / 8.0 - elems[3]).abs() < 0.001);
Sourcepub fn add_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
pub fn add_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
Adds the b
scalar to the a
matrix and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.add_for_scalar(&a, 10.5, &c).unwrap();
assert_eq!(vec![1.0 + 10.5, 2.0 + 10.5, 3.0 + 10.5, 4.0 + 10.5], c.elems());
Sourcepub fn sub_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
pub fn sub_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
Subtracts the b
scalar from the a
matrix and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.sub_for_scalar(&a, 10.5, &c).unwrap();
assert_eq!(vec![1.0 - 10.5, 2.0 - 10.5, 3.0 - 10.5, 4.0 - 10.5], c.elems());
Sourcepub fn rsub_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
pub fn rsub_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
Subtracts the a
matrix from the b
scalar and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.rsub_for_scalar(&a, 10.5, &c).unwrap();
assert_eq!(vec![10.5 - 1.0, 10.5 - 2.0, 10.5 - 3.0, 10.5 - 4.0], c.elems());
Sourcepub fn mul_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
pub fn mul_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
Multiplies the a
matrix by the b
scalar and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.mul_for_scalar(&a, 10.5, &c).unwrap();
assert_eq!(vec![1.0 * 10.5, 2.0 * 10.5, 3.0 * 10.5, 4.0 * 10.5], c.elems());
Sourcepub fn div_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
pub fn div_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
Divides the a
matrix by the b
scalar and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.div_for_scalar(&a, 10.5, &c).unwrap();
let elems = c.elems();
assert!((1.0 / 10.5 - elems[0]).abs() < 0.001);
assert!((2.0 / 10.5 - elems[1]).abs() < 0.001);
assert!((3.0 / 10.5 - elems[2]).abs() < 0.001);
assert!((4.0 / 10.5 - elems[3]).abs() < 0.001);
Sourcepub fn rdiv_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
pub fn rdiv_for_scalar(&self, a: &Matrix, b: f32, c: &Matrix) -> Result<()>
Divides the b
scalar by the a
matrix elements and then the result is in the c
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let c = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.rdiv_for_scalar(&a, 10.5, &c).unwrap();
let elems = c.elems();
assert!((10.5 / 1.0- elems[0]).abs() < 0.001);
assert!((10.5 / 2.0 - elems[1]).abs() < 0.001);
assert!((10.5 / 3.0 - elems[2]).abs() < 0.001);
assert!((10.5 / 4.0 - elems[3]).abs() < 0.001);
Sourcepub fn sigmoid(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn sigmoid(&self, a: &Matrix, b: &Matrix) -> Result<()>
Calculates sigmoid function for the a
matrix and then the result is in the b
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.sigmoid(&a, &b).unwrap();
let elems = b.elems();
assert!((1.0 / (1.0 + (-1.0f32).exp()) - elems[0]).abs() < 0.001);
assert!((1.0 / (1.0 + (-2.0f32).exp()) - elems[1]).abs() < 0.001);
assert!((1.0 / (1.0 + (-3.0f32).exp()) - elems[2]).abs() < 0.001);
assert!((1.0 / (1.0 + (-4.0f32).exp()) - elems[3]).abs() < 0.001);
Sourcepub fn tanh(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn tanh(&self, a: &Matrix, b: &Matrix) -> Result<()>
Calculates hyperbolic tangent function for the a
matrix and then the result is in the b
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.tanh(&a, &b).unwrap();
let elems = b.elems();
assert!((1.0f32.tanh() - elems[0]).abs() < 0.001);
assert!((2.0f32.tanh() - elems[1]).abs() < 0.001);
assert!((3.0f32.tanh() - elems[2]).abs() < 0.001);
assert!((4.0f32.tanh() - elems[3]).abs() < 0.001);
Sourcepub fn swish(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn swish(&self, a: &Matrix, b: &Matrix) -> Result<()>
Calculates swish function for the a
matrix and then the result is in the b
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.swish(&a, &b).unwrap();
let elems = b.elems();
assert!((1.0 / (1.0 + (-1.0f32).exp()) - elems[0]).abs() < 0.001);
assert!((2.0 / (1.0 + (-2.0f32).exp()) - elems[1]).abs() < 0.001);
assert!((3.0 / (1.0 + (-3.0f32).exp()) - elems[2]).abs() < 0.001);
assert!((4.0 / (1.0 + (-4.0f32).exp()) - elems[3]).abs() < 0.001);
Sourcepub fn softmax(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn softmax(&self, a: &Matrix, b: &Matrix) -> Result<()>
Calculates softmax function for the a
matrix and then the result is in the b
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.softmax(&a, &b).unwrap();
let elems = b.elems();
let sum1 = 1.0f32.exp() + 3.0f32.exp();
let sum2 = 2.0f32.exp() + 4.0f32.exp();
assert!((1.0f32.exp() / sum1 - elems[0]).abs() < 0.001);
assert!((2.0f32.exp() / sum2 - elems[1]).abs() < 0.001);
assert!((3.0f32.exp() / sum1 - elems[2]).abs() < 0.001);
assert!((4.0f32.exp() / sum2 - elems[3]).abs() < 0.001);
Sourcepub fn sqrt(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn sqrt(&self, a: &Matrix, b: &Matrix) -> Result<()>
Calculates square root of the a
matrix and then the result is in the b
matrix
().
§Examples
let a = matrix![
[1.0, 2.0],
[3.0, 4.0]
];
let b = Matrix::new(2, 2);
let frontend = Frontend::new().unwrap();
frontend.sqrt(&a, &b).unwrap();
let elems = b.elems();
assert!((1.0f32.sqrt() - elems[0]).abs() < 0.001);
assert!((2.0f32.sqrt() - elems[1]).abs() < 0.001);
assert!((3.0f32.sqrt() - elems[2]).abs() < 0.001);
assert!((4.0f32.sqrt() - elems[3]).abs() < 0.001);
Sourcepub fn really_transpose(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn really_transpose(&self, a: &Matrix, b: &Matrix) -> Result<()>
Indeed transposes the a
matrix and then the result is in the b
matrix
().
This method indeed transposes the a
matrix without changing the transpose flag.
§Examples
let a = matrix![
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]
];
let b = Matrix::new(3, 2);
let frontend = Frontend::new().unwrap();
frontend.really_transpose(&a, &b).unwrap();
assert_eq!(vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0], b.elems());
Sourcepub fn repeat(&self, a: &Matrix, b: &Matrix) -> Result<()>
pub fn repeat(&self, a: &Matrix, b: &Matrix) -> Result<()>
Repeats the a
vector as column or a row
( or
).
§Examples
let a = matrix![
[1.0],
[2.0]
];
let b = Matrix::new(2, 3);
let frontend = Frontend::new().unwrap();
frontend.repeat(&a, &b).unwrap();
assert_eq!(vec![1.0, 1.0, 1.0, 2.0, 2.0, 2.0], b.elems());
let c = matrix![[1.0, 2.0, 3.0]];
let d = Matrix::new(2, 3);
let frontend = Frontend::new().unwrap();
frontend.repeat(&c, &d).unwrap();
assert_eq!(vec![1.0, 2.0, 3.0, 1.0, 2.0, 3.0], d.elems());