Inconsistent results between forward and weighs usage

Hi, I’ve traind a super-simple linear regression model:

class LinearRegressionNet(nn.Module):
    def __init__(self, in_size, out_size):
        super(LinearRegressionNet, self).__init__()
        self.layer = nn.Linear(in_size, out_size)

    def forward(self, x):
        return self.layer(x)

I’ve train this net using nn.SmoothL1Loss, for 200 epochs with batch_size=32.

The loss decreased to 1e-6, and I tried to print the predictions.
Consider my data is X and the labels are all 0, this is the prediction code:

with torch.no_grad():
    X_torch = torch.from_numpy(X.astype(np.float64).to('cuda').float())
    print(model(X_torch).mean())

the output is: 0.023 which way far from 1e-6. Then, I tried the straightforward way:

w = model.weight.data.cpu().numpy()
print(np.dot(X, w.T).mean())

the output is -0.0015, which is more convincible.

How could it be? the output should be consistent within both code chunks

Hi,

Don’t you forget the bias term of nn.Linear?

To mimic pytorch with numpy, I think the code will be

np.dot(x, w.T) + model.bias.data.numpy()