Based on your reply, is the following code right?
optimizer = optim.SGD(params=my_params_list, lr=....)

loss1_func = nn.CrossEntropyLoss()
loss2_func = nn.CrossEntropyLoss()

loss1 = loss1_func(y1, target1)
loss2 = loss2_func(y2. target2)

loss = sum([loss1, loss2])  # or loss = loss1 + loss2
loss.backward()
optimizer.step()


from previous reply, I’ve changed my code into:
optimizer = optim.SGD(params=my_params_list, lr=....)

loss1_func = nn.CrossEntropyLoss()
loss2_func = nn.CrossEntropyLoss()

loss1 = loss1_func(y1, target1)
loss2 = loss2_func(y2. target2)

loss1.backward(retain_gragh=True)
loss2.backward(retain_gragh=True)

optimizer.step()

Thanks for your suggestion.