我正在尝试使用TensorFlow读取以下CSV文件(“test.txt”)
46, 37, 38
27, 23, 17
然后打印出内容,但没有打印任何内容。我正在使用的代码是
import tensorflow as tf
import os
filename_queue = tf.train.string_input_producer(["test.txt"])
reader = tf.TextLineReader()
_, csv_row = reader.read(filename_queue)
record_defaults = [[0], [0], [0]]
val1, val2, val3 = tf.decode_csv(csv_row, record_defaults=record_defaults)
data1 = tf.stack([val1, val2, val3])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
while True:
try:
data2 = sess.run([data1])
print(data2)
except tf.errors.OutOfRangeError:
break
答案 0 :(得分:0)
我认为你的结果不一致,因为局部变量尚未初始化。 tf.string_input_producer
使用了一个纪元计数器局部变量,所以它可能会立即达到纪元限制(当然可以在这里改进错误信息)。
尝试使用明确的纪元限制(否则它应该无限循环)并初始化局部变量:
import tensorflow as tf
filename_queue = tf.train.string_input_producer(["/tmp/test.txt"], num_epochs=1)
reader = tf.TextLineReader()
_, csv_row = reader.read(filename_queue)
record_defaults = [[0], [0], [0]]
val1, val2, val3 = tf.decode_csv(csv_row, record_defaults=record_defaults)
data1 = tf.stack([val1, val2, val3])
init = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
while True:
try:
data2 = sess.run([data1])
print(data2)
except tf.errors.OutOfRangeError:
break