PyTorch,nn.Sequential(),访问nn.Sequential()中特定模块的权重

时间:2017-05-31 12:12:31

标签: python pytorch

这应该是一个快速的。当我在PyTorch中使用预定义模块时,我通常可以非常轻松地访问其权重。但是,如果我先将模块包装在nn.Sequential()中,如何访问它们?请参阅下面的玩具示例

class My_Model_1(nn.Module):
    def __init__(self,D_in,D_out):
        super(My_Model_1, self).__init__()
        self.layer = nn.Linear(D_in,D_out)
    def forward(self,x):
        out = self.layer(x)
        return out

class My_Model_2(nn.Module):
    def __init__(self,D_in,D_out):
        super(My_Model_2, self).__init__()
        self.layer = nn.Sequential(nn.Linear(D_in,D_out))
    def forward(self,x):
        out = self.layer(x)
        return out

model_1 = My_Model_1(10,10)
print(model_1.layer.weight)
model_2 = My_Model_2(10,10)
# How do I print the weights now?
# model_2.layer.0.weight doesn't work.

3 个答案:

答案 0 :(得分:8)

轻松访问权重是使用模型的state_dict()

这适用于您的情况:

for k, v in model_2.state_dict().iteritems():
    print("Layer {}".format(k))
    print(v)

另一个选择是获取modules()迭代器。如果您事先知道图层的类型,这也应该有效:

for layer in model_2.modules():
   if isinstance(layer, nn.Linear):
        print(layer.weight)

答案 1 :(得分:5)

PyTorch forum开始,这是推荐的方法:

model_2.layer[0].weight

答案 2 :(得分:0)

您可以使用_modules通过名称访问模块:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        self.conv1 = nn.Conv2d(3, 3, 3)

    def forward(self, input):
        return self.conv1(input)

model = Net()
print(model._modules['conv1'])
相关问题