PyTorch中具有反向传播和梯度下降的查找参数

问题描述 投票:0回答:1

我正在尝试PyTorch以及自动分化和梯度下降

为此,我想估计将对参数函数中的任意线性产生一定值的参数。

我的代码在这里:

import torch

X = X.astype(float)

X = np.array([[3.], [4.], [5.]])

X = torch.from_numpy(X)

X.requires_grad = True

W = np.random.randn(3,3)

W = np.triu(W, k=0)

W = torch.from_numpy(W)

W.requires_grad = True

out = 10 - ([email protected](X, 1,0) * W).sum()

out是:

enter image description here

我的目标是通过使用out的斜率调整[-.00001 , 0.0001],使W接近0(在W的间隔内。

我应该如何从这里开始使用pytorch实现此目的?

更新

@ Umang:这是我运行您建议的代码时得到的:

enter image description here

实际上算法有所不同。

python-3.x pytorch gradient-descent backpropagation
1个回答
1
投票
# your code as it is
import torch
import numpy as np 

X = np.array([[3.], [4.], [5.]])
X = torch.from_numpy(X)
X.requires_grad = True
W = np.random.randn(3,3)
W = np.triu(W, k=0)
W = torch.from_numpy(W)
W.requires_grad = True

# define parameters for gradient descent
max_iter=100
lr_rate = 1e-3

# we will do gradient descent for max_iter iteration, or convergence till the criteria is met.
i=0
out = compute_out(X,W)
while (i<max_iter) and (torch.abs(out)>0.01):
    loss = (out-0)**2
    W = W - lr_rate*torch.autograd.grad(loss, W)[0]
    i+=1
    print(f"{i}: {out}")
    out = compute_out(X,W)

print(W)
© www.soinside.com 2019 - 2024. All rights reserved.