Dive-into-DL-PyTorch icon indicating copy to clipboard operation
Dive-into-DL-PyTorch copied to clipboard

关于2.3_autograd.md中修改tensor.data的值不会影响反向传播

Open zhkuo24 opened this issue 4 years ago • 9 comments

修改tensor.data的值不会影响反向传播,有点不太懂? 如果y=x^2,此时修改x.data的值,会影响x.grad的值,不知道这个地方怎么理解? image image

zhkuo24 avatar Oct 29 '19 11:10 zhkuo24

我也遇到这个问题了,你的pytorch版本是多少呀

wang-zm18 avatar Nov 10 '19 02:11 wang-zm18

我也遇到这个问题了,你的pytorch版本是多少呀

我的是1.3

zhkuo24 avatar Nov 11 '19 08:11 zhkuo24

这应该不是bug。y = x^2, y对x求导得2x,所以把x的data乘以100的话最后导数也会乘以100.

ShusenTang avatar Nov 12 '19 14:11 ShusenTang

这应该不是bug。y = x^2, y对x求导得2x,所以把x的data乘以100的话最后导数也会乘以100.

如果我们想要修改tensor的数值,但是又不希望被autograd记录(即不会影响反向传播),那么我么可以对tensor.data进行操作。

x.data *= 100 # 只改变了值,不会记录在计算图,所以不会影响梯度传播

y.backward()
print(x) # 更改data的值也会影响tensor的值
print(x.grad)

不太理解“不会影响反向传播”是什么意思,感觉是修改了tensor的数值,但是该值的倒数grad不会改变?但是实际上x.data的值改了之后,x.grad也会改变,这算不算影响反向传播。

zhkuo24 avatar Nov 22 '19 08:11 zhkuo24

Unscribe. I dont want your e. mejl

Den tis 29 okt. 2019 12:49zhkuo [email protected] skrev:

修改tensor.data的值不会影响反向传播,有点不太懂? 如果y=x^2,此时修改x.data的值,会影响x.grad的值,不知道这个地方怎么理解? [image: image] https://user-images.githubusercontent.com/9025284/67764448-f421e580-fa84-11e9-8e1f-ccbdb0fca407.png [image: image] https://user-images.githubusercontent.com/9025284/67764525-259ab100-fa85-11e9-9d62-034d4b79e2f8.png

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ShusenTang/Dive-into-DL-PyTorch/issues/55?email_source=notifications&email_token=AMMV2XXRSVO4XMDC4XDHVALQRAPLTA5CNFSM4JGHLFP2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HVA5XAQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMMV2XRN5KB3MOMFJ55QI7LQRAPLTANCNFSM4JGHLFPQ .

FLINGIUS avatar Nov 22 '19 10:11 FLINGIUS

我也发现这个问题了,而且x.data = 100 在y=x^2之前和之后得到的结果也不一样: x.data = 100 在y=2x^2之前: import torch x = torch.ones(1,requires_grad=True) x.data=100 # 只改变了值,不会记录在计算图,所以不会影响梯度传播 y = 2xx y.backward() print(x) # 更改data的值也会影响tensor的值 print(x.grad) 结果为: tensor([100.], requires_grad=True) tensor([400.]) 这说明x.data变化也会影响x.grad,是否和原文中的不影响反向传播冲突了?

x.data = 100 在y=2x^2之后: import torch x = torch.ones(1,requires_grad=True) y = 2xx x.data*=100 # 只改变了值,不会记录在计算图,所以不会影响梯度传播 y.backward() print(x) # 更改data的值也会影响tensor的值 print(x.grad) 结果为: tensor([100.], requires_grad=True) tensor([202.]) 不清楚x.grad=202是怎么计算出来的?

myjxm avatar Nov 30 '19 15:11 myjxm

你是不是发错邮件了

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: myjxm <[email protected]> 发送时间: 2019年11月30日 23:10 收件人: ShusenTang/Dive-into-DL-PyTorch <[email protected]> 抄送: Subscribed <[email protected]> 主题: 回复:[ShusenTang/Dive-into-DL-PyTorch] 关于2.3_autograd.md中修改tensor.data的值不会影响反向传播 (#55)

我也发现这个问题了,而且x.data = 100 在y=x^2之前和之后得到的结果也不一样: x.data = 100 在y=2x^2之前: import torch x = torch.ones(1,requires_grad=True) x.data=100 # 只改变了值,不会记录在计算图,所以不会影响梯度传播 y = 2xx y.backward() print(x) # 更改data的值也会影响tensor的值 print(x.grad) 结果为: tensor([100.], requires_grad=True) tensor([400.]) 这说明x.data变化也会影响x.grad,是否和原文中的不影响反向传播冲突了?

x.data = 100 在y=2x^2之后: import torch x = torch.ones(1,requires_grad=True) y = 2xx x.data*=100 # 只改变了值,不会记录在计算图,所以不会影响梯度传播 y.backward() print(x) # 更改data的值也会影响tensor的值 print(x.grad) 结果为: tensor([100.], requires_grad=True) tensor([202.]) 不清楚x.grad=202是怎么计算出来的?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

hehaoming avatar Nov 30 '19 16:11 hehaoming

我想去官网搜一下,但并搜不到 tensor.data,官网上 Tensor 类底下只介绍了各种方法,而有一个 Tensor Attributes 的页面下也没有介绍 tensor.data。

risinyoung avatar Dec 06 '19 14:12 risinyoung

的确是,不太理解不影响反向传播是啥意思

1053399472 avatar Oct 05 '21 09:10 1053399472