Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[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.

like image 205
yutseho Avatar asked Feb 10 '23 06:02

yutseho


1 Answers

Oh, it is similar to php

model.modules[2].weight
like image 131
yutseho Avatar answered Feb 14 '23 10:02

yutseho