import numpy as np
def zero_hot(labels_dense, num_classes):
"""Convert class labels from scalars to one-hot vectors."""
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels) * num_classes
labels_one_hot = np.ones((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 0
return labels_one_hot
def Complememnt_EntropyLoss(labels, predictions, num_classes):
#get the length of labels
# get predcitions using softmax function
num_labels = labels.shape[0]
#temp
temp = []
for i in range(num_labels):
x=np.take(predictions[i], labells[i])
temp.append(x)
temp = (1- temp) + le-7
temp2 = np.reshape(temp,(-1, 1))
Px = predictions / temp2
Px_log = np.log(Px + le-10)
output = Px*Px_log
y_zerohot = zero_hot(labels, num_classes)
output = output*y_zerohot
loss = np.sum(output)
loss /= num_labels.as_type(np.float32)
loss /= num_classes.as_type(np.float32)
return loss
本文参考 ICLR 2019论文 COMPLEMENT OBJECTIVE TRAINING, 论文链接:https://arxiv.org/abs/1903.01182,Torch实现:https://github.com/henry8527/COT。