TingsongYu / TingsongYu/PyTorch_Tutorial

请教--关于detach

Open
#60 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided code and the linked Zhihu example, reproducing both the tanh and sin cases with PyTorch 2.3.0+cu121. Trace how detach, the in-place zero_ call, and backward behave in each case. Done means documenting the reason for the different outcomes; the issue names no project file or test location.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.