how to access parameter names

1,119 views
Skip to first unread message

Ao Li

unread,
Sep 8, 2017, 1:57:16 AM9/8/17
to torch7
I need to convert a torch model to pytorch. Since the torch model has layers that pytorch doesn't support(such as inception and LRN), it is not possible to use the build-in APIs. In order to convert such models from torch to pytorch, it is necessary to implement such layers in pytorch and save all the parameters from torch model as hdf5 file, and reload them to python as a dictionary. I'm new to lua and I would like to ask how to access the 'nickname' of all the parameters in torch.

btw, this can be easily done in pytorch, for example:

import torch.nn as nn
model
= nn.Sequential(
                nn
.Conv2d(in_channels=3,out_channels=32,kernel_size=7,stride=1,bias=False),
                nn
.ReLU(inplace=True),
                nn
.BatchNorm2d(num_features=32,affine=True),
                nn
.MaxPool2d(kernel_size=2,stride=2)
               
)
for key in model.state_dict():
    value
= model.state_dict().get(key)
   
print(key, value.size())



if all the parameters are accessible in a dictionary format, reconstructing a model in pytorch can be done in the following code:

model = MyNewInceptionModel()
model
.load_state_dict(param_dict)

ConvNetLearner

unread,
Sep 8, 2017, 4:00:07 PM9/8/17
to torch7
Best way is to look at the code/documentation for the layer types you are interested in, e.g., https://github.com/torch/nn/blob/master/README.md.  For example, nn.SpatialConvolution (https://github.com/torch/nn/blob/master/doc/convolution.md#nn.SpatialModules).

Each of the parameters can be accessed just like in structure members:

% th -lnn

 

  ______             __   |  Torch7                                         

 /_  __/__  ________/ /   |  Scientific computing for Lua. 

  / / / _ \/ __/ __/ _ \  |  Type ? for help                                

 /_/  \___/_/  \__/_//_/  |  https://github.com/torch         

                          |  http://torch.ch                  

th> model = nn.Sequential()

th> model:add(nn.SpatialConvolution(3, 16, 7, 7, 2, 2, 1, 1))

nn.Sequential {

  [input -> (1) -> output]

  (1): nn.SpatialConvolution(3 -> 16, 7x7, 2,2, 1,1)

}

th> model:add(nn.ReLU())

nn.Sequential {

  [input -> (1) -> (2) -> output]

  (1): nn.SpatialConvolution(3 -> 16, 7x7, 2,2, 1,1)

  (2): nn.ReLU

}


th> model

nn.Sequential {

  [input -> (1) -> (2) -> output]

  (1): nn.SpatialConvolution(3 -> 16, 7x7, 2,2, 1,1)

  (2): nn.ReLU

}


th> print(#model.modules)

2


th> model.modules[1].kW

7


th> model.modules[1].kH

7


th> model.modules[1].weight

(1,1,.,.) = 

 0.01 *

   6.2457 -4.3545  4.7430  4.8547 -5.9223  3.3388  0.7862

  -3.3725 -2.0295 -0.3997  7.7973 -5.2516  8.1476 -6.4627

   2.4958  0.2896  6.2000 -4.9034 -7.7588 -5.1510 -3.8102

   2.9991 -4.2541 -1.1643 -4.1169  1.4616  0.5252  0.9619

  -5.8441 -1.3345 -3.3810  7.3973 -4.3998  4.8422  0.0513

  -2.2906  6.6245 -0.5288 -6.3062  7.6088 -4.0588  0.0588

  -0.2463  5.1402  3.0234 -2.9900  2.6643 -5.9465 -2.9145

....

[torch.DoubleTensor of size 16x3x7x7]


th> model.modules[1].bias

0.01 *

-5.4751

-4.5497

-5.9375

-7.4779

 1.1394

-7.3193

 0.1563

 7.2150

 1.8851

-2.9125

 4.1081

-3.5006

-0.8639

-2.4370

 3.5407

 1.8183

[torch.DoubleTensor of size 16]

You get the idea.

Ao Li

unread,
Sep 10, 2017, 9:03:58 PM9/10/17
to torch7
thank you, It is convenient to access the param values, but is there any way to get the keys? The model includes inception layers, creating the names manually is a huge work. does torch support pre-defined param names?
Reply all
Reply to author
Forward
0 new messages