Open In App

Tensorflow.js tf.LayersModel 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: 2, inputShape: [30]})]
});
  
// Compiling model
modl.compile({optimizer: 'sgd', loss: 'meanAbsoluteError'});
  
// Calling evaluate() and randomNormal
// method
const output = modl.evaluate(
     tf.randomNormal([8, 30]), 
     tf.randomNormal([8, 2]), 
     {Sizeofbatch: 3}
);
  
// Printing output
output.print();

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

Tensor
    1.1059763431549072

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: 1, inputShape: [20]})]
});
  
// Compiling model
modl.compile({optimizer: 'adam', loss: 'meanSquaredError'}, 
             (metrics = ["accuracy"]));
  
// Calling evaluate() and truncatedNormal
// method
const output = modl.evaluate(
     tf.truncatedNormal([8, 20]), tf.truncatedNormal([8, 1]), 
      {Sizeofbatch: 3}, {steps: 2});
  
// Printing output
output.print();

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

Tensor
    1.2484867572784424

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


Article Tags :