How to Convert Logits to Probability in Binary Classification in Tensorflow?
logits= tf.matmul(inputs, weight) + bias

After matmul operation, the logits are two values derive from the MLP layer. My target is binary classification, how to convert the two values, logits, into probabilities, which include positive prob and negative prob and the sum of them is 1 ?

2 Answers

I am writing this answer for anyone who needs further clarifications:

If it is a binary classification, it should be:

prediction = tf.round(tf.nn.sigmoid(logit))

If it is a multi-class classification:

prediction = tf.nn.softmax(logit)

then using the argmax function you can get the index of the class that has the highest probability score.

np.argmax(prediction, 0)
3
predictions = tf.nn.softmax(logits)
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest