Open In App

Tensorflow.js tf.LayersModel class .evaluateDataset() 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 .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:  

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

Example 1:




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




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


Article Tags :