Skip to content

Commit 1fc4afb

Browse files
authored
Merge pull request #730 from albanD/autograd_blitz
Update autograd blitz
2 parents b4134ac + b37d6cb commit 1fc4afb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

beginner_source/blitz/autograd_tutorial.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,23 @@
174174

175175
###############################################################
176176
# You can also stop autograd from tracking history on Tensors
177-
# with ``.requires_grad=True`` by wrapping the code block in
177+
# with ``.requires_grad=True`` either by wrapping the code block in
178178
# ``with torch.no_grad():``
179179
print(x.requires_grad)
180180
print((x ** 2).requires_grad)
181181

182182
with torch.no_grad():
183183
print((x ** 2).requires_grad)
184184

185+
###############################################################
186+
# Or by using ``.detach()`` to get a new Tensor with the same
187+
# content but that does not require gradients:
188+
print(x.requires_grad)
189+
y = x.detach()
190+
print(y.requires_grad)
191+
print(x.eq(y).all())
192+
193+
185194
###############################################################
186195
# **Read Later:**
187196
#

0 commit comments

Comments
 (0)