Getting a slice of a container?

Given a container

model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
nn.Conv2d(64,128,5),
nn.ReLU()
nn.Conv2d(128,128,5),
nn.ReLU()
)

I would like to extract some of the features with indexing.

feats = model[:2]

TypeError: ‘<’ not supported between instances of ‘slice’ and ‘int’

The current best solution I’m guessing is to do this:

feats = nn.Sequential( *(model[i] for i in range(2)) )

Will the former solution be possible later or is there an alternative solution?

The way to extract is not really to index in PyTorch as far as I know.

Your model is only a series of operations, not the values stored in it during forward prop. So, you need to delete the last layer -

model = models.resnet152(pretrained=True)
modules = list(model.children())[:-1]      # delete the last fc layer.
model = nn.Sequential(*modules)

Now you have a model with the same features but the last layer removed.

Now, if you do model(x) your forward prop will give you the features. Similarly, if you want to extract output with 2 layers removed, just change that line to model.children())[:-2] and it will remove the last 2 layers.

Hope this helps!

2 Likes