def __init__(self, schema):
"""Build an ExampleProtoCoder.
Args:
schema: A `Schema` object.
Raises:
ValueError: If `schema` is invalid.
"""
self._schema = schema
# Using pre-allocated tf.train.Example objects for performance reasons.
#
# The _encode_example_cache is used solely by "encode" paths while the
# the _decode_example_cache is used solely be "decode" paths, since the
# caching strategies are incompatible with each other (due to proto
# parsing/merging implementation).
#
# Since the output of both "encode" and "decode" are deep as opposed to
# shallow transformations, and since the schema always fully defines the
# Example's FeatureMap (ie all fields are always cleared/assigned or
# copied), the optimizations and implementation are correct and
# thread-compatible.
#
# Due to pickling issues actual initialization of this will happen lazily
# in encode or decode respectively.
self._encode_example_cache = None
self._decode_example_cache = None
self._feature_handlers = []
for name, feature_spec in six.iteritems(schema.as_feature_spec()):
if isinstance(feature_spec, tf.FixedLenFeature):
self._feature_handlers.append(
_FixedLenFeatureHandler(name, feature_spec))
elif isinstance(feature_spec, tf.VarLenFeature):
self._feature_handlers.append(
_VarLenFeatureHandler(name, feature_spec))
elif isinstance(feature_spec, tf.SparseFeature):
self._feature_handlers.append(
_SparseFeatureHandler(name, feature_spec))
else:
raise ValueError('feature_spec should be one of tf.FixedLenFeature, '
'tf.VarLenFeature or tf.SparseFeature: %s was %s' %
(name, type(feature_spec)))
评论列表
文章目录