You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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]}`);
}
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
The text was updated successfully, but these errors were encountered: