Open In App

Tensorflow.js tf.Sequential class .evaluate() 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 .evaluate() function is used to find the measure of loss and the values of metrics in favor of the prototype in test method.



Note:

Syntax:  



evaluate(x, y, args?)

 

Parameters:  

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

Example 1: Using optimizer as “sgd” and loss as “meanAbsoluteError”.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining model
const modl = tf.sequential({
   layers: [tf.layers.dense({units: 3, inputShape: [40]})]
});
  
// Compiling model
modl.compile({optimizer: 'sgd', loss: 'meanAbsoluteError'});
  
// Calling evaluate() and randomNormal
// method
const output = modl.evaluate(
    tf.randomNormal([5, 40]), 
    tf.randomNormal([5, 3]), 
    {Sizeofbatch: 3}
);
  
// Printing output
output.print();

Output:

Tensor
    1.554270625114441

Here, randomNormal() method is used as tensor input.

Example 2: Using optimizer as “adam”, loss as “meanSquaredError” and “accuracy” as metrics.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining model
const modl = tf.sequential({
   layers: [tf.layers.dense({units: 2, inputShape: [30]})]
});
  
// Compiling model
modl.compile({optimizer: 'adam', loss: 'meanSquaredError'}, 
             (metrics = ["accuracy"]));
  
// Calling evaluate() and truncatedNormal
// method
const output = modl.evaluate(
     tf.truncatedNormal([6, 30]), tf.truncatedNormal([6, 2]), 
      {Sizeofbatch: 2}, {steps: 2});
  
// Printing output
output.print();

Output:

Tensor
    2.7340292930603027

Here, truncatedNormal() method is used as tensor input and step parameter is also included.

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


Article Tags :