I am using PyTorch 0.4.1
I am trying to create a cost-sensitive Loss by modifying the code source of cross entropy loss.
I wrote new Log_Softmax function as described below
def LogSoftmax(input,labels,ksi):
# input = the output layer of my model (nn.Linear(N,nb_classes)
# labels = target classes
# ksi = cost sensitive matrix
layer = torch.empty(input.shape,requires_grad=True)
for j in range(input.shape[0]):
for i in range(input.shape[1]):
layer[j,i] = log(ksi[labels[i],i]*exp(input[j,i])) - log(sum([ksi[labels[i],k]*exp(input[j,k]) for k in range(len(ksi))]))
return layer
input, labels and ski are tensors and have requires_grad set to True
I got error message for loss.backward() during training phase.
For my function’s return ‘layer’:
If requires_grad = True, I got the following error msg: RuntimeError: leaf variable has been moved into the graph interior.
If requires_grad = False, RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn.
Thank you