May 2020
Intermediate to advanced
404 pages
10h 52m
English
After the training is done, we also need to create an API to invoke the prediction function and return the predicted result. We bind the API to the '/predict' route with a POST method to make a request to this API, as shown:
app.post('/predict', function(req, res) { var test = tf.tensor2d([parseFloat(req.body.sepLen), parseFloat(req.body.sepWid), parseFloat(req.body.petLen), parseFloat(req.body.petWid)], [1,4]); var out = model.predict(test); var maxIndex = 0; for (let i=1;i<out.size; i++){ if (out.buffer().get(0, i) > out.buffer().get(0, maxIndex)){ maxIndex = i; } } ans = "Undetermined"; switch(maxIndex) { case 0: ans = "Setosa"; break; case 1: ans = "Virginica"; break; case 2: ans = "Versicolor"; break; ...Read now
Unlock full access