While implementing a model from scratch I had no trouble with setting multiple outputs(2 in my case and backpropagating the combined loss) . The last fully connected layer has 10 neurons and I need to have two multiple outputs of 10 neurons. This is my code.
class LeNet(nn.Module):
def __init__(self):
super(LeNet,self).__init__()
self.cnn_model=nn.Sequential(nn.Conv2d(1,6,5),
nn.ReLU(),
nn.AvgPool2d(2,stride=2),
nn.Conv2d(6,16,5),
nn.ReLU(),
nn.AvgPool2d(2,stride=2))
self.fc_model=nn.Sequential(
nn.Linear(400,120),
nn.LeakyReLU(),
nn.Linear(120,84),
nn.LeakyReLU(),
nn.Linear(84,10))
def forward(self,x):
x1=self.cnn_model(x)
x1=x1.view(x1.size(0),-1)
x1=self.fc_model(x1)
x2=self.cnn_model(x)
x2=x2.view(x2.size(0),-1)
x2=self.fc_model(x2)
return x1,x2
My question is how should I change any existing architecture like resnet18,vgg16 to have multiple outputs.(Note that the number of classes are 10).Kindly provide code snippets.