Multiple Output using Pytorch

nachappa_P_R: <p>Hello All, I’m using vgg_16 model and i’m modifying the classifier to have two outputs.</p>
<p>(classifier): Sequential(<br>
(0): Linear(in_features=25088, out_features=4096, bias=True)<br>
(1): ReLU(inplace)<br>
(2): Dropout(p=0.5)<br>
(3): Linear(in_features=4096, out_features=4096, bias=True)<br>
(4): ReLU(inplace)<br>
(5): Dropout(p=0.5)<br>
(6): Linear(in_features=4096, out_features=10, bias=True)<br>
(7): Linear(in_features=4096, out_features=10, bias=True)<br>
)</p>
<p>when i directly pass the input i get an of error size mismatch, can anyone help me how should i pass the input in order to get two outputs for a same images.</p>

charan_Vjy: <p>Post the error message. Which line is messing it up?</p>

sai_tharun: <p>error is in the 6 and 7</p>
<pre><code class="lang-auto">(6): Linear(in_features=4096, out_features=10, bias=True)
(7): Linear(in_features=4096, out_features=10, bias=True)
</code></pre>
<p>(7) expects the in_features to be 10</p>
<p>change the above code to:</p>
<pre><code class="lang-auto">(6): Linear(in_features=4096, out_features=10, bias=True)
(7): Linear(in_features=10, out_features=10, bias=True)
</code></pre>
<p>hope that helps!</p>

nachappa_P_R: <p>Hello Sai_tharun,</p>
<p>I need to have two outputs from a single input, In the above case classifier[6] output is fed as input to classifier[7], i don’t want that to happen , i want classifier[6] to give 10 outputs and classifier[7] to give 10 outputs, how can i write a custom function to achieve that ?</p>
<p>here are the code snipets.</p>
<p><span class="hashtag">#adding</span> new layer<br>
num_features = model_vowel.classifier[6].in_features<br>
features = list(model_vowel.classifier.children())[:-1]<br>
features.extend([nn.Linear(num_features, 10),nn.Linear(num_features, 10)])<br>
model_vowel.classifier = nn.Sequential(*features)</p>
<p>model_vowel.to(device)<br>
loss_fn = nn.CrossEntropyLoss()<br>
opt = optim.SGD(model.parameters(),lr=0.1,momentum=0.9)</p>
<p>for i, data in enumerate(train_loader):<br>
images, labels = data<br>
images = images.to(device)<br>
vowel_labels = torch.max(labels[:,0],1)[-1]<br>
consonant_labels = torch.max(labels[:,1],1)[-1]<br>
vowel_labels = vowel_labels.to(device)<br>
consonant_labels = consonant_labels.to(device)<br>
opt.zero_grad()<br>
ouput = model_vowel(images)</p>
<p>How do i write a custom function to get two outputs, i tried this code,</p>
<p>class vgg_net(nn.Module):<br>
def <strong>init</strong>(self,original_model):<br>
super().<strong>init</strong>()<br>
self.original_model = original_model<br>
self.features = nn.Sequential(*list(self.original_model.children())[:-1])</p>
<pre><code>def forward(self,x):
    x = self.features(x)
    x = x.view(x.size(0),-1)
    vowel = what should i write here?
    consonant = what should i write here?
    
    return vowel,consonant</code></pre>

sai_tharun: <p>change the code as below:<br>
its better you write an another classifier</p>
<pre><code class="lang-auto">(classifier): Sequential(
(0): Linear(in_features=25088, out_features=4096, bias=True)
(1): ReLU(inplace)
(2): Dropout(p=0.5)
(3): Linear(in_features=4096, out_features=4096, bias=True)
(4): ReLU(inplace)
(5): Dropout(p=0.5)
(6): Linear(in_features=4096, out_features=10, bias=True)
)

(classifier1): Sequential(
(0): Linear(in_features=25088, out_features=4096, bias=True)
(1): ReLU(inplace)
(2): Dropout(p=0.5)
(3): Linear(in_features=4096, out_features=4096, bias=True)
(4): ReLU(inplace)
(5): Dropout(p=0.5)
(6): Linear(in_features=4096, out_features=10, bias=True)
)</code></pre>

sai_tharun: <pre><code class="lang-auto">model_vowel.classifier1 = model_vowel.classifier

def forward(self,x):
    x = self.features(x)
    x = x.view(x.size(0),-1)
    o1 = self.classifier(x)
    o2 = self.classifier1(x)
   
    return o1,o2
</code></pre>

nachappa_P_R: <p>Hello Sai_tharun,</p>
<p>I have implemented the above logic to return two output but i want i check point my model , in the current setup my model is a class and it doesn’t have the property of model.state_dict(), how can i implement the same.</p>
<p>Here is my code:</p>
<p>class resnet(nn.Module):<br>
def <strong>init</strong>(self,original_model):<br>
super().<strong>init</strong>()<br>
self.original_model = original_model<br>
self.features = nn.Sequential(*list(self.original_model.children())[:-2])</p>
<pre><code>def forward(self,x):
    x = self.features(x)
    x = x.view(x.size(0),-1)
    vowel = self.original_model.fc(x)
    consonant = self.original_model.fc1(x)
    return vowel,consonant
</code></pre>
<p>but i want to save the check point as in when i get the least error while training, how can i implement the same.</p>

nachappa_P_R: <p>I found the solution, thanks for the help.</p>

Andre_Almeida: <p>Hi. What was the solution?</p>

sripathisony_sony: <p><a class="mention" href="/u/sai_tharun">@sai_tharun</a><br>
Hello Sai_tharun,<br>
how to train the model with two classification head</p>