Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model with softmax output layer not outputing probabilities but just 1 and 0 #8271

Open
croraf opened this issue May 6, 2024 · 4 comments
Open

Comments

@croraf
Copy link

croraf commented May 6, 2024

I'm testing the model provided in node-js example https://github.com/tensorflow/tfjs-examples/blob/master/mnist-node/model.js to classify images of numbers into classes 0 to 9.

It is a tf.sequential() model, and its final layer is model.add(tf.layers.dense({ units: 10, activation: "softmax" }));

Therefore the prediction should output probabilities per class, but this is not happening. Instead only 0s and 1s (in a one-hot fashion) are outputed in the following logging.

const predictions = model.predict(data);
predictions.print();

image

How to get the probabilities of specific image belonging to specific class

@gaikwadrahul8
Copy link
Contributor

gaikwadrahul8 commented May 10, 2024

Hi, @croraf

I apologize for the delayed response and you're absolutely right. The final layer in your model (tf.layers.dense({ units: 10, activation: 'softmax' })) is configured correctly to output probabilities for each class (0-9). However, the way you're interpreting the model's prediction (predictions.print()) might be misleading. that's why maybe you're seeing only 0s and 1s because one-Hot encoded predictions format, the softmax activation ensures the output is a probability distribution between 0 and 1, where each element represents the probability of the input belonging to a specific class.When you print the tensor using predictions.print(), TensorFlow might display the values in a one-hot encoded format. This means the element with the highest probability will be set to 1 and all others will be 0.

Could you please try the below code snippet and see if it is working as expected or not ? If the issue still persists please let us know. Thank you for your cooperation and patience.

const predictions = model.predict(data);
const classProbabilities = predictions.dataSync(); // Use dataSync() for Node.js

// Loop through each element (probability for each class)
for (let i = 0; i < classProbabilities.length; i++) {
  console.log(`Probability of class ${i}: ${classProbabilities[i]}`);
}

@croraf
Copy link
Author

croraf commented May 12, 2024

It outputs the same (except in one value where it is basically 0)
image

@gaikwadrahul8
Copy link
Contributor

gaikwadrahul8 commented May 31, 2024

Hi, @croraf

I apologize for the delayed response, could you please try something like below and see is it working as expected or not? if not please help me with your Github repo to replicate the same behavior from our end to investigate this issue further.

const predictions = model.predict(data);
const classProbabilities = predictions.dataSync(); // Use dataSync() for Node.js

// Loop through each element (probability for each class)
for (let i = 0; i < classProbabilities.length; i++) {
console.log(`Probability of class ${i}: ${classProbabilities[i].toFixed(4)}`);
}

Thank you for your cooperation and patience.

@croraf
Copy link
Author

croraf commented Jun 5, 2024

Hi @gaikwadrahul8 . It is the same. I get:
image

Here is my repo: https://github.com/croraf/x-ray-ai (it has a README)

After training npm run train``, you can use npm run predict` to reproduce the issue.

The repo is basically a reproduction of example in tfjs https://github.com/tensorflow/tfjs-examples/blob/master/mnist-node/model.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants