Hello.
Three different input: images1, images1_sub(a subset of images1), images2,.
One network: model.
... ...
optimizer.zero_grad()
output1,intermediate_feature_map_1 = model(images1)
# get output and intermediate feature map
output1_sub, intermediate_feature_map_sub1 = model(images1_sub)
# get output and intermediate feature map
output2, intermediate_feature_map2 = model(images2)
# calculate the cross_entropy ce_loss1 for output1
ce_loss1 = cross_entropy(output1, label)
# calculate the cross_entropy ce_loss2 for output2
ce_loss2 = cross_entropy(output2, label) # the same label
# the distance of intermediate feature map for images_sub1 and images2
#(the shape of images_sub1 is the same as the shape of images2 )
loss_d = distance(intermediate_feature_map_sub1, intermediate_feature_map2 )
loss = ce_loss1 + ce_loss2 + loss_d
loss.backward()
optimizer.step()
I’d like to sum the loss and backward. Then update parameter for network.
Is there anything wrong with my code?
Hi, thanks for your reply!
I have not seen anything strange happening so far. But I have a question:
images1_sub in the code above is a subset of images1. If images1_sub = images1[:half]
Compared to the code: output1_sub, intermediate_feature_map_sub1 = model(images1_sub)
will the following code achieve the same goal and save some calculation resouces? output1_sub, intermediate_feature_map_sub1 = output1[:half], intermediate_feature_map1[:half]
Thank you.
Thanks for the information. I though you would like to split the spatial size.
If you split the batch, you would have to take care of e.g. batch norm layers, as the running stats will differ.
Besides that it should yield the same output.