How to set requires_grad = false in C++?

Hello there!

I was wondering if it’s possible to set requires_grad = false for a model’s parameters in C++?

For instance, if I understood correctly, in Python this would be done as follows:

for param in model.parameters():
    param.requires_grad = False

What is the equivalent of this in C++?

Thank you very much.

Just add the following line in your code:

torch::NoGradGuard no_grad;

Hi Oktai15!

Thank you for your reply!

If I understood correctly, the NoGradGuard should, in essence, emulate the behaviour of requires_grad=false, correct?

If so, no parameters should change if I have the following, right? I.e., I should see the same bias before and after?

std::cout << "Biases before:\n" << policy->affine2-> << std::endl;

   torch::NoGradGuard no_grad;

std::cout << "Biases after:\n" << policy->affine2-> << std::endl;

It turns out it still changed the biases, do you know why?

Hmm, try to add this line to the same scope with model and loss