Open In App

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

Last Updated : 04 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:  

  • x: It is the stated input data, like a tensor, or else an array of tf.Tensors in case the model has numerous inputs. It can be type tf.Tensor, or tf.Tensor[].
  • args: It is an optional parameter that is of type object.
    1. batchSize: It is the stated batch size in integer form and is optional parameter. Moreover, in case its not specified, then the default value is 32.
    2. verbose: It is the stated verbosity mode which is by default false. It is of type Boolean and is optional.

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

Example 1:  

Javascript




// 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:

Javascript




// 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads