soft cross entropy in pytorch
I have a bit of a problem implementing a soft cross entropy loss in pytorch.
I need to implement a weighted soft cross entropy loss for my model, meaning the target value is a vector of probabilities as well, not hot one vector.
I tried using the kldivloss as suggested in a few forums, but it does not expect a weight vector so I can not use it.
In general I'm a bit confused about how to create a custom loss function with pytorch and how auto grad follows a custom loss function, especially if after the model we apply some function which is not a mathematical, like mapping the output of the model to some vector and calculating the loss on the mapped vector and etc.

According to your comment, you are looking to implement a weighted cross-entropy loss with soft labels. Indeed nn.CrossEntropyLoss only works with hard labels (one-hot encodings) since the target is provided as a dense representation (with a single class label per instance).
You can implement the function yourself though. I originally implemented this kind of function in this answer but there wasn't any weighting on the classes. Here instead we take the following three arguments:

logits: your unscaled predictions,
weights: the weights per-logit, and
labels your target tensor.

We have the following loss term:
&gt;&gt;&gt; p = F.log_softmax(pred, 1)
&gt;&gt;&gt; w_labels = weights*labels
&gt;&gt;&gt; loss = -(w_labels*p).sum() / (w_labels).sum()

As long as you operate with differentiable PyTorch builtins, you should be able to backward pass from your custom loss' output. In any case, you can always verify if a backward pass can be called from a given tensor by checking if it has a grad_fn attribute.

You can wrap the logic inside a nn.Module
class SoftCrossEntropyLoss():
   def __init__(self, weights):
      super().__init__()
      self.weights = weights

   def forward(self, y_hat, y):
      p = F.log_softmax(y_hat, 1)
      w_labels = self.weights*y
      loss = -(w_labels*p).sum() / (w_labels).sum()
      return loss

