lr=0.001
x=np.linspace(-6,6,120)
y=0.5+3*x-x**2+np.exp(-0.4*x)
x=torch.from_numpy(x)
y=torch.from_numpy(y)
w0=torch.tensor(0.1,requires_grad=True)
w1=torch.tensor(0.1,requires_grad=True)
w2=torch.tensor(0.1,requires_grad=True)
w3=torch.tensor(0.1,requires_grad=True)
Y=w0+w1*x+w2*x**2+torch.exp(w3*x)
#opt=torch.optim.Adam([w0,w1,w2,w3],0.001)
L=nn.MSELoss()
for i in range(20):
opt.zero_grad()
err=L(Y,y)
print(err)
err.backward(retain_graph=True)
with torch.no_grad():
w0=w0-lr*w0.grad
w1=w1-lr*w1.grad
w2=w2-lr*w2.grad
w3=w3-lr*w3.grad
w0.grad.zero_()
w1.grad.zero_()
w2.grad.zero_()
w3.grad.zero_()
from the line w0 = w0 - lr * w0.grad:
===> unsupported operand type(s) for *: 'float' and 'NoneType'
from the line w0.grad.zero_():
===> 'NoneType' object has no attribute 'zero_'
comes up.
How should I fix it?
And
x=np.linspace(-6,6,120) y=0.5+3*x-x**2+np.exp(-0.4*x) x=torch.from_numpy(x) y=torch.from_numpy(y) w0=torch.tensor(0.1,requires_grad=True) w1=torch.tensor(0.1,requires_grad=True) w2=torch.tensor(0.1,requires_grad=True) w3=torch.tensor(0.1,requires_grad=True) Y=w0+w1*x+w2*x**2+torch.exp(w3*x) opt=torch.optim.Adam([w0,w1,w2,w3],0.001) L=nn.MSELoss() for i in range(20): opt.zero_grad() err=L(Y,y) print(err) err.backward(retain_graph=True) opt.step()
If I try this code, then err is not updated. What is the problem? and how should I fix it
- Further, should the input of
nn.MSELoss()be torch.double? Sometimes I getexpected dtype Double but got dtype Floaterror.
What should be the type of parameters w0, w1, ...?