How to replace last layer in Sequential

kkjh0723: <p>I have a pretrained model with layers stacked in nn.Sequential (I’m using ResNext from <a href="https://github.com/clcarwin/convert_torch_to_pytorch" rel="nofollow noopener">link</a>)<br>
And I want to replace only the last Linear layer.</p>
<p>I first load the pretrained model and weights as below,</p>
<pre><code>model = resnext_101_64x4d.resnext_101_64x4d
model.load_state_dict(torch.load('resnext_101_64x4d.pth'))
</code></pre>
<p>Then how can I replace the last layer?<br>
the model looks like below,</p>
<pre><code>Sequential(
  (0): Conv2d (3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
  (2): ReLU()
   ...
  (10): Sequential(
    (0): Lambda(
    )
    (1): Linear(in_features=2048, out_features=1000)
  )
)
</code></pre>
<p>I want to remove the last Linear layer  and then <code>add_module</code> but I don’t know how to remove.<br>
Or is it okay to just replace it by,<br>
<code>model[10].add_module('1', torch.nn.Linear(2048,365))</code>?</p>

Alpha: <p>Hi</p>
<ol>
<li>Remove the last layer</li>
</ol>
<blockquote>
<p>removed = list(model.children())[:-1]<br>
model= torch.nn.Sequential(*self.removed)</p>
</blockquote>
<ol start="2">
<li>add the new layer</li>
</ol>
<blockquote>
<p>model = torch.nn.Sequential(model, torch.nn.Linear(2048,365))</p>
</blockquote>

PabloRR100: <p>I have a problem replicating this behaviour in VGG.</p>
<p>It looks to me as ResNet and VGG are constructed differently. I can’t simply: <code>list(model.children())[:-1]</code> because it retrieves an entire nn.Sequential of 6 modules. And I can’t go deeper in the indexing. I retrieve that same Sequential if I try <code>list(model.children())[-1]</code>, <code>list(model.children())[:][-1]``` or</code>list(model.children())[-1]```</p>
<p>The only way I have found possible to do it is:</p>
<pre><code class="lang-auto">model = models.vgg16(pretrained=True)
model.name = 'VGG_Baseline'            
        
last = nn.Linear(list(model.children())[-1][-1].in_features, out_size)
block = nn.Sequential(*list(model.children())[-1][:-1], last)
        
model = nn.Sequential(*list(model.children())[:-1], block)
</code></pre>
<p>However, this way I can’t use requires_grad_(False) for every layer except this last one.</p>
<p>Any ideas?</p>

Lucy_Gan: <p>it doesn’t work. said ‘Sequential’ object has no attribute ‘removed’</p>

Saurabh_Kataria: <p>remove the word “self”</p>

Vaggelis_Tikas: <p>Since the source code of this model is:<br>
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))<br>
self.fc = nn.Linear(512 * block.expansion, num_classes)<br>
from pytorch sourcecode in torchvision,<br>
you can simply write:<br>
model = resnext_101_64x4d.resnext_101_64x4d<br>
model.fc = torch.nn.Linear(2048, class_index)</p>

Vaggelis_Tikas: <p>Since the source code for VGG is like this:<br>
self.classifier = nn.Sequential(<br>
nn.Linear(512 * 7 * 7, 4096),<br>
nn.ReLU(True),<br>
nn.Dropout(p=dropout),<br>
nn.Linear(4096, 4096),<br>
nn.ReLU(True),<br>
nn.Dropout(p=dropout),<br>
nn.Linear(4096, num_classes),<br>
)<br>
You could simply write:<br>
model.classifier[-1] = nn.Linear(num_of_channels, num_of_classes)</p>