Search code examples
tensorflowtensorflow2.0tensorflow-datasets

How to print a FlatMapDataset?


For debugging, I would like to print a tensorflow FlatMapDataset.

When trying to use the print method of a tf.data.Dataset, I get the error: AttributeError: 'FlatMapDataset' object has no attribute 'print'.

What I expected was some kind of print-out to assess whether the content of the dataset is what I expected.

Apparently a FlatMapDataset does not have the method:

['_GeneratorState', '__abstractmethods__', '__bool__', '__class__', '__class_getitem__', '__debug_string__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__tf_tracing_type__', '__weakref__', '_abc_impl', '_add_trackable_child', '_add_variable_with_custom_getter', '_apply_debug_options', '_as_serialized_graph', '_checkpoint_dependencies', '_common_args', '_consumers', '_convert_variables_to_tensors', '_deferred_dependencies', '_deserialization_dependencies', '_deserialize_from_proto', '_export_to_saved_model_graph', '_flat_shapes', '_flat_structure', '_flat_types', '_functions', '_gather_saveables_for_checkpoint', '_graph', '_graph_attr', '_handle_deferred_dependencies', '_input_dataset', '_inputs', '_lookup_dependency', '_map_func', '_map_resources', '_maybe_initialize_trackable', '_maybe_track_assets', '_metadata', '_name', '_name_based_attribute_restore', '_name_based_restores', '_no_dependency', '_object_identifier', '_options', '_options_attr', '_options_tensor_to_options', '_preload_simple_restoration', '_restore_from_tensors', '_serialize_to_proto', '_serialize_to_tensors', '_setattr_tracking', '_shape_invariant_to_type_spec', '_structure', '_tf_api_names', '_tf_api_names_v1', '_trace_variant_creation', '_track_trackable', '_trackable_children', '_transformation_name', '_type_spec', '_unconditional_checkpoint_dependencies', '_unconditional_dependency_names', '_update_uid', '_variant_tensor', '_variant_tensor_attr', 'apply', 'as_numpy_iterator', 'batch', 'bucket_by_sequence_length', 'cache', 'cardinality', 'choose_from_datasets', 'concatenate', 'element_spec', 'enumerate', 'filter', 'flat_map', 'from_generator', 'from_tensor_slices', 'from_tensors', 'get_single_element', 'group_by_window', 'interleave', 'list_files', 'load', 'map', 'options', 'padded_batch', 'prefetch', 'random', 'range', 'reduce', 'rejection_resample', 'repeat', 'sample_from_datasets', 'save', 'scan', 'shard', 'shuffle', 'skip', 'snapshot', 'take', 'take_while', 'unbatch', 'unique', 'window', 'with_options', 'zip']

How can I print a FlatMapDataSet in some convenient way to review it's content?


Solution

  • You can print the flatmap dataset by iterating over the datset.
    For example

    import tensorflow as tf
    dataset = tf.data.Dataset.from_tensor_slices([[[1,2, 3], [3,4,5]]])
    dataset = dataset.flat_map(lambda x : tf.data.Dataset.from_tensor_slices(x**2))
    for i in dataset:
      print(i)
    

    output of above code:

    tf.Tensor([1 4 9], shape=(3,), dtype=int32)
    tf.Tensor([ 9 16 25], shape=(3,), dtype=int32)
    

    Thank You.