Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

neural network - Inplace parameter updation without torch.no_grad()

I have just started learning this awesome tool called PyTorch but sadly I am stuck in an equivocal situation.

Below is a code snippet from one of the tutorials

 with torch.no_grad():
       weights -= weights.grad * lr
       bias -= bias.grad * lr
       weights.grad.zero_()
       bias.grad.zero_()

I am kind of confused that even if I will do parameter update without using torch.no_grad() ( i.e. only in-place ) like this:-

 # with torch.no_grad()
   weights -= weights.grad * lr
   bias -= bias.grad * lr
   weights.grad.zero_()
   bias.grad.zero_()

and since the backward call has already been made in the code above this snippet( not included in the snippet ) which basically means all the “grad” attributes are already computed and don’t require the “original” values again. Then, why is it illegal to do those operations without torch.no_grad()?

I know it will flag off the error in PyTorch but I just wanted to know where my line of thought is at fault?

question from:https://stackoverflow.com/questions/65842822/inplace-parameter-updation-without-torch-no-grad

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The questions have been solved by one of the PyTorch Moderator on the PyTorch Discussion forum.

Here's the link to it :- Inplace parameter updation without torch.no_grad()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...