3

I am trying build predictive system into a MERN app. I am new to Tensorflow and just followed this tutorial which gets relation between Horsepower and Miles per gallon.

https://codelabs.developers.google.com/codelabs/tfjs-training-regression/index.html#6

I've not understood how to utilize this to get a predicted value. How do I enter a horsepower in input field and get predicted MPG.

const xs = tf.linspace(0, 1, 100);      
  const preds = model.predict(xs.reshape([100, 1]));  

The above line is used to plot range of horsepower from low to high I guess. But how do I get a predicted value for entered value?

Please help

Stacy J
  • 2,721
  • 15
  • 58
  • 92

1 Answers1

4

Given the model, the features are a 2d dimensional data.

If you have an input field value, you can find its prediction by using

const tensor = model.predict(tf.tensor([valueOfInput], [1, 1]))
// Get the value
const value = tensor.dataSync()[0]
edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • I added above line and console logged it. following is the response - {"isDisposedInternal":false,"shape":[1,1],"dtype":"float32","size":1,"strides":[1],"dataId":{},"id":10892,"rankType":"2"}. Is this the way it should be. I was expecting a single value for the input – Stacy J Jul 20 '19 at 00:39
  • const price = model.predict(tf.tensor([96087], [1, 1])); – Stacy J Jul 20 '19 at 00:40
  • 1
    If you want to get the value, you can use `datasync()` or `data()`. You can see my edited answer. – edkeveked Jul 20 '19 at 15:17