You can use hooks:
https://pytorch.org/docs/master/generated/torch.nn.Module.html?highlight=hook#torch.nn.Module.register_forward_hook
(look for a good tutorial by googling)
Similarly, you ahve this new feature
  
      

      pytorch.org
  

  
    

Feature Extraction in TorchVision using Torch FX 1