美文网首页工作生活
(二) autograd Automatic Different

(二) autograd Automatic Different

作者: 狼无雨雪 | 来源:发表于2019-07-04 18:58 被阅读0次
import torch
x = torch.ones(2, 2, requires_grad = True)
print(x)
tensor([[1., 1.],
        [1., 1.]], requires_grad=True)
y = x + 2
print(y)
tensor([[3., 3.],
        [3., 3.]], grad_fn=<AddBackward>)
print(y.grad_fn)
<AddBackward object at 0x7fd17e4422e8>
z = y * y * 3
out = z.mean()
print(z, out)
tensor([[27., 27.],
        [27., 27.]], grad_fn=<MulBackward>) tensor(27., grad_fn=<MeanBackward1>)
a= torch.randn(2,2)
a = ((a * 3) / (a - 1))
print(a.requires_grad)
a.requires_grad_(True)
print(a.requires_grad)
b = (a * a).sum()
print(b.grad_fn)
False
True
<SumBackward0 object at 0x7fd17e442a90>
out.backward()
print(x.grad)
tensor([[4.5000, 4.5000],
        [4.5000, 4.5000]])
x = torch.randn(3, requires_grad = True)

y = x * 2
while y.data.norm() < 1000:
    y = y * 2

print(y)
tensor([-1025.6301, -1234.8947,   -10.0212], grad_fn=<MulBackward>)
v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(v)
print(x.grad)
tensor([ 204.8000, 2048.0000,    0.2048])
print(x.requires_grad)
print((x ** 2).requires_grad)
with torch.no_grad():
    print((x ** 2).requires_grad)
True
True
False

相关文章

网友评论

    本文标题:(二) autograd Automatic Different

    本文链接:https://www.haomeiwen.com/subject/fwxdhctx.html