Open In App

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

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 .evaluateDataset() function is used to evaluate the stated model by means of a stated dataset object.

Note: This method differs from evaluate(), as it is asynchronous.

Syntax:

evaluateDataset(dataset, args?)

 

Parameters:  

  • dataset: The stated dataset object. Whose iterator() method is awaited in order to produce a dataset iterator object, it’s next() method is awaited to generate data batches for evaluation. Moreover, the return value of the next() call must hold a boolean done field as well as a value field. Where the value field is likely to be an array of two tf.Tensors or else an array of two nested tf.Tensor builds. The first case is for models with exactly one input and one output i.e. a sequential model. The latter case is in favor of the models along with several inputs and/or several outputs. Out of two elements in the array, the first one is the input feature(s) and the second one is the output target(s). It is of type tf.data.Dataset.
  • args: The stated configuration object in favor of the dataset based evaluation. It is optional and is of type object. The following arguments come under it:
    1. batches: The stated number of batches which is to be dragged from the given dataset object before terminating the evaluation. It is of type number.
    2. verbose: The stated verbosity mode. It is of type ModelLoggingVerbosity.

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

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining an array x
const Array_x = [
   [1, 1, 1, 1, 1, 1, 1, 1],
   [1, 1, 1, 1, 1, 1, 1, 1],
   [1, 1, 1, 1, 1, 1, 1, 1],
   [1, 1, 1, 1, 1, 1, 1, 1],
];
  
// Defining an array y
const Array_y = [1, 1, 1, 1];
  
// Defining dataset of x
const Dataset_x = tf.data.array(Array_x);
  
// Defining dataset of y
const Dataset_y = tf.data.array(Array_y);
  
// Defining dataset of xy using zip method
const Dataset_xy = tf.data.zip({xs: Dataset_x, ys: Dataset_y})
     .batch(5)
     .shuffle(3);
  
// Defining model
const mymodel = tf.sequential({
   layers: [tf.layers.dense({units: 1, inputShape: [8]})]
});
  
// Compiling model
mymodel.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
  
// Calling evaluateDataset() method
const res = await mymodel.evaluateDataset(Dataset_xy, 
tf.ones([7, 10]), tf.ones([7, 1]), {
   batchSize: 5,
});
  
// Printing output
res.print();


Output:

Tensor
    0.9196577668190002

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining dataset of xy using zip method
const Dataset_xy = tf.data.zip({
    xs: tf.data.array([[1, 0, 1, 2, 1]]), 
    ys: tf.data.array([1, 2, 1, 3])})
    .batch(8);
  
// Defining model
const mymodel = tf.sequential({
   layers: [tf.layers.dense(
      {units: 1, inputShape: [5], activation: 'sigmoid'})
   ]
});
  
// Compiling model
mymodel.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
  
// Calling evaluateDataset() method
const res = await mymodel.evaluateDataset(
    Dataset_xy, tf.truncatedNormal([7, 10]), 
        tf.randomNormal([7, 1]), 
        {batchSize: 2, steps: 1}
    );
  
// Printing output
console.log(JSON.stringify(res));


Output:

{"kept":false,"isDisposedInternal":false,"shape":[],"dtype":"float32",
"size":1,"strides":[],"dataId":{"id":6923},"id":4594,
"rankType":"0","scopeId":4223}

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



Last Updated : 01 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads