Monday, 9 September 2019

Guide: Convert Labels to Probabilities for Network with Probability Outputs

A simple regression model comes with just 1 neuron in output layer for class index. However, it can be multiple neurons when having probability outputs instead of class index. For example, output to 4 classes will have 3 probability neurons at output layer.

The matter is that training data might be in form [x1...xN] => class. The following code uses onehot to transform classes in training data to probabilities.

Source code:
#libs
import tensorflow as tf;

tf.enable_eager_execution();
tf.executing_eagerly();

#for single neuron class-index output layer
Y_Labels = [0,1,2,3,2,3];

#for multi-neuron probabilities output layer
#depth is number of classes
Y_Probs  = tf.one_hot(Y_Labels, depth=4);
print(Y_Probs);
#eof

Result:
tf.Tensor( [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.] [0. 0. 1. 0.] [0. 0. 0. 1.]], shape=(6, 4), dtype=float32)

Colab link:
https://colab.research.google.com/drive/1jgBlqQLS4j5pMP5W6Ae3uInrxT2DNQUz

No comments:

Post a Comment