Pixelwise weights for MSELoss
I would like to use a weighted MSELoss function for image-to-image training.  I want to specify a weight for each pixel in the target.  Is there a quick/hacky way to do this, or do I need to write my own MSE loss function from scratch?
abweiss
you can do this: <SCODE>def weighted_mse_loss(input, target, weights):
    out = input - target
    out = out * weights.expand_as(out)
    # expand_as because weights are prob not defined for mini-batch
   loss = out.sum(0) # or sum over whatever dimensions
   return loss
<ECODE>
smth
Oh, I see.  There’s no magic to the loss functions.  You just calculate whatever loss you want using predefined Torch functions and then call backward on the loss.  That’s super easy.  Thanks!
abweiss
what if I want L2 Loss ? just do it like this? <SCODE>def weighted_mse_loss(input,target,weights):
    out = (input-target)**2
    out = out * weights.expand_as(out)
    loss = out.sum(0) # or sum over whatever dimensions
    return loss

<ECODE> right ?
liygcheng
smth
Jordan_Campbell
delhi_loafer
Same question, what is your thoughts now ?
JindongJiang
delhi_loafer
Thanks. Actually, variablization must be done if we need them operate at gpu(s).
JindongJiang
picolo
ptrblck
I am trying an unsupervised approach to identify “important” areas of an image without explicitly pre-defining the weight-map. For the other question, I am not sure yet. I am thinking of a simple form like this - LW+(1/W) where L W is elementwise multiplication of learned weights and the MSE/MAE loss. and the second term prevents going too close to 0?
picolo