Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading data in tensorflow - TypeError("%s that don't all match." % prefix)

I am trying to load the following data file (with 225805 rows) in tensor flow. The data file looks like this:

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

the code that reads the data is

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)

and this is the error I am getting

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.
like image 605
user3639557 Avatar asked Sep 13 '16 04:09

user3639557


1 Answers

The tf.pack() operator requires that all of the tensors passed to it have the same element type. In your program, the first two tensors have type tf.int32, while the third tensor has type tf.float32. The simplest solution is to cast the first two tensors to have type tf.float32, using the tf.to_float() operator:

features = tf.pack([tf.to_float(col1), tf.to_float(col2), col3])
like image 146
mrry Avatar answered Sep 30 '22 14:09

mrry