def parse_instances(self, instances, prediction=False):
"""Parses input instances according to the associated schema.
Arguments:
instances: The tensor containing input strings.
prediction: Whether the instances are being parsed for producing predictions or not.
Returns:
A dictionary of tensors key'ed by field names.
"""
# Convert the schema into an equivalent Example schema (expressed as features in Example
# terminology).
features = {}
for field in self.schema:
if field.type == SchemaFieldType.integer:
dtype = tf.int64
default_value = [0]
elif field.type == SchemaFieldType.real:
dtype = tf.float32
default_value = [0.0]
else:
# discrete
dtype = tf.string
default_value = ['']
if field.length == 0:
feature = tf.VarLenFeature(dtype=dtype)
else:
if field.length != 1:
default_value = default_value * field.length
feature = tf.FixedLenFeature(shape=[field.length], dtype=dtype, default_value=default_value)
features[field.name] = feature
return tf.parse_example(instances, features, name='examples')
评论列表
文章目录