yutseho
yutseho

Reputation: 1689

[torch]how to read weights in nn model

I constructed the nn model using itorch notebook.

model = nn.Sequential()

model:add(nn.Reshape(ninputs))

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

Input data to the model

output = model:forward(input)

Then, I print the model and got this.

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
}

how to read the weight in nn.linear ?

Thanks in advance.

Upvotes: 4

Views: 6420

Answers (2)

linzhineng
linzhineng

Reputation: 11

I find that model.modules[1].weight is similar to model:get(1).weight, but both can't get the parameters from the table layer like residual block. In this way the residual block as a layer.

however, we can use params, gradParams = model:parameters() to get the parameters for each layer even in the table layer.

It is worth noting that, in the second way, each layer of the network parameters are divided into two layers and arranged in layers

Upvotes: 1

yutseho
yutseho

Reputation: 1689

Oh, it is similar to php

model.modules[2].weight

Upvotes: 7

Related Questions