forked from coreylowman/dfdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-ops.rs
33 lines (24 loc) · 807 Bytes
/
02-ops.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Intro to dfdx::tensor_ops
use rand::prelude::*;
use dfdx::arrays::HasArrayData;
use dfdx::tensor::{Tensor0D, Tensor2D, TensorCreator};
use dfdx::tensor_ops::add;
fn main() {
let mut rng = StdRng::seed_from_u64(0);
let a: Tensor2D<2, 3> = TensorCreator::randn(&mut rng);
dbg!(a.data());
let b: Tensor2D<2, 3> = TensorCreator::randn(&mut rng);
dbg!(b.data());
// we can do binary operations like add two tensors together
let c = add(a, b);
dbg!(c.data());
// or unary operations like apply the `relu` function to each element
let d = c.relu();
dbg!(d.data());
// we can add/sub/mul/div scalar values to tensors
let e = d + 0.5;
dbg!(e.data());
// or reduce tensors to smaller sizes
let f: Tensor0D = e.mean();
dbg!(f.data());
}