How to simplify the code of multi-label classification?

XiaXuehai: <p>I built a model of multi-labels  classification. The model like this:</p>
<pre><code class="lang-auto">class Net(nn.Module):
	def __init__(self):
		super(Net, self).__init__()
		self.conv1 = nn.Conv2d(1, 32, 3)
		self.conv2 = nn.Conv2d(32, 32, 3)
		self.pool24 = nn.MaxPool2d(2, 2)
		self.conv3 = nn.Conv2d(32, 64, 3)
		self.conv4 = nn.Conv2d(64, 64, 3)
		self.conv5 = nn.Conv2d(64, 128, 3)
		self.conv6 = nn.Conv2d(128, 128, 3)
		self.pool6 = nn.MaxPool2d(3, 2)
		self.fc1 = nn.Linear(128 * 5 * 29, 65)
		self.fc2 = nn.Linear(128 * 5 * 29, 65)
		self.fc3 = nn.Linear(128 * 5 * 29, 65)
		self.fc4 = nn.Linear(128 * 5 * 29, 65)
		self.fc5 = nn.Linear(128 * 5 * 29, 65)
		self.fc6 = nn.Linear(128 * 5 * 29, 65)
		self.fc7 = nn.Linear(128 * 5 * 29, 65)


	def forward(self, x):
		x = self.conv2(F.relu(self.conv1(x)))
		x = self.pool24(F.relu(x))
		x = self.conv4(F.relu(self.conv3(x)))
		x = self.pool24(F.relu(x))
		x = self.conv6(F.relu(self.conv5(x)))
		x = self.pool6(F.relu(x))
		x = x.view(x.shape[0],-1)
		x = nn.Dropout(0.5)(x)
		fc1 = self.fc1(x)
		fc2 = self.fc2(x)
		fc3 = self.fc3(x)
		fc4 = self.fc4(x)
		fc5 = self.fc5(x)
		fc6 = self.fc6(x)
		fc7 = self.fc7(x)
		return fc1,fc2,fc3,fc4,fc5,fc6,fc7
</code></pre>
<p>It trains data well.<br>
and calculate the loss with <code>nn.CrossEntropyLoss()</code>：</p>
<pre><code class="lang-auto">output1,output2,output3,output4,output5,output6,output7 = net(inputs)
		loss1 = criterion(output1, labels[:, 0])
		loss2 = criterion(output2, labels[:, 1])
		loss3 = criterion(output3, labels[:, 2])
		loss4 = criterion(output4, labels[:, 3])
		loss5 = criterion(output5, labels[:, 4])
		loss6 = criterion(output6, labels[:, 5])
		loss7 = criterion(output7, labels[:, 6])
		loss = (loss1 + loss2 + loss3 +loss4 +loss5 +loss6 +loss7)/7.
</code></pre>
<p>I’m not sure it is correct. I think maybe it looks complicated.</p>

ptrblck: <p>You could also use <code>nn.BCELoss</code> for multi-label classification with just one output layer given the probabilities of all class occurrences.</p>
<p>Could you explain your output shapes? Why do you return logits of shape <code>65</code> in each <code>fcX</code> layer?</p>

XiaXuehai: <p>It is the licence plate. There are 7 number in one  licence plate,each number have 65 choices.</p>

ptrblck: <p>Thanks for the clarification.<br>
So we basically have multiple predictions (7 numbers) using a multi-class classification (not multi-label).<br>
In a multi-label classification, each sample might have more than one target class.<br>
Using license plates this approach seems to be invalid, since neither a single figure can be more than one class (e.g. it’s either an A or a 7, but not both), nor does it make sense to use the multi-label classification on the whole license plate, since letters and numbers can be repeated.</p>
<p>Given that information, I think your approach looks good to me.<br>
If you don’t like the code repetition, you could use a <code>nn.ModuleList</code>, creating and applying your linear layers in a loop. But it wouldn’t bother me that much.</p>

XiaXuehai: <p>I see. Thank you. <img src="https://discuss.pytorch.org/images/emoji/apple/grin.png?v=5" title=":grin:" class="emoji" alt=":grin:"></p>

Ajeet_Ojha: <p>what is the suggested way to do the multi label multi class classification - any sample code, pointers will really help.</p>
<p>I have 7 Labels in target and each Labels have different number of distinct values ranging from  10 to 200.</p>
<p>I have implemented it using 7 different  SLMC using Logistic regression, want to convert in a single model using pytorrch.</p>