Inkplay_
Inkplay_

Reputation: 561

How do you change require_grad to false for each parameters in your model in pytorch?

My code is below which I thought would do what I want but the output shows require_grad didn't change to false.

import torch
import torch.nn as nn

encoder = nn.Sequential( nn.Conv2d(1, 4, 1), nn.Sigmoid())

for params in encoder.parameters():

    params.require_grad = False
    print(params.requires_grad) # prints two True statements?

What am I doing wrong?

Upvotes: 1

Views: 1870

Answers (1)

Rohan Singh
Rohan Singh

Reputation: 471

You just have a typo :) Simply add an s at the end of grad in params.require_grad = False

Change this to params.requires_grad = False (note the added s)

Typos can be hard to catch sometimes ;)

Upvotes: 3

Related Questions