Weighted Class-Wise Product

Hello everyone.
Let A be a tensor of shape 64*10. Each row corresponds to the output of our model on a single image and we know the ground label of all images. I want to multiply each row by weight according to its label. For example, I want to multiply all rows with labels 1 by 1.5, all rows with labels 2 by 0.25, and so on. Finally, I want a tensor with the same shape 64*10.

What is vectorized efficient way to do this in PyTorch. Thanks a lot.

Generate your weighted vector of shape 1x10, and then multiply it element-wise with your label tensor. Multiply then the result by the output of your model.

wighted_mask = torch.tensor(weights)
weighted_output = output * (lables * weighted_mask).sum(dim= 1)

Just be careful about the dimensionality of the tensors, depending on that you might need to deal with broadcasting or not.

Maybe I didn’t express my question well; I think there is something wrong. When I print “labels * weighted_mask”, the weights are multiplied by the label number. This is not what I wanted. Suppose the first row was [1, 0.5, 2, 1.5] and its label was 2. I want to multiply all rows with labels 2 by 5. So this row should be [5, 2.5, 10, 7.5].

Hi, I was expecting the labels to be one-hot encoded. Let’s say:

labels = [
          [0, 1, 0, 0]
         ]

weighted_mask = [
                 [1.5, 5, 3.2, 0.3]
                ]

# labels * weighted_mask
row_weights = (labels * weighted_mask).sum(dim= 1, keepdim= True) # [[5]]

wighted_output = output * row_weights # [[1, 0.5, 2, 1.5]] * [[5]] = [[5, 2.5, 10, 7.5]]
1 Like

Hi, Thank you very much. It’s great and it works. Just a small typo in the last line it should be row_weights instead of weight.

Edited :wink:

I am happy I could help you

1 Like