concept model.fitDataset in category deep learning

appears as: model.fitDataset, model.fitDataset
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.

Once you have your data as a dataset, inevitably you are going to want to access the data in it. Data structures you can create but never read from are not really useful. There are two APIs to access the data from a dataset, but tf.data users should only need to use these infrequently. More typically, higher-level APIs will access the data within a dataset for you. For instance, when training a model, we use the model.fitDataset() API, described in section 6.2, which accesses the data in the dataset for us, and we, the users, never need to access the data directly. Nevertheless, when debugging, testing, and coming to understand how the Dataset object works, it’s important to know how to peek into the contents.

model.fitDataset() accepts a dataset as its first argument, but the dataset must meet a certain pattern to work. Specifically, the dataset must yield objects with two properties. The first property is named xs and has a value of type Tensor, representing the features for a batch of examples; this is similar to the xs argument to model.fit(), but the dataset yields elements one batch at a time rather than the whole array at once. The second required property is named ys and contains the corresponding target tensor.[7] Compared to model.fit(), model.fitDataset() provides a number of advantages. Foremost, we don’t need to write code to manage and orchestrate the downloading of pieces of our dataset—this is handled for us in an efficient, as-needed streaming manner. Caching structures built into the dataset allow for prefetching data that is anticipated to be needed, making efficient use of our computational resources. This API call is also more powerful, allowing us to train on much larger datasets than can fit on our GPU. In fact, the size of the dataset we can train on is now limited only by how much time we have because we can continue to train for as long as we are able to get new training examples. This behavior is illustrated in the data-generator example in the tfjs-examples repository.

  • batchesPerEpoch—As we saw in listing 6.9, the configuration for model.fitDataset() takes an optional field for specifying the number of batches that constitute an epoch. When we handed the entirety of the data to model.fit(), it was easy to calculate how many examples there are in the whole dataset. It’s just data.shape[0]! When using fitDataset(), we can tell TensorFlow.js when an epoch ends in one of two ways. The first way is to use this configuration field, and fitDataset() will execute onEpochEnd and onEpochStart callbacks after that many batches. The second way is to have the dataset itself end as a signal that the dataset is exhausted. In listing 6.7, we could change
    while (true) { ... }
    to
    for (let i = 0; i<ui.getBatchesPerEpoch(); i++) { ... }
    to mimic this behavior.