Multiple Output using Pytorch

Hello All, I’m using vgg_16 model and i’m modifying the classifier to have two outputs.

(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)
(7): Linear(in_features=4096, out_features=10, bias=True)
)

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.

Post the error message. Which line is messing it up?

error is in the 6 and 7

(6): Linear(in_features=4096, out_features=10, bias=True)
(7): Linear(in_features=4096, out_features=10, bias=True)

(7) expects the in_features to be 10

change the above code to:

(6): Linear(in_features=4096, out_features=10, bias=True)
(7): Linear(in_features=10, out_features=10, bias=True)

hope that helps!

Hello Sai_tharun,

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 ?

here are the code snipets.

#adding new layer
num_features = model_vowel.classifier[6].in_features
features = list(model_vowel.classifier.children())[:-1]
features.extend([nn.Linear(num_features, 10),nn.Linear(num_features, 10)])
model_vowel.classifier = nn.Sequential(*features)

model_vowel.to(device)
loss_fn = nn.CrossEntropyLoss()
opt = optim.SGD(model.parameters(),lr=0.1,momentum=0.9)

for i, data in enumerate(train_loader):
images, labels = data
images = images.to(device)
vowel_labels = torch.max(labels[:,0],1)[-1]
consonant_labels = torch.max(labels[:,1],1)[-1]
vowel_labels = vowel_labels.to(device)
consonant_labels = consonant_labels.to(device)
opt.zero_grad()
ouput = model_vowel(images)

How do i write a custom function to get two outputs, i tried this code,

class vgg_net(nn.Module):
def init(self,original_model):
super().init()
self.original_model = original_model
self.features = nn.Sequential(*list(self.original_model.children())[:-1])

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

change the code as below:
its better you write an another classifier

(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)
)
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

Hello Sai_tharun,

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.

Here is my code:

class resnet(nn.Module):
def init(self,original_model):
super().init()
self.original_model = original_model
self.features = nn.Sequential(*list(self.original_model.children())[:-2])

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

but i want to save the check point as in when i get the least error while training, how can i implement the same.

I found the solution, thanks for the help.

Hi. What was the solution?

@sai_tharun
Hello Sai_tharun,
how to train the model with two classification head