Here is a example to get output of specified layer in vgg16

  
      github.com
  
  
    chenyuntc/pytorch-book/blob/master/chapter8-风格迁移(Neural Style)/PackedVGG.py 1.3k
#coding:utf8
import torch
import torch.nn as nn
from torchvision.models import vgg16
from collections import namedtuple

class Vgg16(torch.nn.Module):
    def __init__(self):
        super(Vgg16, self).__init__()
        features = list(vgg16(pretrained = True).features)[:23]
        # features的第3，8，15，22层分别是: relu1_2,relu2_2,relu3_3,relu4_3
        self.features = nn.ModuleList(features).eval() 
        
    def forward(self, x):
        results = []
        for ii,model in enumerate(self.features):
            x = model(x)
            if ii in {3,8,15,22}:
                results.append(x)
        


  This file has been truncated. show original

  
  
    
    
  
  


the 3rd, 8th, 15th,22nd layer is relu1_2,relu2_2,relu3_3,relu4_3.
BTW, you may use

```Python

your code

```

to format your code
it’ll goes like
your code