读取tensorflow中的数据 - TypeError("%s并不是全部匹配."%前缀)

use*_*557 7 python tensorflow

我试图在张量流中加载以下数据文件(225805行).数据文件如下所示:

1,1,0.05,-1.05
1,1,0.1,-1.1
1,1,0.15,-1.15
1,1,0.2,-1.2
1,1,0.25,-1.25
1,1,0.3,-1.3
1,1,0.35,-1.35
Run Code Online (Sandbox Code Playgroud)

读取数据的代码是

import tensorflow as tf

# read in data
filename_queue = tf.train.string_input_producer(["~/input.data"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

record_defaults = [tf.constant([], dtype=tf.int32),    # Column 1
                   tf.constant([], dtype=tf.int32),    # Column 2
                   tf.constant([], dtype=tf.float32),  # Column 3
                   tf.constant([], dtype=tf.float32)]  # Column 4

col1, col2, col3, col4 = tf.decode_csv(value, record_defaults=record_defaults)
features = tf.pack([col1, col2, col3])

with tf.Session() as sess:
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  for i in range(225805):
    example, label = sess.run([features, col4])

  coord.request_stop()
  coord.join(threads)
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误

Traceback (most recent call last):
  File "dummy.py", line 16, in <module>
    features = tf.pack([col1, col2, col3])
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 487, in pack
    return gen_array_ops._pack(values, axis=axis, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1462, in _pack
    result = _op_def_lib.apply_op("Pack", values=values, axis=axis, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 437, 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, int32, float32] that don't all match.
Run Code Online (Sandbox Code Playgroud)

mrr*_*rry 4

tf.pack()运算符要求传递给它的所有张量都具有相同的元素类型。在您的程序中,前两个张量的类型为tf.int32,而第三个张量的类型为tf.float32tf.float32最简单的解决方案是使用运算符将​​前两个张量转换为类型tf.to_float()

features = tf.pack([tf.to_float(col1), tf.to_float(col2), col3])
Run Code Online (Sandbox Code Playgroud)