简体   繁体   中英

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?

You are not updating the weights in the right place. Your self.linear is not a nn.Linear layer, but rather a nn.Sequential container. Your nn.Linear is the first layer in the sequential. To access it you need to index self.linear :

with torch.no_grad():
  mod.linear[0].weight.data = torch.tensor([1. ,2. ,3. ,4. ,5.], requires_grad=True)[:, None]
  mod.linear[0].bias.data = torch.zeros((5, ), requires_grad=True)  # bias is not a scalar here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM