Search code examples
pythonnumpytensorflowkerasloss-function

How to count the elements belonging to one label class in a custom keras loss function?


I'm looking for a way to count to number of occurrences of each class in the y_true array in a custom loss function and replace each element in the array with its respective number of occurrences.

I've already implemented a numpy solution, but I can't seem to translate it into keras (with tf backend).

Example Input:

y_true = np.array([0, 1, 1, 1, 0, 3])

Imports:

import numpy as np
from keras import backend as k

Numpy implementation:

def custom_loss(y_true, y_pred):
    bins = np.bincount(y_true)
    y_true_counts = bins[y_true]

    >>> y_true_counts: [2 3 3 3 2 1]

Keras implementation:

def custom_loss(y_true, y_pred)
    bins = k.tf.bincount(y_true)
    y_true_counts = bins[y_true]

While the numpy solution works fine, when I want to evaluate the keras implementation I get the following error:

a = custom_loss(y_true, y_pred)
>>> InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice_4' (op: 'StridedSlice') with input shapes: [?], [1,6], [1,6], [1].
[...]
----> 3     y_true_counts = bins[y_true]
[...]

Solution

  • Try tf.bincount and tf.gather.

    import tensorflow as tf
    
    y_true = tf.constant([0, 1, 1, 1, 0, 3],dtype=tf.int32)
    bins = tf.bincount(y_true)
    y_true_counts = tf.gather(bins,y_true)
    
    with tf.Session()as sess:
        print(sess.run(bins))
        print(sess.run(y_true_counts))
    
    [2 3 0 1]
    [2 3 3 3 2 1]