[英]Assign custom weight in pytorch
I'm trying to assign some custom weight to my PyTorch model but it doesn't work correctly.我正在尝试为我的 PyTorch model 分配一些自定义权重,但它无法正常工作。
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. output 预计为 [1., 2., 3., 4., 5.] 但它没有按预期工作。 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.您的
self.linear
不是nn.Linear
层,而是nn.Sequential
容器。 Your nn.Linear
is the first layer in the sequential.您的
nn.Linear
是顺序中的第一层。 To access it you need to index self.linear
:要访问它,您需要索引
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.