def flat_dict_to_tf_example(inputs,
                            structure):
  """Convert a flat dictionary to a tf.Example.

  Args:
    inputs: A dictionary of flat numpy arrays.
    structure: A nested structure of placeholders that have specified shapes.

  Returns:
    example: An example with the flattened inputs.
  """
  flat_structure = nest_to_flat_dict(structure)
  tf.nest.assert_same_structure(
      nest1=flat_structure, nest2=inputs, check_types=False)
  example = tf.train.Example()
  dtype_map = {
      tf.string: add_string_feature,
      tf.int32: add_int_feature,
      tf.int64: add_int_feature,
      tf.float32: add_float_feature,
      tf.float64: add_float_feature
  }
  for k, v in inputs.items():
    placeholder = flat_structure[k]
    assert placeholder.shape == v.shape
    add_fn = dtype_map[placeholder.dtype]
    add_fn(k, v.flatten(), example)
  return example
Convert a flat dictionary to a tf.Example.

  Args:
    inputs: A dictionary of flat numpy arrays.
    structure: A nested structure of placeholders that have specified shapes.

  Returns:
    example: An example with the flattened inputs.
  
language/common/utils/nest_utils.py