I wanted to freeze the first two layers of the network. Based on this
I wrote a code to freeze the first two layers like this before the optimisation line 105 on train.py
Here's the code
Freeze weights
for layer,param in enumerate(net.parameters()): if layer == 1 or layer == 2: param.requires_grad = False else: param.requires_grad = True
I'm getting this error on this line
optimizer = optim.SGD(net.parameters(), lr=args.lr,momentum=args.momentum, weight_decay=args.weight_decay)
File "train.py", line 155, in
optimizer = optim.SGD(net.parameters(), lr=args.lr,momentum=args.momentum, weight_decay=args.weight_decay)
File "/Users/name/.virtualenvs/test/lib/python3.6/site-packages/torch/optim/sgd.py", line 57, in __init__
super(SGD, self).__init__(params, defaults)
File "/Users/name/.virtualenvs/test/lib/python3.6/site-packages/torch/optim/optimizer.py", line 39, in __init__
self.add_param_group(param_group)
File "/Users/name/.virtualenvs/test/lib/python3.6/site-packages/torch/optim/optimizer.py", line 153, in add_param_group
raise ValueError("optimizing a parameter that doesn't require gradients")
ValueError: optimizing a parameter that doesn't require gradients
What's wrong any help would be appreciated. I'm stuck
Thanks to this article
Some important Pytorch tasks - A concise summary from a vision researcher
My code for freezing the layers in not exactly correct.
I was able to get the layers using this

OUTOUT

now to just freeze the vgg layer

OUTPUT

When the optimizer has to update the weights( by default requires_grad=True while using optimiser I guess)
So on using this

we get an error

just change the net.parameters() to filter(lambda p: p.requires_grad,net.parameters())

Most helpful comment
Thanks to this article
Some important Pytorch tasks - A concise summary from a vision researcher
My code for freezing the layers in not exactly correct.
I was able to get the layers using this

OUTOUT

now to just freeze the vgg layer
OUTPUT
When the optimizer has to update the weights( by default

requires_grad=Truewhile using optimiser I guess)So on using this
we get an error

just change the
net.parameters()tofilter(lambda p: p.requires_grad,net.parameters())