Closed mcksshg closed 4 years ago
Thank you for this great project. When I ported a PyTorch sample code to torch-rb, I met an error.
require "torch" require "npy" # y = 2 x1 + 3 x2 w_true = Torch::Tensor.new([2,3]) x = Torch.randn(100,2) y = Torch.mv(x, w_true) + Torch.randn(100)*0.5 net = Torch::NN::Linear.new(2, 1, bias: false) optimizer = Torch::Optim::SGD.new(net.parameters, lr: 0.1) loss_fn = Torch::NN::MSELoss.new 100.times do |epoc| optimizer.zero_grad y_pred = net.call(x) loss = loss_fn.call(y_pred.view_as(y),y) loss.backward optimizer.step puts loss.item puts net.parameters end
Error message is
53:in `block (2 levels) in zero_grad': undefined method `grad' for nil:NilClass (NoMethodError)
It seems that unbiased linear model does not work in using `grad'.
If I change the bias into true, it works correctly:
net = Torch::NN::Linear.new(2, 1, bias: true)
Corresponding python code is here
from torch import nn, optim import torch # y = 2 x1 + 3 x2 w_true = torch.Tensor([2,3]) x = torch.randn(100,2) y = torch.mv(x, w_true) + torch.randn(100)*0.5 net = nn.Linear(2, 1, bias = False) optimizer = optim.SGD(net.parameters(), lr= 0.1) loss_fn = nn.MSELoss() for epoc in range(100): optimizer.zero_grad() y_pred = net(x) loss = loss_fn(y_pred.view_as(y),y) loss.backward() optimizer.step() print(loss.item()) print(list(net.parameters()))
Hey @mcksshg, thanks for reporting and the great repro steps! Fixed on master.
Thank you for this great project. When I ported a PyTorch sample code to torch-rb, I met an error.
Example Code
Result
Error message is
It seems that unbiased linear model does not work in using `grad'.
Biased Linear Model
If I change the bias into true, it works correctly:
Python Code
Corresponding python code is here