1

我想导入各种类型的列,如 int、float、string 作为张量。只有第一个 record_defaults 工作,其中所有设置为字符串。

但我得到下面的错误。有没有办法将 tensorflow 与各种类型的占位符一起使用?

csv 文件来自aws

import tensorflow as tf

# https://s3.amazonaws.com/aml-sample-data/banking.csv
file1="/Users/Q/Downloads/banking.csv"
filename_queue = tf.train.string_input_producer([file1])
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(filename_queue)

# record_defaults = [[""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [0]]
# record_defaults = [[0], [""], [""], [""], [""], [""], [""], [""], [""], [""], [0], [0], [0], [0], [""], [0.0], [0.0], [0.0], [0.0], [0.0], [0]]
record_defaults = [[0], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [""], [0]]

cols = tf.decode_csv(value, record_defaults=record_defaults, field_delim=",")

features = tf.stack(cols[:-1])

with tf.Session() as sess:
  # Start populating the filename queue.
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  for i in range(1000):
    # Retrieve a single instance:
    example, label = sess.run([features, cols[-1]])

  coord.request_stop()
  coord.join(threads)

错误信息

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/Q/Dropbox/code/Analyzing-Tea/tf-test.py
Traceback (most recent call last):
  File "/Users/Q/Dropbox/code/Analyzing-Tea/tf-test.py", line 14, in <module>
    features = tf.stack(cols[:-1])
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/array_ops.py", line 715, in stack
    return gen_array_ops._pack(values, axis=axis, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1975, in _pack
    result = _op_def_lib.apply_op("Pack", values=values, axis=axis, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 463, in apply_op
    raise TypeError("%s that don't all match." % prefix)
TypeError: Tensors in list passed to 'values' of 'Pack' Op have types [int32, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string] that don't all match.

Process finished with exit code 1
4

1 回答 1

0

罪魁祸首是这一行:features = tf.stack(cols[:-1]). 它尝试将所有特征列张量堆叠成一个张量,这仅在它们具有相同类型时才有效(并且类型是从记录默认值推导出来的)。

所以不要把它们都堆起来,你会没事的。

于 2017-04-19T20:41:57.823 回答