I am looking for a method to retrieve the activation functions used in a PyTorch network saved as a .pth file (torch.save(model)
). Indeed, if the activation functions were not declared in the class when creating the model but only in the forward method, I am unable to identify these activation functions
eg:
class SimpleCNN_2(nn.Module):
def __init__(self):
super(SimpleCNN_2, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(in_features=32768, out_features=128)
self.fc2 = nn.Linear(in_features=128, out_features=10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = nn.Flatten()(x)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
I get this description:
SimpleCNN_2(
(conv1): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(conv2): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(fc1): Linear(in_features=32768, out_features=128, bias=True)
(fc2): Linear(in_features=128, out_features=10, bias=True)
)
If activation function are declared, I don’t have any issue. My concern is about the fact that I want to assess a network from a .pth file, and if I can’t get the whole structure of the network it’s a mess.
I would like to have a method that allows me to obtain the complete structure of a network, layer by layer, from a .pth file, in the form of a list, without knowing the structure of the network in advance.
Guillaume BERTHELOT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.