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.)

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

错误的原因是因为您正在尝试运行不是张量或操作而是数据集对象的东西。您可以从数据集对象创建张量,以便每次运行时,都可以从数据集中获取下一个示例。

尝试以下内容:

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

最新更新