Thanks for your reply!
What I want to do is actually very simple. Let’s use AlexNet as an example.
Assume I have an AlexNet and I have no idea what is the architecture and the naming convention of that network. Moreover, I have the final logits given by
logits = model_alexnet(input_var).
I would like to trace back from logits and get the following layers in order: FC8 (nn.Conv2d) -> ReLU_FC7 (nn.ReLU) -> FC7 (nn.Conv2d) -> ReLU_FC6 (nn.ReLU) -> … -> ReLU_CONV1 (nn.ReLU) -> CONV1 (nn.Conv2d). Moreover, I need to know what are the weights and biases in each layer and what are the activations of the input and output feature maps of each layer.
print is not what I want because I would like to modify the parameters of a layer based on the parameters of its near-by layers.
I know I can use state_dict() to extract all the parameters, but it does not tell what is the connection between each layer. Especially, Pytorch is a flexible library and we have different ways to define a network. For example, we can put all the layers in a single nn.Sequential. We can also put all the convolutional layers into a nn.Sequential, put all the fully-connected layers into another nn.Sequential and put the two nn.Sequential into another nn.Sequential. I wonder whether there is a universal way I can easily traverse the network and get all the feature maps and parameters.
Thanks!