concept tf.data in category deep learning

appears as: tf.data, tf.data
Deep Learning with JavaScript: Neural networks in TensorFlow.js

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.

We will first introduce the tf.data.Dataset API and show how to configure it and connect it to a model. We will then introduce some theory and tooling to help you review and explore your data and resolve problems you might discover. The chapter wraps up by introducing data augmentation, a method for expanding a dataset to improve model quality by creating synthetic pseudo-examples.

6.1.2. Creating a tf.data.Dataset

As of TensorFlow.js version 1.2.7, there are three ways to connect up tf.data .Dataset to some data provider. We will go through each in some detail, but table 6.1 contains a brief summary.

Table 6.1. Creating a tf.data.Dataset object from a data source (view table figure)

How to get a new tf.data.Dataset

API

How to use it to build a dataset

From a JavaScript array of elements; also works for typed arrays like Float32Array tf.data.array(items) const dataset =
tf.data.array([1,2,3,4,5]); See listing 6.1 for more.
From a (possibly remote) CSV file, where each row is an element tf.data.csv(
source,
csvConfig)
const dataset =
tf.data.csv("https://path/to/my.csv"); See listing 6.2 for more. The only required parameter is the URL from which to read the data. Additionally, csvConfig accepts an object with keys to help guide the parsing of the CSV file. For instance,
  • columnNames—A string[] can be provided to set the names of the columns manually if they don’t exist in a header or need to be overridden.
  • delimiter—A single character string can be used to override the default comma delimiter.
  • columnConfigs—A map of string columnName to columnConfig objects can be provided to guide the parsing and return type of the dataset. The columnConfig will inform the parser of the element’s type (string or int), or if the column is to be considered as the dataset label.
  • configuredColumnsOnly—Whether to return data for each column in the CSV or only those columns included in the columnConfigs object.
More detail is available in the API docs at js.tensorflow.org.
From a generic generator function that yields elements tf.data.generator(
generatorFunction)
function* countDownFrom10() {
for (let i=10; i>0; i--) {
yield(i);
}
}

const dataset =
tf.data.generator(countDownFrom10); See listing 6.3 for more. Note that the argument passed to tf.data.generator() when called with no arguments returns a Generator object.
Listing 6.1. Building a tf.data.Dataset from an array
  const myArray = [{xs: [1, 0, 9], ys: 10},
                   {xs: [5, 1, 3], ys: 11},
                   {xs: [1, 1, 9], ys: 12}];
  const myFirstDataset = tf.data.array(myArray);    #1
  await myFirstDataset.forEachAsync(
       e => console.log(e));                        #2

// Yields output like
// {xs: Array(3), ys: 10}
// {xs: Array(3), ys: 11}
// {xs: Array(3), ys: 12}