Hi phdproblems!



 phdproblems:

But from what i have read, pytorch does not support this, it only supports the same weight for all instances in a batch which has to be provided when the loss is declared/initialized.
Is this the case or can I provide a different weight for each instance?


Well, the documentation for the weight argument in BCELoss 62
and binary_cross_entropy 79 is, to put it nicely, somewhat abbreviated.
I purposely used binary_cross_entropy in my example,
because you can pass in a batch of weights (together with
your predict and target) every time the loss is called.
(As you note, with BCELoss you pass in the weight only at
the beginning when you instantiate the BCELoss class, so
you can’t give it different weights every time you call it with a
new predict and target.)
Also in my example I showed that passing in per-voxel weights
to binary_cross_entropy does indeed work, even if the
documentation doesn’t make this clear, and showed that this
gives the same result as the “flattened” version (predict_f,
target_f, weight_f) where a batch of weights – one weight
for each sample in the batch – is passed in, consistent with
the simplest interpretation of the documentation.
Good luck.
K. Frank