在tensorflow数据集中,稀疏张量

4
我有一个使用TFRecord的tensorflow程序,想要使用tf.contrib.data.TFRecordDataset来读取数据,但是当我尝试解析示例时出现异常:“TypeError:无法将类型为< class 'xxx'>的对象转换为张量”。 仅仅使用下面这段代码时:
代码如下:
def _parse_function(example_proto):
    features = {"var_len_feature": tf.VarLenFeature(tf.float32),
                 "FixedLenFeature": tf.FixedLenFeature([10], tf.int64),
                 "label": tf.FixedLenFeature((), tf.int32default_value=0)}

parsed_features = tf.parse_single_example(example_proto, features)
return parsed_features["image"], parsed_features["label"]

filenames = ["/var/data/file1.tfrecord", "/var/data/file2.tfrecord"]
dataset = tf.contrib.data.TFRecordDataset(filenames)
dataset = dataset.map(_parse_function)

这方面有任何更新吗?知道答案真的会很有用。 - Adam Snaider
@AdamSnaider 是的,它在tf 1.5中得到了支持。 - ofer-a
似乎VarLenFeature尚未得到支持。 - tobe
@tobe 它是完全支持的,我们正在使用tensorflow 1.6并且它运行良好。 - ofer-a
3个回答

2

0
在Tensor Flow编程指南中的教程缩进不同。
# Transforms a scalar string `example_proto` into a pair of a scalar string and
# a scalar integer, representing an image and its label, respectively.
def _parse_function(example_proto):
  features = {"image": tf.FixedLenFeature((), tf.string, default_value=""),
              "label": tf.FixedLenFeature((), tf.int32, default_value=0)}
  parsed_features = tf.parse_single_example(example_proto, features)
  return parsed_features["image"], parsed_features["label"]

# Creates a dataset that reads all of the examples from two files, and extracts
# the image and label features.
filenames = ["/var/data/file1.tfrecord", "/var/data/file2.tfrecord"]
dataset = tf.contrib.data.TFRecordDataset(filenames)
dataset = dataset.map(_parse_function)

错误的缩进可能导致 TypeError,从而由 Python 解释器处理不需要的控制流程。

这与缩进无关。使用FixedLenFeature可以正常工作,但使用VarLenFeature则不行。缩进只是我为了StackOverFlow创建示例时添加的。 - ofer-a

0

tf.VarLenFeature 创建 SparseTensor。大多数情况下,SparseTensors 与 mini batch 相关联。您可以像下面这样尝试吗?

dataset = tf.contrib.data.TFRecordDataset(filenames)

dataset = dataset.batch(batch_size=32)

dataset = dataset.map(_parse_function)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接