List All Trainable Variables in PyTorch – PyTorch Tutorial

By | December 17, 2021

In this tutorial, we will introduce how to list all trainable variables in pytorch, which is very useful when you are fine-tuning a model.

How to list all trainable variables in pytorch?

It is very easy to implement, here is an example:

vars = system.parameters()
for v in vars:
    if v.requires_grad:
        print(v.numel(), v.requires_grad)

Here system is a pytorch model, if a pytorch variable is trainable, its requires_grad will be True.

Run this code, you may view:

List All Trainable Variables in PyTorch - PyTorch Tutorial