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.
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():
print(child)
layer_name = child[0]
layer_params = {}
for param in child[1].named_parameters():
print(param)
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'
np.save(save_name, layer_params)
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: