I built a model of multi-labels classification. The model like this:
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
It trains data well.
and calculate the loss with nn.CrossEntropyLoss()
:
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.
I’m not sure it is correct. I think maybe it looks complicated.