def encode(self, instance):
"""Encode a tf.transform encoded dict as serialized tf.Example."""
if self._encode_example_cache is None:
# Initialize the encode Example cache (used by this and all subsequent
# calls to encode).
example = tf.train.Example()
for feature_handler in self._feature_handlers:
feature_handler.initialize_encode_cache(example)
self._encode_example_cache = example
# Encode and serialize using the Example cache.
for feature_handler in self._feature_handlers:
value = instance[feature_handler.name]
try:
feature_handler.encode_value(value)
except TypeError as e:
raise TypeError('%s while encoding feature "%s"' %
(e, feature_handler.name))
return self._encode_example_cache.SerializeToString()
评论列表
文章目录