Python 如何从TensorFlow数据集映射函数输出列表?

Python 如何从TensorFlow数据集映射函数输出列表?,python,tensorflow,tensorflow-datasets,Python,Tensorflow,Tensorflow Datasets,我需要数据集中的示例采用我正在使用的格式([feature_1,feature_2,…],标签)。为此,我将阅读.tfrecords: dataset = tf.data.TFRecordDataset(tf_records) dataset = dataset.map(map_func=parser, num_parallel_calls=num_parallel_calls) ... 但是,我在使用map\u func时遇到了困难: def parser(record, training=

我需要数据集中的示例采用我正在使用的格式
([feature_1,feature_2,…],标签)
。为此,我将阅读
.tfrecord
s:

dataset = tf.data.TFRecordDataset(tf_records)
dataset = dataset.map(map_func=parser, num_parallel_calls=num_parallel_calls)
...
但是,我在使用
map\u func
时遇到了困难:

def parser(record, training=True):
    context_features = {
        "labels": tf.VarLenFeature(tf.int64)
    }

    sequence_features = {
        "feature_1": tf.FixedLenSequenceFeature([], dtype=tf.string),
        "feature_2": tf.FixedLenSequenceFeature([], dtype=tf.string)
    }

    context_parse, sequence_parsed = tf.parse_single_sequence_example(record, context_features, sequence_features)

    X_feature_1 = tf.decode_raw(sequence_parsed["feature_1"], tf.uint8)
    X_feature_2 = tf.decode_raw(sequence_parsed["feature_2"], tf.uint8)
    y = tf.sparse_to_dense(context_parse["labels"].values, [3862], 1)

    return ([X_feature_1, X_feature_2], y)
如果我尝试使用该列表按上述方式保存
[X\u功能\u 1,X\u功能\u 2]
,我最终会得到以下错误:

AttributeError:“list”对象没有属性“get\u shape”

如何传递包含列表的元组

更新(解决方案):

因此,问题中的代码是生成器的一部分。我意识到,由于生成器只是运行一个tensorflow图(希望术语是正确的,我还是新手),因此我可以在最后进行转换:

 feature_1, feature_2, y = sess.run(next_el)
 yield [feature_1, feature_2], y

虽然这样做有效,但我仍然可以在map函数中这样做。

map:函数将一个嵌套的张量结构(具有self.output\u形状和self.output\u类型定义的形状和类型)映射到另一个嵌套的张量结构。问题是你返回的是一个张量列表,而映射的是一个嵌套的张量列表。我认为最好的方法是重新运行它们x_特征,x_特征2,y,并将它们堆叠在feed dict中,但因为我不知道你的模型是如何构造的……我猜不出我考虑过堆叠,但这涉及到匹配的维度。我找到了一个有效的解决办法。我已经在问题的更新中发布了它。在我看来,你应该在课后做。运行(下一个元素),再次运行,因为我看不到代码,我无法猜测如何将输入传递到模型,在tensorflow中,你通常定义占位符,而在keras中,你直接将输入传递到模型,在tensorflow中,您可以定义一个尺寸为x1、x2的占位符,并通过feed dict将其传递为numpy val,因为下一步需要计算numpy objs的张量。您是否尝试将内部列表更改为元组?