How does one check if a tensor/parameter in pytorch is trainable?

How does one check if a tensor/parameter in pytorch is trainable?

1 Like

check if it has flag requires_grad to true:

Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> x = torch.randn(3,4)
>>> x
tensor([[ 0.5905,  0.7622,  0.5487,  0.4738],
        [-0.2488,  0.1633,  2.3185, -0.6563],
        [ 1.0734, -0.3375, -0.5587,  0.3377]])
>>> x.grad
>>> x.requires_grad
False
>>> x.requires_grad = True
>>> x
tensor([[ 0.5905,  0.7622,  0.5487,  0.4738],
        [-0.2488,  0.1633,  2.3185, -0.6563],
        [ 1.0734, -0.3375, -0.5587,  0.3377]], requires_grad=True)
>>>

source: https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html

1 Like

Yes a Tensor will have it’s .grad field populated if requires_grad=True and is_leaf=True.

1 Like