[Vuejs]-How To fix "must be numeric tensor, but got string tensor" error

0👍

Check this line:

this.predictedValue = this.model.predict(tf.tensor2d([this.valueToPredict], [1, 1])).get(0, 0);

Tensorflow expects the values inside a 2d tensor to be numeric. As you are reading the value from an HTML input field this.valueToPredict is going to be a string hence the error message.

Simply convert your raw value to a number, for example an integer using parseInt(this.valueToPredict) and it should work.

0👍

I was able to resolve this error by simply using parseInt.

   let val = parseInt(document.getElementById('inputValue').value);
   console.log(tf.tensor2d([val],[1,1]));
   document.getElementById('output').innerText = model.predict(tf.tensor2d([val],[1,1]));
});

Output for Console Log:
dataId: {}
dtype: "float32"
id: 35515
isDisposed: (...)
isDisposedInternal: false
kept: false
rank: (...)
rankType: "2"
shape: (2) [1, 1]
size: 1
strides: [1]
__proto__: Object

Leave a comment