我正在尝试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
是:
我的目标是通过使用out
的斜率调整[-.00001 , 0.0001]
,使W
接近0(在W
的间隔内。
我应该如何从这里开始使用pytorch实现此目的?
@ Umang:这是我运行您建议的代码时得到的:
实际上算法有所不同。
# 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)