Reputation: 5087
When I do a classification job, I need to encode a classid with one_hot method.
But shuold I encode background class with -1 or 0 with tf.one_hot
function?
For example:
// plan a
logits = [0.1, 0.1, 0.2, 0.3, 0.4]
classids = [-1,1,2,3,4] // -1 is background class
class_num = 5
on_hot_class = tf.one_hot(class_ids, depth=class_num)
loss = tf.keras.losses.categorical_crossentropy(one_hot_class,class_logits, from_logits=True)
// plan b
logits = [0.1, 0.1, 0.2, 0.3, 0.4]
classids = [0,1,2,3,4] // 0 is background class
class_num = 5
on_hot_class = tf.one_hot(class_ids, depth=class_num)
loss = tf.keras.losses.categorical_crossentropy(one_hot_class,class_logits, from_logits=True)
Upvotes: 3
Views: 342
Reputation: 9905
Canonically, you would treat your background class just like any other and encode as a one_hot(on_value=1)
. If you want to emphasize this class you can use weighted tf.nn.weighted_cross_entropy_with_logits
and assign a higher weight to that class.
Since you rely on crossentropy with logits, the output of logits function would always be between 0 and 1. It means, that your model would always produce high loss value whenever it sees an input of background class. It is very likely to disrupt your training. It still does not mean that you can not use -1 or that it would have no positive effect in any imaginable situation.
Upvotes: 3