Loss for multiple output in multitask CNN


(Herleeyandi Markoni) #1

Hello I am getting serious problem when doing this in pytorch. Suppose that Ihave CNN to calculate 3 kind of classification, let we said out1, out2, out3. I am using same criterion for classification. Then in training phase I write like this.

loss1 = criterion(outpute, out1)
loss2 = criterion(outputh, out2)
loss3 = criterion(outputm, out3)
loss = loss1 + loss2 + loss3
loss.backward()
optimizer.step()

Is it true to do that or should I do like this?

loss1 = criterion(outpute, out1)
loss2 = criterion(outputh, out2)
loss3 = criterion(outputm, out3)
loss1.backward()
loss2.backward()
loss3.backward()
optimizer.step()

or like this?

loss1 = criterion(outpute, out1)
loss2 = criterion(outputh, out2)
loss3 = criterion(outputm, out3)
loss = loss1 + loss2 + loss3
loss1.backward()
loss2.backward()
loss3.backward()
loss.backward()
optimizer.step()

-Thank you-


(Shi Qiu, Facebook) #2

I think the first two pieces of code should work fine and have the same net effect.
The last one seems incorrect as it would duplicate the backward computation.
You can refer to post below for more explanations