RSVP for your your local TensorFlow Everywhere event today!


Mock tfds to generate random data.


  • Usage (automated):
with tfds.testing.mock_data(num_examples=5):
  ds = tfds.load('some_dataset', split='train')

  for ex in ds:  # ds will yield randomly generated examples.
  • Usage (manual):

For more control over the generated examples, you can manually overwrite the DatasetBuilder._as_dataset method:

def as_dataset(self, *args, **kwargs):
      lambda: ({
          'image': np.ones(shape=(28, 28, 1), dtype=np.uint8),
          'label': i % 10,
      } for i in range(num_examples)),,,

with mock_data(as_dataset_fn=as_dataset):
  ds = tfds.load('some_dataset', split='train')

  for ex in ds:  # ds will yield the fake data example of 'as_dataset'.


For improved results, you can copy the true metadata files (dataset_info.json, label.txt, vocabulary files) in data_dir/dataset_name/version. This will allow the mocked dataset to use the true metadata computed during generation (split names,...).

If metadata files are not found, then info from the original class will be used, but the features computed during generation won't be available (e.g. unknown split names, so any splits are accepted).


  • The examples are deterministically generated. Train and test split will yield the same examples.
  • The actual examples will be randomly generated using
  • Download and prepare step will always be a no-op.
  • Warning: info.split['train'].num_examples won't match len(list(ds_train))

Some of those points could be improved. If you have suggestions, issues with this functions, please open a new issue on our Github.

num_examples number of fake example to generate.
policy Strategy to use to generate the fake examples. See tfds.testing.MockPolicy.
as_dataset_fn if provided, will replace the default random example generator. This function mock the FileAdapterBuilder._as_dataset
data_dir Folder containing the metadata file (searched in data_dir/dataset_name/version). Overwrite data_dir kwargs from tfds.load. Used in MockPolicy.USE_FILES mode.