Skip to content

请教--关于detach #60

Description

@A-Pai

书写得很全面细致,关于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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions