Autograd:自动求导

import torch
# 创建张量并设置requires_grad=True来追踪其计算历史
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=<AddBackward0>)
print(y.grad_fn)
<AddBackward0 object at 0x0000000007BFC688>
z = y * y * 3
out = z.mean()
print(z, out)
tensor([[27., 27.],
        [27., 27.]], grad_fn=<MulBackward0>) tensor(27., grad_fn=<MeanBackward1>)
# .requires_grad_(...)可以改变现有张量的requires_grad属性,默认flag为False
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 0x0000000007BEBFC8>
x1 = torch.ones(2,2,requires_grad=True)
z1 = 3 * (x1 + 2) * (x1 + 2)
out = z1.mean()
# z1 = 3(x1+2)^2, out = mean(z1)
print("x1 = ", x1)
print("z1 = ", z1)
# out是一个纯量(scalar),out.backward()相当于out.backward(torch,tensor(1))
out.backward()
# x.grad 梯度d(out)/dx
print(x1.grad)
# 清零
x1.grad.data.zero_()
x1 =  tensor([[1., 1.],
        [1., 1.]], requires_grad=True)
z1 =  tensor([[27., 27.],
        [27., 27.]], grad_fn=<MulBackward0>)
tensor([[4.5000, 4.5000],
        [4.5000, 4.5000]])





tensor([[0., 0.],
        [0., 0.]])
x = torch.randn(3, requires_grad=True)
y = x * 2
while y.data.norm() < 1000: # .data.norm() 张量L2范数
    y = y * 2
print("y =",y)

gradients = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(gradients)
print("grad =", x.grad)
y = tensor([1203.4269, 1136.8713,  854.0352], grad_fn=<MulBackward0>)
grad = tensor([2.0480e+02, 2.0480e+03, 2.0480e-01])
print(x.requires_grad)
print((x ** 2).requires_grad)
# 将变量包裹在with torch.no_grad()中,可以暂时屏蔽autograd计算
with torch.no_grad():
    print((x ** 2).requires_grad)
print((x ** 2).requires_grad)
True
True
False
True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值