How to get multi-label output after using tf.nn.sigmoid()

1.3k Views Asked by At

Purpose:

I want to get label name directly in tensorflow-serving when prediction, my question is how to transfer pred = tf.nn.sigmoid(last_layer_output) to real label name?

Question Description:

I know how to do it with multi-classes issue:

CLASSES = tf.constant(['a', 'b', 'c', 'd', 'e'])

pred = tf.nn.softmax(last_layer_output)

# pred pretty similar to:
pred = [[0, 1, 0, 0, 0],
        [0, 0, 0, 1, 0],
        [0, 0, 0, 0, 1]]

classes_value = tf.argmax(pred, 1)
classes_name = tf.gather(CLASSES, classes_value)
# classes_name: [b'b' b'd' b'e']
# batch_size = 3

So classes_name is what I want, I could use it design signature in tensorflow-serving, when I prediction, I could get final labels.

But how to do like this in multi-label issue?

e.g.

CLASSES = tf.constant(['a', 'b', 'c', 'd', 'e'])

pred = tf.nn.sigmoid(last_layer_output)
pred = tf.round(pred)

# pred pretty similar to:
pred = [[0, 1, 1, 0, 1],     # 'b','c','e'
        [1, 0, 0, 1, 0],     # 'a','d'
        [1, 1, 1, 0, 1]]     # 'a','b','c','e'

How could I transfer pred to labels name? I can't do it after sees.run() or using other api like numpy because this is for tensorflow-serving, I think I need to do it using tf method.

Any suggestions are appreciated !

1

There are 1 best solutions below

1
On BEST ANSWER

You should first define given probabilities of many classes, which classes you want to return. For example if the probability of this class is above 0.5.

Then you can use tf.where with proper condition to get indices and then same tf.gather to get classes.

Like this:

indicies = tf.where(tf.greater(pred, 0.5))
classes = tf.gather(CLASSES, indicies[:, 1])

Then you need to re-organize it using indicies[:, 0] that tells you which example the class from.

Also, you must understand that correct form of the answer is SparseTensor, which are not very supported by serving and etc. So you may want to return two tensors [strings and indicators which strings are for which example] and deal with on your client side.