Hi Tùng!



 tungdop2:

This is my model and loss function for 2 labels: style and layout, each have more than 10 classes. How can i improve it?


First comment:  For reasons of numerical stability, you should use
BCEWithLogitsLoss and get rid of the final Sigmoid layers.
Other than that, your approach looks reasonable.
However, you don’t need to split your final layer into separate layout
and style pieces.  The following is (essentially*) equivalent:
        self.layout_and_style_together = nn.Sequential(
            nn.Dropout(0.2),
            nn.Linear(last_channel, n_classes_layout + n_classes_style),
            nn.Sigmoid()
        )

*)  There is a difference in that your version has two separate
Dropouts.  Therefore the choice of which inputs to randomly zero
out is made separately for your layout and style layers, where
the same random choice is used for both the “layout” and “style”
sections of my layout_and_style_together layer.  (I can’t imagine
that this matters at all.)
Also, if you are willing to forgo the Dropout (and do the right thing by
removing the Sigmoid), you can simply replace resnet50's final layer
with a single Linear:
        self.base_model = models.resnet50 (pretrained=True).to (device)
        last_channel = self.base_model.fc.in_features
        self.base_model.fc = nn.Linear (last_channel, n_classes_layout + n_classes_style)

(I don’t have an opinion about whether your final Dropout is helpful
or necessary.  However, the resnet50 architecture doesn’t have one.)
Best.
K. Frank