2016-09-13 13 views
6

我試圖在張量流中加載以下數據文件(帶有225805行)。數據文件是這樣的:在張量流中讀取數據 - TypeError(「不匹配的%s」。%前綴)

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 

讀取數據的代碼是

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) 

,這是我得到

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. 

回答

2

錯誤的tf.pack()運營商要求所有的傳遞給它的張量具有相同的元素類型。在您的程序中,頭兩張張的類型爲tf.int32,而第三張張的類型爲tf.float32。最簡單的解決方案是使用tf.to_float()算子將前兩張張投上tf.float32,使用tf.to_float()算子:

features = tf.pack([tf.to_float(col1), tf.to_float(col2), col3])