感谢:Pytorch深度学习实战 出版社:清华大学出版社
import numpy as np
import torch
Tensor的requires_grad属性设置为True时,Pytorch的torch.autograd会自动地追踪它的计算轨迹,当需要计算微分的时候,只需要对最终计算的tensor调用backward方法,中间所有的计算节点的微分就会被保存在grad属性。
a = torch.arange(9).view(3,3)
a.requires_grad
False
x = torch.rand(3,3,requires_grad = True)
print('微分后的X\n:',x)
微分后的X
: tensor([[0.4568, 0.3014, 0.1629],
[0.9983, 0.9308, 0.4588],
[0.7727, 0.1441, 0.9768]], requires_grad=True)
w = torch.ones(3,3,requires_grad = True)
y = torch.sum(torch.mm(x,w))
print(y)
tensor(15.6081, grad_fn=<SumBackward0>)
print(y.backward)
print(y.grad)
print(x.grad)
print(w.grad)
<bound method Tensor.backward of tensor(15.6081, grad_fn=<SumBackward0>)>
None
tensor([[3., 3., 3.],
[3., 3., 3.],
[3., 3., 3.]])
tensor([[2.2278, 2.2278, 2.2278],
[1.3763, 1.3763, 1.3763],
[1.5986, 1.5986, 1.5986]])
D:\install_file\Anaconda3\envs\5tsp\lib\site-packages\torch\_tensor.py:1013: UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the .grad field to be populated for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations. (Triggered internally at aten\src\ATen/core/TensorBody.h:417.)
return self._grad
x = torch.rand(3,3,requires_grad = True)
w = torch.rand(3,3,requires_grad = True)
print('x:',x)
print('w:',w)
yy = torch.mm(w,x)
detach_yy = yy.detach()
y = torch.mean(yy)
y.backward()
print('-'*60)
print(yy.grad)
print(detach_yy)
print(x.grad)
print(w.grad)
print('-'*60)
y = torch.sum(torch.mm(w,x))
print(y.requires_grad)
with torch.no_grad():
y = torch.sum(torch.mm(w,x))
print(y.requires_grad)
x: tensor([[0.2699, 0.2150, 0.4124],
[0.5506, 0.1069, 0.6992],
[0.1359, 0.9054, 0.6022]], requires_grad=True)
w: tensor([[0.3951, 0.9469, 0.1590],
[0.0279, 0.3368, 0.9199],
[0.0966, 0.1213, 0.7982]], requires_grad=True)
------------------------------------------------------------
None
tensor([[0.6496, 0.3302, 0.9208],
[0.3180, 0.8749, 0.8009],
[0.2013, 0.7564, 0.6053]])
tensor([[0.0577, 0.0577, 0.0577],
[0.1561, 0.1561, 0.1561],
[0.2086, 0.2086, 0.2086]])
tensor([[0.0997, 0.1507, 0.1826],
[0.0997, 0.1507, 0.1826],
[0.0997, 0.1507, 0.1826]])
------------------------------------------------------------
True
False
|