tensorflow TFRecords无法分析序列化的examp

2024-05-14 17:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试解码TFRecord示例时遇到以下错误:

InvalidArgumentError: Name: , Feature: relevance (data type: float) is required but could not be found. [Op:ParseExample]

为了解码这个例子,我使用tf.io.parse_example,如下所示

example_features = tf.compat.v1.io.parse_example(
     tf.reshape(serialized_list, [-1]), peritem_feature_spec)

其中serialized_list看起来像

^{pr2}$

并且peritem_feature_spec看起来像

peritem_feature_spec = {
    'relevance':tf.FixedLenFeature([], tf.float32),
    'encoded_clust_index':tf.VarLenFeature(tf.float32)
    }

我很困惑为什么找不到“相关性”这个功能。我认为我正确地编码和创建了TFRecord对象。我是否错误地创建了特性规范?我的想法是tf.VarLenFeature是不正确的特性类型,但我无法确定什么是正确的。在

使用tensorflow_ranking.python.data.parse_from_example_in_example可以正确地将TFRecord解码为它的特性,但是我不知道tf.io.parse_example失败的原因


Tags: iodataparseexampletf错误tfrecord特性
2条回答

代替VarLenFeature你能试试tf.FixedLenSequenceFeature([], tf.float32, allow_missing = True ,default_value=0)解释的here,看看这是否有效?在

编辑:我以前的答案在这个答案下面

正确的答案是向特性规范提供default_value

peritem_feature_spec = {
    'relevance':tf.FixedLenFeature([], tf.float32, default_value=0.0),
    'encoded_clust_index':tf.VarLenFeature(tf.float32, default_value=0.0)
    }

我的旧的答案如下

所以问题归结为我的特征是如何被填充在tensorflow_排名库中的。它填充了一个列表功能,比如:

^{pr2}$

此方法将空字节追加到张量的末尾。解析器在空张量中寻找feature_name,并返回找不到它。我的解决方法是附加序列化的TFRecord示例原型,而不是一个空字节字符串。我是这样完成的:

def pad_fn():
    # Create feature spec for tf.train.Example to append
    pad_spec = {}
    # Default values are 0 or an empty byte string depending on 
    # original serialized data type
    dtype_map = {tf.float32:tf.train.Feature(
            float_list=tf.train.FloatList(value=[0.0])), 
                 tf.int32:tf.train.Feature(
                         int64_list=tf.train.Int64List(value=[0])), 
                 tf.string:tf.train.Feature(
                         bytes_list=tf.train.BytesList(
                                 value=[bytes('', encoding='UTF-8')]))}
     # Create the feature spec
    for key, item in peritem_feature_spec.items():
        dtype = item.dtype
        pad_spec[key] = dtype_map[dtype]
    # Make and serialize example to append
    constant_values = tf.train.Example(
            features=tf.train.Features(feature=pad_spec))
    constant_val_str = constant_values.SerializeToString()

    # Add serialized padding to end of list
    return tf.pad(
        tensor=serialized_list,
        paddings=[[0, 0], [0, list_size - cur_list_size]],
        constant_values=constant_val_str)

相关问题 更多 >

    热门问题