Save the model and parameters in to npz files


I have a model based on vgg16 net, I want to save its layers and each layers’ parameters(except the maxpooling layers) into separate npz files. just like the pictures showed. The parameters can be used for other programmes to use and test without installing Pytorch first.

if you have any ideas, plz letme know.
Thank you very much.

Maybe this could help you as a starter:

class MyNet(nn.Module):
    def __init__(self):
        super(MyNet, self).__init__()
        self.fc1 = nn.Linear(1, 5)
        self.fc2 = nn.Linear(5, 1)
    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.logsigmoid(self.fc2(x))
        return x
model = MyNet()

for child in model.named_children():
    layer_name = child[0]
    layer_params = {}
    for param in child[1].named_parameters():
        param_name = param[0]
        param_value = param[1].data.numpy()
        layer_params[param_name] = param_value
    # Save here using your syntax. E.g.
    save_name = layer_name + '.npy', layer_params)
1 Like

wow, that’s great help!!
but what if my layers are added in a sequential??

Using nn.Sequential will give you the same dict with layer names defined as ascending numbers.
Try this as a model definition and run the loop with it:

model_seq = nn.Sequential(
    nn.Linear(1, 5),
    nn.Linear(5, 1),

EDIT: Alternatively you could use an OrderedDict, if you need names:

from collections import OrderedDict

model_seq = nn.Sequential(OrderedDict([('fc1', nn.Linear(1, 5)),
                                       ('relu1', nn.ReLU()),
                                       ('fc2', nn.Linear(5, 1)),
                                       ('out', nn.LogSigmoid())

Thank you very much!!!