Tensorflow - 在Graph Build Tim中选择TFRecord功能

时间:2017-02-10 21:54:17

标签: tensorflow

让我说我有TFRecord示例,其中包含以下功能映射:

feature_mapping =  {
"sentence":tf.VarLenFeature(tf.string), 
'caps':tf.VarLenFeature(tf.string),
'tags':tf.VarLenFeature(tf.string),
'labels': tf.VarLenFeature(tf.string)
} 

我总是需要sentencelabels,但有时我想要0,1或更多其余功能。我知道在Graph构建时我想要什么功能。

如何在图形构建时选择多个功能?

例如,句子和标签,没问题:

parsed = tf.parse_example(example, features=feature_mapping)
sentence = parsed['sentence']
labels = parsed['labels']

但我可以提取多项功能吗?即:

FEATURE_NAMES = ['caps', 'tags']
parsed = tf.parse_example(example, features=feature_mapping)
features = tf.multiple_features(parsed, FEATURE_NAMES] # Does something like this exist?  

我也愿意改变我的TFRecord表示。任何帮助,将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

感谢@Allen Lavoie指出它是一个简单的解决方案。我需要将它们放在一个列表中,然后根据这篇文章here,我只需将tf.pack放入列表中。以下是解决方案。

# get features
FEATURES = ['labels', 'caps']
output_list = [] 
for f in range(len(FEATURES)):
    feats = parsed[FEATURES[f]]
    dense_feats = tf.sparse_tensor_to_dense(feats, default_value='<PAD>')
    output_list.append(dense_feats)

features = tf.pack(output_list)