Open croraf opened 6 months ago
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]}`);
}
It outputs the same (except in one value where it is basically 0)
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.
Hi @gaikwadrahul8 . It is the same. I get:
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
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 ismodel.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.
How to get the probabilities of specific image belonging to specific class