Add some optional layer to the last layers of pretrained model

S_M: <p>Hi, I am trying to add some layer to the resnet18 pre-trained network using this code.<br>
class T(nn.Module):<br>
def <strong>init</strong>(self, class_num):<br>
super(T, self).<strong>init</strong>()<br>
self.base = models.resnet18(pretrained=True)<br>
self.sigmoid = nn.Sigmoid()<br>
self.class_num = class_num<br>
self.num_ftrs = self.base.fc.in_features</p>
<pre><code>    self.linear = nn.Linear(10,1)

def freez(self, net):
    ct = 0
    for child in net.children():
        ct += 1
        if ct &lt; 7:
            for param in child.parameters():
                param.requires_grad = False
def fc(self, net):
    net.fc =  nn.Linear(self.num_ftrs, self.class_num)
    return net               

def one_forward(self, x):
    self.base = self.freez(models.resnet18(pretrained=True))
    print(self.base)
    self.base = self.fc(self.base)
    net = self.sigmoid(self.base)
    out = net(x)
    return out
</code></pre>
<p>I’ve got this error<br>
Exception has occurred: AttributeError</p>
<p>‘NoneType’ object has no attribute ‘fc’</p>
<p>how can I handle it?<br>
any help would be appreciated.</p>

KFrank: <p>Hi S!</p>
<aside class="quote no-group quote-modified" data-username="S_M" data-post="1" data-topic="164406" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/s_m/48/19733_2.png" class="avatar"> S_M:</div>
<blockquote>
<pre><code>def freez(self, net):
    ct = 0
    for child in net.children():
        ct += 1
        if ct &lt; 7:
            for param in child.parameters():
                param.requires_grad = False
</code></pre>
<p>…<br>
def one_forward(self, x):<br>
self.base = self.freez(models.resnet18(pretrained=True))<br>
print(self.base)<br>
self.base = self.fc(self.base)<br>
net = self.sigmoid(self.base)<br>
out = net(x)<br>
return out</p>
<p>‘NoneType’ object has no attribute ‘fc’</p>
</blockquote>
</aside>
<p>As posted, <code>freez()</code> has no <code>return</code> statement, so, in effect, it returns <code>None</code>.<br>
You set <code>self.base</code> to <code>freez()</code>, that is, <code>None</code> and call <code>self.fc(self.base)</code><br>
which tries to evaluate <code>net.fc</code> (i.e., <code>self.base.fc</code>), but <code>net</code> (<code>self.base</code>)<br>
is <code>None</code>, hence the error.</p>
<p>Best.</p>
<p>K. Frank</p>

S_M: <p>Hi <a class="mention" href="/u/kfrank">@KFrank</a>, thank you so much. how about this issue, I want add sigmid function just after the last fully connected layer of resnet18 so the follow code is added:<br>
net = self.simoid(self.base)</p>
<p>but It cause an error<br>
Exception has occurred: TypeError<br>
sigmoid(): argument ‘input’ (position 1) must be Tensor, not ResNet</p>

KFrank: <p>Hi S!</p>
<aside class="quote no-group" data-username="S_M" data-post="3" data-topic="164406" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/s_m/48/19733_2.png" class="avatar"> S_M:</div>
<blockquote>
<p>net = self.simoid(self.base)</p>
<p>but It cause an error<br>
Exception has occurred: TypeError<br>
sigmoid(): argument ‘input’ (position 1) must be Tensor, not ResNet</p>
</blockquote>
</aside>
<p>The error message is self-explanatory.</p>
<p><code>self.base</code> is a <code>ResNet</code>, not a <code>Tensor</code>, so you can’t apply the <code>sigmoid()</code><br>
function to it.</p>
<p>But more to the point, why would you expect that calling<br>
<code>self.sigmoid (self.base)</code> would add a layer to your network?</p>
<p>Try writing your own very simple network from scratch, and then experiment<br>
with ways to add layers to it.  If you still have questions, please post them<br>
with complete sample code for your toy model.</p>
<p>Best.</p>
<p>K. Frank</p>