简体   繁体   English

PyTorch 中的简单梯度下降优化器不起作用

[英]Simple gradient descent optimizer in PyTorch not working

I'm trying to implement a simple minimizer in PyTorch, here is the code ( v and q and v_trans are tensors, and eta is 0.01):我正在尝试在 PyTorch 中实现一个简单的最小化器,这是代码( vqv_trans是张量, eta是 0.01):

for i in range(10):
    print('i =', i, ' q =', q)
    v_trans = forward(v, q)
    loss = error(v_trans, v_target)
    q.requires_grad = True
    loss.backward()
    grads = q.grad
    with torch.no_grad()
        q = q - eta * grads

print('Final q = ', q)

On the second iteration of the loop, I get an error at the line "loss.backward()":在循环的第二次迭代中,“loss.backward()”行出现错误:

Traceback (most recent call last):
  File "C:\Scripts\main.py", line 97, in <module>
    loss.backward()
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\_tensor.py", line 307, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\autograd\__init__.py", line 154, in backward
    Variable._execution_engine.run_backward(
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

I've tried several things and cannot get this simple example to work.我已经尝试了几件事,但无法让这个简单的例子起作用。 Is there a tutorial/guide/documentation on how to make a simple optimizer for a project that doesn't involve neural networks?是否有关于如何为不涉及神经网络的项目制作简单优化器的教程/指南/文档? Or maybe, how to use the optimizers built in PyTorch for non-NN projects?或者,如何将 PyTorch 中内置的优化器用于非 NN 项目?

Here is a simple example of finding a zero (or local minimum) of a function (in this case这是一个查找函数零(或局部最小值)的简单示例(在这种情况下功能 ). )。

# the loss function 
def mse(Y, target):
    diff = target - Y 
    return (diff * diff).sum() / 2
# this is the variable 
X = torch.rand(1, requires_grad=True) 
# this is our learning rate
lr = 1e-3 
# this is the learning loop
for i in range(0, 1000):
    # here the actual function 
    Y=X*X+3*X+1.0
    # we are looking for 0
    loss = mse(Y, 0)
    loss.backward()
    if i % 100 == 0:
        print("X", X.item(),"loss", loss.item(), "grad", X.grad.item())
    with torch.no_grad():
        X -= X.grad * lr
        X.grad.zero_()
print("result found (could be local minimum): ", X.item(), "/", Y.item())

It should give you an output similar to this:它应该为您提供类似于以下的输出:

X 0.45342570543289185 loss 3.2918500900268555 grad 10.024480819702148
X -0.048841409385204315 loss 0.3662492334842682 grad 2.483980894088745
X -0.21245644986629486 loss 0.08313754200935364 grad 1.0500390529632568
X -0.2880801260471344 loss 0.02392572909593582 grad 0.5302143096923828
X -0.3278542160987854 loss 0.007678795140236616 grad 0.2905181050300598
X -0.35011476278305054 loss 0.0026090284809470177 grad 0.16612648963928223
X -0.3629951477050781 loss 0.0009150659898295999 grad 0.09728223085403442
X -0.37058910727500916 loss 0.00032688589999452233 grad 0.0577557273209095
X -0.3751157820224762 loss 0.0001180334365926683 grad 0.0345664918422699
X -0.377831369638443 loss 4.289587013772689e-05 grad 0.020787909626960754
result found (could be local minimum):  -0.37946680188179016 / 0.00562286376953125

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM