Fuledbyramen
Fuledbyramen

Reputation: 304

TensorFlow : Loss matrix function

I currently have a RNN in which my labels have two classes. Outputting [1, 0] or [0, 1]. Id like to implement a loss matrix so that incorrectly guessing [1, 0] for [0, 1] is 100 times more costly than guessing [0, 1] for [1, 0]. So I think my loss matrix would be [[0, 1], [100, 0]].

Is this possible with tensorflow? If so what cost function should I use? Thank you

Upvotes: 1

Views: 512

Answers (1)

Allen Lavoie
Allen Lavoie

Reputation: 5808

One option is to use a lookup table for the different combinations. This would let you then weight whichever loss you're using (e.g. cross entropy) based on the discretized prediction (since this lookup operation is not differentiable).

import tensorflow as tf

# This example is using eager execution, but the same code will work with graph
# building if you run it in a Session.
tf.enable_eager_execution()

penalties = tf.constant([
    # Predicted 0
    0.,    # Label 0
    100.,  # Label 1
    # Predicted 1
    1.,    # Label 0
    0.     # Label 1
])

def compute_loss_weight(predicted, label):
  sparse_predicted = tf.argmax(predicted, axis=-1)
  sparse_label = tf.argmax(label, axis=-1)
  offset = sparse_predicted * tf.shape(label, out_type=tf.int64)[-1] + sparse_label
  return tf.gather(params=penalties, indices=offset)

print(compute_loss_weight(predicted=[1, 0], label=[0, 1]))  # 100.
print(compute_loss_weight(predicted=[0, 1], label=[1, 0]))  # 1.
# Also works on batches
print(compute_loss_weight(predicted=[[1, 0], [1, 0], [0, 1], [0, 1]],
                          label=    [[0, 1], [1, 0], [0, 1], [1, 0]]))
# Prints                             [100.   0.       0.      1.]

Upvotes: 3

Related Questions