|
| 1 | +// Copyright 2020-present pytorch-cpp Authors |
| 2 | +#include <torch/torch.h> |
| 3 | +#include <iostream> |
| 4 | +#include <iomanip> |
| 5 | + |
| 6 | +int main() { |
| 7 | + std::cout << "Deep Learning with PyTorch: A 60 Minute Blitz\n\n"; |
| 8 | + std::cout << "Autograd: Automatic Differentiation\n\n"; |
| 9 | + |
| 10 | + std::cout << "Tensor\n\n"; |
| 11 | + |
| 12 | + // Create a tensor and set requires_grad=True to track computation with it: |
| 13 | + auto x = torch::ones({2, 2}, torch::TensorOptions().requires_grad(true)); |
| 14 | + std::cout << "x:\n" << x << '\n'; |
| 15 | + |
| 16 | + // Do a tensor operation: |
| 17 | + auto y = x + 2; |
| 18 | + std::cout << "y:\n" << y << '\n'; |
| 19 | + |
| 20 | + // y was created as a result of an operation, so it has a grad_fn: |
| 21 | + std::cout << "y.grad_fn:\n" << y.grad_fn() << '\n'; |
| 22 | + |
| 23 | + // Do more operations on y: |
| 24 | + auto z = y * y * 3; |
| 25 | + auto out = z.mean(); |
| 26 | + std::cout << "z:\n" << z << "out:\n" << out << '\n'; |
| 27 | + |
| 28 | + // .requires_grad_(...) changes an existing Tensor’s requires_grad flag in-place: |
| 29 | + auto a = torch::randn({2, 2}); |
| 30 | + a = ((a * 3) / (a - 1)); |
| 31 | + std::cout << a.requires_grad() << '\n'; |
| 32 | + a.requires_grad_(true); |
| 33 | + std::cout << a.requires_grad() << '\n'; |
| 34 | + auto b = (a * a).sum(); |
| 35 | + std::cout << b.grad_fn() << '\n'; |
| 36 | + |
| 37 | + std::cout << "Gradients\n\n"; |
| 38 | + |
| 39 | + // Let’s backprop now: |
| 40 | + out.backward(); |
| 41 | + |
| 42 | + // Print gradients d(out)/dx: |
| 43 | + std::cout << "x.grad:\n" << x.grad() << '\n'; |
| 44 | + |
| 45 | + // Example of vector-Jacobian product: |
| 46 | + x = torch::randn(3, torch::TensorOptions().requires_grad(true)); |
| 47 | + y = x * 2; |
| 48 | + while (y.data().norm().item<int>() < 1000) { |
| 49 | + y = y * 2; |
| 50 | + } |
| 51 | + std::cout << "y:\n" << y << '\n'; |
| 52 | + |
| 53 | + // Simply pass the vector to backward as argument: |
| 54 | + auto v = torch::tensor({0.1, 1.0, 0.0001}, torch::TensorOptions(torch::kFloat)); |
| 55 | + y.backward(v); |
| 56 | + std::cout << "x.grad:\n" << x.grad() << '\n'; |
| 57 | + |
| 58 | + // Stop autograd from tracking history on Tensors with .requires_grad=True: |
| 59 | + std::cout << "x.requires_grad\n" << x.requires_grad() << '\n'; |
| 60 | + std::cout << "(x ** 2).requires_grad\n" << (x * x).requires_grad() << '\n'; |
| 61 | + torch::NoGradGuard no_grad; |
| 62 | + std::cout << "(x ** 2).requires_grad\n" << (x * x).requires_grad() << '\n'; |
| 63 | + |
| 64 | + // Or by using .detach() to get a new Tensor with the same content but that does not require gradients: |
| 65 | + std::cout << "x.requires_grad:\n" << x.requires_grad() << '\n'; |
| 66 | + y = x.detach(); |
| 67 | + std::cout << "y.requires_grad:\n" << y.requires_grad() << '\n'; |
| 68 | + std::cout << "x.eq(y).all():\n" << x.eq(y).all() << '\n'; |
| 69 | +} |
0 commit comments