'Assign custom weight in pytorch

I'm trying to assign some custom weight to my PyTorch model but it doesn't work correctly.

class Mod(nn.Module):
    def __init__(self):
        super(Mod, self).__init__()
        
        self.linear = nn.Sequential(
            nn.Linear(1, 5)
        )
    def forward(self, x):
        x = self.linear(x)
        return x
mod = Mod()

mod.linear.weight = torch.tensor([1. ,2. ,3. ,4. ,5.], requires_grad=True)
mod.linear.bias = torch.nn.Parameter(torch.tensor(0., requires_grad=True))

print(mod.linear.weight)
>>> tensor([1., 2., 3., 4., 5.], requires_grad=True)

output = mod(torch.ones(1))
print(output)
>>> tensor([ 0.2657,  0.3220, -0.0726, -1.6987,  0.3945], grad_fn=<AddBackward0>)

The output is expected to be [1., 2., 3., 4., 5.] but it doesn't work as expected. What am I missing here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source