I try to define a information entropy loss. The input is a tensor(1*n), whose elements are all between [0, 4]. The EntroyLoss will calculate its information entropy loss.
For exampe, if the input is

class EntroyLoss(nn.Module):
def __init__(self):
super(EntroyLoss, self).__init__()
def forward(self, x):
y = x.view(-1)
p = torch.zeros([5])
for i in range(y.shape[0]):
p[y[i].int()] = p[y[i].int()] + 1
p = p.float() / y.shape[0]
entropy = -p.mul(p.log2()).sum()
return entropy

But pytorch can not calcualate grad:

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

Math Problem
Given a vector X=(…, x_i ,…), H(X) denotes a histogram of X. Define a derivative of H(X) with respect to the i-th value x_i.

A Solution
X_i(+) and X_i(-) denote two vector made by replacing the i-th value of X with x_i + 1 and x_i - 1, respectively. We can define an interpolated histogram