我有以下方法,应该从序列化中解码样本TFRecordDataset
:
def decode_example(self, serialized_example):
"""Return a dict of Tensors from a serialized tensorflow.Example."""
data_fields, data_items_to_decoders = self.example_reading_spec()
# Necessary to rejoin examples in the correct order with the Cloud ML Engine
# batch prediction API.
data_fields['batch_prediction_key'] = tf.io.FixedLenFeature([1], tf.int64, 0)
if data_items_to_decoders is None:
data_items_to_decoders = {
field: tf.contrib.slim.tfexample_decoder.Tensor(field)
for field in data_fields
}
decoder = tf.contrib.slim.tfexample_decoder.TFExampleDecoder(data_fields, data_items_to_decoders)
decode_items = list(sorted(data_items_to_decoders))
decoded = decoder.decode(serialized_example, items=decode_items)
return dict(zip(decode_items, decoded))
但是,这在Tensorflow 2下不起作用。
tf.contrib
不再存在,我找不到可用于解码这些示例的任何内容。
TFExampleDecoder
安装后我什至找不到tensorflow-data-validation
。
知道那里有什么问题和/或如何解码示例吗?
我能够使用来使其工作tf.io.parse_single_example
。
我们必须像往常一样声明数据字段(example_reading_spec
),然后才能对示例进行解码:
def example_reading_spec():
data_fields = {
'inputs': tf.io.VarLenFeature(tf.float32),
'targets': tf.io.VarLenFeature(tf.int64),
}
return data_fields
def decode_example(serialized_example):
"""Return a dict of Tensors from a serialized tensorflow.Example."""
return tf.io.parse_single_example(
serialized_example,
features=example_reading_spec()
)
现在我们可以Dataset.map
像这样加载数据集碎片:
record_dataset = tf.data.TFRecordDataset(filenames, buffer_size=1024)
record_dataset = record_dataset.map(decode_example)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句