knox 修订了这个 Gist . 跳至此修订
1 file changed, 17 insertions
Burn-readme-3.rs(file created)
@@ -0,0 +1,17 @@ | |||
1 | + | use burn::backend::{Autodiff, Fusion, Wgpu}; | |
2 | + | use burn::tensor::{Distribution, Tensor}; | |
3 | + | ||
4 | + | fn main() { | |
5 | + | type Backend = Autodiff<Fusion<Wgpu>>; | |
6 | + | ||
7 | + | let x: Tensor<Backend, 2> = Tensor::random([32, 32], Distribution::Default); | |
8 | + | let y: Tensor<Backend, 2> = Tensor::random([32, 32], Distribution::Default).require_grad(); | |
9 | + | ||
10 | + | let tmp = x.clone() + y.clone(); | |
11 | + | let tmp = tmp.matmul(x); | |
12 | + | let tmp = tmp.exp(); | |
13 | + | ||
14 | + | let grads = tmp.backward(); | |
15 | + | let y_grad = y.grad(&grads).unwrap(); | |
16 | + | println!("{y_grad}"); | |
17 | + | } |
更新
更早