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]
]

[1.5, 5, 3.2, 0.3]
]