def tf_example_to_structure(serialized_example,
                            structure):
  """Convert a serialized tf.Example into a nested structure of Tensors.

  Args:
    serialized_example: String tensor containing a serialized example.
    structure: A nested structure of placeholders that have specified shapes.

  Returns:
    features: A nested structure of Tensors consistent with `structure`.
  """
  flat_structure = nest_to_flat_dict(structure)
  dtype_map = {
      tf.string: tf.string,
      tf.int32: tf.int64,
      tf.int64: tf.int64,
      tf.float32: tf.float32,
      tf.float64: tf.float32
  }

  def _placeholder_to_feature(placeholder):
    return tf.FixedLenFeature(
        shape=np.prod(placeholder.shape, dtype=np.int32),
        dtype=dtype_map[placeholder.dtype])

  flat_feature_spec = {
      k: _placeholder_to_feature(v) for k, v in flat_structure.items()
  }
  flat_features = tf.parse_single_example(serialized_example, flat_feature_spec)
  flat_features = {
      k: tf.reshape(tf.cast(flat_features[k], v.dtype), v.shape)
      for k, v in flat_structure.items()
  }
  features = flat_dict_to_nest(flat_features, structure)
  return features
Convert a serialized tf.Example into a nested structure of Tensors.

  Args:
    serialized_example: String tensor containing a serialized example.
    structure: A nested structure of placeholders that have specified shapes.

  Returns:
    features: A nested structure of Tensors consistent with `structure`.
  
language/common/utils/nest_utils.py