首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解码Tensorflow 2中的示例(从1.12移植)

如何解码Tensorflow 2中的示例(从1.12移植)
EN

Stack Overflow用户
提问于 2020-02-25 17:28:55
回答 1查看 226关注 0票数 2

我有以下方法,用于解码序列化TFRecordDataset中的示例

代码语言:javascript
复制
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

你知不知道哪里出了什么问题和/或我如何破译我的例子?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-10 06:26:55

我能够使用tf.io.parse_single_example使其工作。

我们必须像往常一样声明我们的数据字段(example_reading_spec),然后我们可以用它来解码一个示例:

代码语言:javascript
复制
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加载数据集碎片,如下所示:

代码语言:javascript
复制
record_dataset = tf.data.TFRecordDataset(filenames, buffer_size=1024)
record_dataset = record_dataset.map(decode_example)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60400210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档