4

我使用 itorch notebook 构建了 nn 模型。

model = nn.Sequential()

model:add(nn.Reshape(ninputs))

model:add(nn.Linear(ninputs,noutputs))

向模型输入数据

output = model:forward(input)

然后,我打印模型并得到了这个。

print(model)

nn.Sequential {
  [input -> (1) -> (2) -> output]
  (1): nn.Reshape(3072)
  (2): nn.Linear(3072 -> 10)
}
{
  gradInput : DoubleTensor - empty
  modules : 
    {
      1 : 
        nn.Reshape(3072)
        {
          _input : DoubleTensor - empty
          nelement : 3072
          train : true
          output : DoubleTensor - size: 3072
          gradInput : DoubleTensor - empty
          size : LongStorage - size: 1
          _gradOutput : DoubleTensor - empty
          batchsize : LongStorage - size: 2
        }
      2 : 
        nn.Linear(3072 -> 10)
        {
          gradBias : DoubleTensor - size: 10
          weight : DoubleTensor - size: 10x3072
          train : true
          bias : DoubleTensor - size: 10
          gradInput : DoubleTensor - empty
          gradWeight : DoubleTensor - size: 10x3072
          output : DoubleTensor - size: 10
        }
    }
  train : true
  output : DoubleTensor - size: 10
}

如何读取 nn.linear 中的权重?

提前致谢。

4

2 回答 2

7

哦,和php差不多

model.modules[2].weight
于 2015-08-19T04:00:16.477 回答
1

我发现这model.modules[1].weight类似于model:get(1).weight,但两者都无法像残差块一样从表层获取参数。这样将残差块作为一层。

但是,我们params, gradParams = model:parameters()甚至可以在表格层中使用它来获取每个层的参数。

值得注意的是,第二种方式,将每一层的网络参数分为两层,分层排列

于 2016-12-27T12:38:06.743 回答