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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! `topos` is a tiny autograd engine for the GPU-poor.
//!
//! Expressions record a static computation graph onto a shared
//! `Network`; `forward` materializes every value, `backward`
//! differentiates one scalar target, and `update` produces the next
//! network generation from a gradient step:
//!
//! ```
//! use topos::Network;
//!
//! let network = Network::new();
//! let w = network.parameter(0.0_f64);
//! let x = network.input(0.0);
//! let y = network.input(0.0);
//!
//! // Operators record the graph; values are `Copy` and never consumed.
//! let error = w * x - y;
//! let loss = error * error;
//!
//! let w_symbol = w.symbol();
//! let x_symbol = x.symbol();
//! let y_symbol = y.symbol();
//! let loss_symbol = loss.symbol();
//!
//! // The graph is recorded once; every step feeds one sample of the line
//! // `y = 2 * x` and steps to the next generation, which shares the recorded
//! // graph while replacing the parameter payloads.
//! let samples = [(1.0, 2.0), (2.0, 4.0), (3.0, 6.0)];
//! let mut network = network;
//! for step in 0..100 {
//! let (sample_x, sample_y) = samples[step % samples.len()];
//! let loss = network.resolve(loss_symbol);
//! let run = network.forward_with([(x_symbol, sample_x), (y_symbol, sample_y)]);
//! let gradients = run.backward(loss);
//! network = network.update(&gradients, |w, g| w - 0.02 * g);
//! }
//!
//! let learned = network.resolve(w_symbol).payload().unwrap();
//! assert!((learned - 2.0).abs() < 1e-6);
//! ```
// The default build forbids `unsafe` outright. A backend feature
// drops `forbid` but keeps the crate-wide `deny`, so `unsafe`
// outside a scope-allowed backend module stays a compile error.
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;