Tensorflow:Int、float 类型在 record_defaults 中不起作用

Tensorflow: Int, float types not working in record_defaults

我想导入各种类型的列,如 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

罪魁祸首是这一行:features = tf.stack(cols[:-1])。它试图将所有特征列张量堆叠到一个张量中,只有当它们属于同一类型时才会起作用(并且类型是从记录默认值推导出来的)。

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