Open In App

Tensorflow.js tf.data.zip() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.data.zip() function is used for creating a dataset by zipping together a dict, array, or nested structure of Dataset.



Syntax: 

tf.data.zip(datasets)

Parameters:



Return Value: It returns the tf.data.Dataset.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing Array dataset.
let geek1 = tf.data.array([1, 2, 3, 4]);
let geek2 = tf.data.array([5, 6, 7, 8]);
 
// Zipping dataset of objects.
let geek3 = tf.data.zip([geek1, geek2]);
 
// Printing the returned promise. 
await geek3.forEachAsync(function(geek){
  console.log(JSON.stringify(geek))
});

Output:

[1,5]
[2,6]
[3,7]
[4,8]

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Zipping two array dataset.
let geek = tf.data.zip({
    geek1: tf.data.array([1, 2, 3, 4]),
    geek2: tf.data.array([5, 6, 7, 8])
});
 
// Printing the result.
await geek.forEachAsync(function(e){
  console.log(JSON.stringify(e))
});

Output:

{"geek1":1,"geek2":5}
{"geek1":2,"geek2":6}
{"geek1":3,"geek2":7}
{"geek1":4,"geek2":8}

Reference:   https://js.tensorflow.org/api/3.6.0/#tf.data.zip

Article Tags :