You can use register_forward_hook for whichever intermediate layer you want. Check out my previous post below on extracting features.

  
    
    
    How to get input&output tensor of a module in backward process autograd
  
  
    You can store the forward results in an attribute. Simply saving resnet18’s output after first 7x7 conv… 
class Net(nn.module):
    def __init__(self):
        self.net = models.resnet18(pretrained=True)
        self.remove_handle = self.net._modules['relu'].register_forward_hook(self.forward_hook)
        self.forward_output = None
    def forward_hook(self, module, input, output):
        # save any modules output in an attribute and later use it in backprop.
        self.forward_output = outp…