forked from coreylowman/dfdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumpy-save-load.rs
27 lines (21 loc) · 928 Bytes
/
numpy-save-load.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
//! Demonstrates how to use dfdx::numpy to save and load arrays
#[cfg(feature = "numpy")]
fn main() {
use dfdx::numpy as np;
np::save("0d-rs.npy", &1.234).expect("Saving failed");
np::save("1d-rs.npy", &[1.0, 2.0, 3.0]).expect("Saving failed");
np::save("2d-rs.npy", &[[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]).expect("Saving failed");
let mut expected_0d = 0.0;
np::load("0d-rs.npy", &mut expected_0d).expect("Loading failed");
assert_eq!(expected_0d, 1.234);
let mut expected_1d = [0.0; 3];
np::load("1d-rs.npy", &mut expected_1d).expect("Loading failed");
assert_eq!(expected_1d, [1.0, 2.0, 3.0]);
let mut expected_2d = [[0.0; 3]; 2];
np::load("2d-rs.npy", &mut expected_2d).expect("Loading failed");
assert_eq!(expected_2d, [[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]);
}
#[cfg(not(feature = "numpy"))]
fn main() {
panic!("Use the 'numpy' feature to run this example");
}