[torch] how to read weights in nn model

I built an nn model using itorch notebook.

model = nn.Sequential() model:add(nn.Reshape(ninputs)) model:add(nn.Linear(ninputs,noutputs)) 

Model Input

 output = model:forward(input) 

Then I print the model and get it.

 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 weight in nn.linear?

Thanks in advance.

+4
source share
2 answers

Oh it's like php

 model.modules[2].weight 
+7
source

I find that model.modules[1].weight is similar to model:get(1).weight , but both cannot get parameters from the table level, like a residual block. Thus, the residual block is like a layer.

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

It should be noted that in the second case, each level of network parameters is divided into two layers and located in layers

+1
source

All Articles