I'm using Keras (Tensorflow backend) to build a model that given an input predicts a single class (out of 64 classes), a multiclass model. Given the pretty large number of classes, I do not want to use the categorical_crossentropy
or the sparse_categorical_crossentropy
loss functions, since they quite based on a single predicted class.
For example: let y_true=0
(sparse), y_pred1=[0.4, 0.5, 0.01, 0.02, 0.01, ...]
and y_pred2=[0.04, 0.05, 0.5, 0.4, 0.01, ...]
(where y_pred1
and y_pred2
are predicted one-hot vectors). The losses above will get the same loss - since the gold label 0 is not the highest label that was predicted. In my case, I do want to separate the predictions, and get the loss for y_pred1
much lower, since the gold label was at the top 5 predicted classes.
I tried to use a costume loss function, but with no success - unclear exception of ValueError: None values not supported.
occured.
def in_top_k(y_true, y_pred):
return keras.backend.in_top_k(y_pred, math_ops.argmax(y_true, axis=-1), k=5)
model.compile(loss=in_top_k,
optimizer='adam',
metrics=['accuracy'])
where y_pred
and y_true
are [n,64] tensors.
- What could be that error?
- Is my costume loss impl. correct?
- Is my line of thinking correct?
Thanks!