我有以下方法,用于解码序列化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。
你知不知道哪里出了什么问题和/或我如何破译我的例子?
发布于 2020-08-10 06:26:55
我能够使用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)https://stackoverflow.com/questions/60400210
复制相似问题