Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 21 Jun, 2021

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() function is used to produce the output estimates for the given input instances. Moreover, the calculations here are made in sets. Where, the step operation is not supportive at present as core backend of tensorflow.js is only required.


predict(x, args?)


  • x: It is the stated input data, like a Tensor, otherwise an array of tf.Tensors in case the model has various inputs. It can be of type tf.Tensor, or tf.Tensor[].
  • args: It is the stated ModelPredictArgs object that holds elective fields.
    1. batchSize: It is the stated batch dimension which is of type integer. In case its undefined, the by default value will be 32.
    2. verbose: It is the stated verbosity mode whose by default value is false.

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

Example 1:  


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


    [[-0.7650393, -0.8317917],
     [-0.7274997, 1.827635  ],
     [-0.9398478, -0.2998275],
     [-1.0945926, -1.9154934],
     [0.0067322 , -1.9220339],
     [0.2052939 , 0.6488774 ]]

Example 2:


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


    [[0.2670097, -1.2741219, -0.3159108],
     [0.9108799, -0.1305539, -0.1370454]]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!