【问题标题】:how to apply gradients manually in pytorch如何在pytorch中手动应用渐变
【发布时间】:2018-08-15 17:00:29
【问题描述】:

开始学习pytorch并尝试做一些非常简单的事情,尝试将随机初始化的大小为5的向量移动到值为[1,2,3,4,5]的目标向量。

但是我的距离并没有减少!!而我的矢量x 简直发疯了。不知道我错过了什么。

import torch
import numpy as np
from torch.autograd import Variable

# regress a vector to the goal vector [1,2,3,4,5]

dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU

x = Variable(torch.rand(5).type(dtype), requires_grad=True)
target = Variable(torch.FloatTensor([1,2,3,4,5]).type(dtype), 
requires_grad=False)
distance = torch.mean(torch.pow((x - target), 2))

for i in range(100):
  distance.backward(retain_graph=True)
  x_grad = x.grad
  x.data.sub_(x_grad.data * 0.01)

【问题讨论】:

  • 我的回答能解决你的问题吗?为什么不提供任何反馈?

标签: mathematical-optimization pytorch autograd


【解决方案1】:

您的代码中有两个错误会阻止您获得所需的结果。

第一个错误是您应该将距离计算放在循环中。因为在这种情况下,距离就是损失。所以我们必须在每次迭代中监控它的变化。

第二个错误是您应该手动将x.grad 归零,因为pytorch won't zero out the grad in variable by default

以下是按预期工作的示例代码:

import torch
import numpy as np
from torch.autograd import Variable
import matplotlib.pyplot as plt

# regress a vector to the goal vector [1,2,3,4,5]

dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU

x = Variable(torch.rand(5).type(dtype), requires_grad=True)
target = Variable(torch.FloatTensor([1,2,3,4,5]).type(dtype), 
requires_grad=False)

lr = 0.01 # the learning rate

d = []
for i in range(1000):
  distance = torch.mean(torch.pow((x - target), 2))
  d.append(distance.data)
  distance.backward(retain_graph=True)

  x.data.sub_(lr * x.grad.data)
  x.grad.data.zero_()

print(x.data)

fig, ax = plt.subplots()
ax.plot(d)
ax.set_xlabel("iteration")
ax.set_ylabel("distance")
plt.show()

以下是距离 w.r.t 迭代的图

我们可以看到模型在大约 600 次迭代时收敛。如果我们将学习率设置得更高(例如 lr=0.1),模型的收敛速度会更快(大约需要 60 次迭代,见下图)

现在,x 变成了下面的样子

0.9878 1.9749 2.9624 3.9429 4.9292

这非常接近您的目标 [1, 2, 3, 4, 5]。

【讨论】:

    猜你喜欢
    • 2023-01-22
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2019-08-27
    • 2019-10-19
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多