Open In App

Tensorflow.js tf.Sequential 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 sequential 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);
  
// Training Model 
const gfg = tf.sequential();
    
// Adding layer to model  
const layer = tf.layers.dense({units:1, 
               inputShape : [8]});
   gfg.add(layer);
      
// Compiling our model 
const config = {optimizer:'sgd'
              loss:'meanSquaredError'};
  gfg.compile(config);
  
// Calling evaluateDataset() method
const res = await gfg.evaluateDataset(Dataset_xy, 
tf.ones([7, 10]), tf.ones([7, 1]), {
   batchSize: 5,
});
  
// Printing output
res.print();

Output:

Tensor
    2.9478049278259277

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);
  
// Training Model 
const gfg = tf.sequential();
    
// Adding layer to model  
const layer = tf.layers.dense({units:1, 
    inputShape : [5], activation: 'sigmoid'});
  
gfg.add(layer);
      
// Compiling our model 
const config = {optimizer:'sgd'
              loss:'meanSquaredError'};
  
gfg.compile(config);
  
// Calling evaluateDataset() method
const res = await gfg.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":7097},
"id":4731,"rankType":"0","scopeId":4352}

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


Article Tags :