Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教--关于detach #60

Open
A-Pai opened this issue Jun 23, 2024 · 1 comment
Open

请教--关于detach #60

A-Pai opened this issue Jun 23, 2024 · 1 comment

Comments

@A-Pai
Copy link

A-Pai commented Jun 23, 2024

书写得很全面细致,关于detach,请教:网上很多地方都没有讲清楚detach,特别是将其值改变了之会怎么样,例如:
https://zhuanlan.zhihu.com/p/505445223 中的第一段代码,但是如果将其中“a = a0.tanh()”改为“a = a0.sin()”,如下,就可以正常运行了--没有报错,why?我的版本是2.3.0+cu121

import torch
a0 = torch.tensor([1.1, 2.2, 3.3], requires_grad = True)
a = a0.sin()
print('a=',a)
print('a.requires_grad=',a.requires_grad)
a_detach = a.detach()
print('a_detach=',a_detach)
print('a_detach.requires_grad=', a_detach.requires_grad)

a_detach.zero_()
print('a_detach=',a_detach)
print('a=',a)
print('a.requires_grad=',a.requires_grad) # 此时对原来的a求导
a.sum().backward()
print(a0.grad)

PixPin_2024-06-23_14-22-40

@A-Pai
Copy link
Author

A-Pai commented Jun 23, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant