Tensorflow读取制表符分隔文件

时间:2018-03-26 00:35:38

标签: python tensorflow

我正在尝试将制表符分隔文件读入tensorflow

# Metadata describing the text columns
COLUMNS = ['queue_name','block_name', 'car_name',
           'position_id', 'x_ord',
           'y_ord']
FIELD_DEFAULTS = [[''], [''], [''], [0], [0], [0]]
def _parse_line(line):
    # Decode the line into its fields
    fields = tf.decode_csv(line, FIELD_DEFAULTS, field_delim="\t")

    # Pack the result into a dictionary
    features = dict(zip(COLUMNS,fields))

    # Separate the label from the features
    y = features.pop('y_ord')
    x = features.pop('x_ord')

    return features, x, y


ds = tf.data.TextLineDataset(filenames).skip(1)
ds = ds.map(_parse_line)
with tf.Session() as sess:
print(sess.run(ds)) # I am getting an error when running the session

然而,这给了我一个错误 TypeError: Fetch argument <MapDataset shapes: ({period_name: (), block_name: (), trial_name: (), trial_id: ()}, (), ()), types: ({period_name: tf.string, block_name: tf.string, trial_name: tf.string, trial_id: tf.int32}, tf.int32, tf.int32)> has invalid type <class 'tensorflow.python.data.ops.dataset_ops.MapDataset'>, must be a string or Tensor. (Can not convert a MapDataset into a Tensor or Operation.)

这是否意味着我无法在地图数据集中组合字符串和整数,或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

错误的原因是因为您尝试运行的不是Tensor或Operation,而是数据集对象。您可以从数据集对象创建张量,以便每次运行它时,都可以从数据集中获取下一个样本。

尝试以下方法:

value = ds.make_one_shot_iterator().get_next()
print(sess.run(value)) # First value in your dataset
print(sess.run(value)) # Second value in your dataset

从这里建立起来,你可以从这个张量构建你的模型的其余部分。

请参阅https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_generator

上的文档