I am implementing a custom loss for my multi-class classification application.
For every y_true, there is a corresponding P_true which represents a list of values.
For every y_pred, after some reformat, using it as the key of an external dictionary, can look up another list of values (with equal length of P_true).
I’d like to calculate the MSE loss between these two lists of values (true_samples, pred_samples), and use the result as the output of this custom loss function.
def custom_loss_fn(y_pred, y_true, P_true):
y_pred_softmax = torch.log_softmax(y_pred, dim=1)
_, y_pred_tags = torch.max(y_pred_softmax, dim=1)
true_samples = Variable(P_true, requires_grad=True).to(device)
pred_cluster_names = [idx2class[tag.squeeze().tolist()]
for tag in y_pred_tags]
pred_samples = Variable(
torch.FloatTensor(
[name_samples_mapping[pred_name]
for pred_name in pred_cluster_names]
), requires_grad=True).to(device)
output = mseloss(true_samples, pred_samples)
return output
But I found that the training loss from each epoch never changed, suggesting something wrong with my computation graph connectivity that autograd failed. Could anyone provide help? Thanks!
Edit: true_samples tensors have requires_grad=True
, pred_samples tensors have grad_fn=<CopyBackwards>
, and output for training loss has grad_fn=<MeanBackward0>
.