tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.37k stars 1.92k forks source link

setClassifierDataset of knn-classifier doesn't fully restore the state #6213

Open ToonTalk opened 2 years ago

ToonTalk commented 2 years ago

System information TFJS from https://cdn.jsdelivr.net/npm/@tensorflow/tfjs

Describe the current behavior The log shows

{classIndex: 0, label: 'a', confidences: {…}}
{classIndex: undefined, label: 'a', confidences: {…}}

Describe the expected behavior The classIndexshould not be undefined. The second call to predictClassshould behave the same as the first one. Standalone code to reproduce the issue

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier"></script>
<script>
const classifier = knnClassifier.create();
classifier.addExample(tf.tensor([1, 2]), 'a');
classifier.addExample(tf.tensor([2, 3]), 'a');
classifier.addExample(tf.tensor([4, 5]), 'b');
classifier.addExample(tf.tensor([6, 7]), 'b');
classifier.predictClass(tf.tensor([2,4]), 3).then(x => console.log(x));
const dataset = classifier.getClassifierDataset();
const classifier2 = knnClassifier.create();
classifier2.setClassifierDataset(dataset);
classifier2.predictClass(tf.tensor([2,4]), 3).then(x => console.log(x)); 
</script>
rthadur commented 2 years ago

Here is the reproduction link https://codepen.io/rthadur/pen/VwyZZKZ

shmishra99 commented 1 year ago

Hi @ToonTalk , Apology for the late response. I was able to replicate the issue you reported using the latest version of @tensorflow/tfjs@4.4.0, and I encountered the same error. Thank You ! CC: @lina128