Open In App

Tensorflow.js tf.Sequential class.predict() Method

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .predict() method is used to produce the output expectations considering the stated input instances. Moreover, the calculation is performed here in groups.



Syntax:

predict(x, args?)

Parameters:  



Return Value: It returns tf.Tensor or tf.Tensor[].

Example 1:  




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining model
const modl = tf.sequential({
   layers: [tf.layers.dense({units: 2, inputShape: [40]})]
});
  
// Calling predict() method and
// Printing output
modl.predict(tf.truncatedNormal([7, 40])).print();

Output:

Tensor
    [[0.1556173 , 1.2365075 ],
     [-1.7945877, 2.3424799 ],
     [0.3632407 , -0.1731701],
     [0.195157  , -0.7823027],
     [0.4565429 , 2.512109  ],
     [-1.2392806, 0.1868197 ],
     [0.6895663 , -0.2006246]]

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling predict() method and
// Printing output
tf.sequential({
   layers: [tf.layers.dense({units: 3, inputShape: [20]})]
}).predict(tf.randomNormal([8, 20])).print();

Output:

Tensor
    [[-1.1149288, 0.8968778 , -0.7492741],
     [1.3654473 , -0.471923 , 1.3632329 ],
     [0.5550661 , 0.6949158 , 1.9761562 ],
     [-0.2109454, -0.3558912, 0.243051  ],
     [-1.2827762, 0.5370077 , 0.1645843 ],
     [0.1542411 , -1.359634 , -0.1656512],
     [-0.4721956, 0.3904444 , 0.7398967 ],
     [-0.2076109, -3.0447464, 1.3338548 ]]

Reference: https://js.tensorflow.org/api/latest/#tf.Sequential.predict

Article Tags :