Burn-readme-2.rs
· 497 B · Rust
原始文件
use burn::backend::{Autodiff, Wgpu};
use burn::tensor::{Distribution, Tensor};
fn main() {
type Backend = Autodiff<Wgpu>;
let x: Tensor<Backend, 2> = Tensor::random([32, 32], Distribution::Default);
let y: Tensor<Backend, 2> = Tensor::random([32, 32], Distribution::Default).require_grad();
let tmp = x.clone() + y.clone();
let tmp = tmp.matmul(x);
let tmp = tmp.exp();
let grads = tmp.backward();
let y_grad = y.grad(&grads).unwrap();
println!("{y_grad}");
1 | use burn::backend::{Autodiff, Wgpu}; |
2 | use burn::tensor::{Distribution, Tensor}; |
3 | |
4 | fn main() { |
5 | type Backend = Autodiff<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}"); |