-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAutoGrad.cpp
34 lines (30 loc) · 980 Bytes
/
AutoGrad.cpp
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
/*
* @File : AutoGrad.cpp
* @Time : 2022/07/07
* @Author : Han Hui
* @Contact : [email protected]
*/
#include <iostream>
#include <torch/torch.h>
int main() {
// y = w * x + b
torch::Tensor x = torch::tensor({2.0});
torch::Tensor w = torch::tensor({3.0}, torch::requires_grad());
torch::Tensor b = torch::tensor({4.0}, torch::requires_grad());
torch::Tensor y = w * x + b;
y.backward();
std::cout << w.grad() << std::endl;
std::cout << b.grad() << std::endl;
std::cout << x.requires_grad() << std::endl;
x.requires_grad_(true);
std::cout << x.requires_grad() << std::endl;
// yy = xx * xx + 2 * xx
torch::Tensor xx = torch::randn({3}, torch::requires_grad());
torch::Tensor yy = xx * xx + 2 * xx;
yy.backward(torch::tensor({1, 2, 3}));
std::cout << xx << std::endl;
std::cout << xx.grad() << std::endl;
std::cout << xx * torch::tensor({2, 4, 6}) + torch::tensor({2, 4, 6})
<< std::endl;
return 0;
}