TensorFlow:读取CSV文件永远挂起
我已经按照教程进行了学习,但在那里显示的内容似乎不起作用。它永远被卡住了 代码如下:TensorFlow:读取CSV文件永远挂起,csv,tensorflow,Csv,Tensorflow,我已经按照教程进行了学习,但在那里显示的内容似乎不起作用。它永远被卡住了 代码如下: 将tensorflow导入为tf filename\u queue=tf.train.string\u input\u producer([“file0.csv”,“file1.csv]”) reader=tf.TextLineReader() key,value=reader.read(文件名\队列) #如果列为空,则为默认值。还指定了 #解码结果。 记录_默认值=[[1]、[1]、[1]、[1]、[1]]
将tensorflow导入为tf
filename\u queue=tf.train.string\u input\u producer([“file0.csv”,“file1.csv]”)
reader=tf.TextLineReader()
key,value=reader.read(文件名\队列)
#如果列为空,则为默认值。还指定了
#解码结果。
记录_默认值=[[1]、[1]、[1]、[1]、[1]]
col1,col2,col3,col4,col5=tf.decode_csv(
值,记录默认值=记录默认值)
features=tf.pack([col1,col2,col3,col4])
使用tf.Session()作为sess:
#开始填充文件名队列。
coord=tf.train.Coordinator()
线程=tf.train.start\u queue\u runner(coord=coord)
对于范围(1200)内的i:
#检索单个实例:
例如,label=sess.run([features,col5])
协调请求停止()
坐标连接(线程)
我正在使用Tensorflow 0.7.1和Python3
我做错了什么
我的文件只有这一行:
5,4,3,2,1
感谢您坚持不懈地尝试调试。原来您遇到了一个在中修复的bug,但该修复尚未发布。有两种可能的修复方法(除此之外):
问题的原因是TensorFlow使用一个有界线程池来调度op,并且(在修复之前)读卡器op可能会阻塞,如果在读卡器完成之前必须运行另一个op(例如,由于生产者-消费者关系),这将导致死锁。该修复程序通过异步运行读卡器来解决此问题。在TensorFlow 0.10中,需要注意的是,Python 3.0中的修改和格式设置都是必需的,即使使用正确的2.x打印语法,在Python 2.x中也无法实现
import tensorflow as tf
filename_queue = tf.train.string_input_producer(["./iris.data"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [tf.constant([], dtype=tf.float32), # Column 0
tf.constant([], dtype=tf.float32), # Column 1
tf.constant([], dtype=tf.float32), # Column 2
tf.constant([], dtype=tf.float32),
tf.constant([], dtype=tf.string)]
col1, col2, col3, col4, col5 = tf.decode_csv(
value, record_defaults=record_defaults)
features = tf.pack([col1, col2, col3, col4])
config = tf.ConfigProto(inter_op_parallelism_threads=2)
with tf.Session(config=config) as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# Retrieve a single instance:
example, label = sess.run([features, col5])
print(example, label)
coord.request_stop()
coord.join(threads)
这段代码正适合我(在TF0.6.0和0.7.1中,以及Python 2.7和3.4中)。您能否尝试在
sess.run()
调用之后添加print(例如,label)
,以确定它是否取得了任何进展?没有发生任何情况,它只是挂起运行步骤。我使用的是Python 3.4。假设它在读取文件(权限、未找到等)时遇到问题,那么它应该抛出一个错误,对吗?是的,当我从其他目录尝试它时(这样就找不到文件),它会以NotFoundError
终止。这些文件(比如它们在远程文件系统上)有什么奇怪的地方会导致它们在读取时阻塞吗?奇怪的是,当文件不可访问时,我不会出错!我在虚拟机上有tf,我的代码通过共享文件夹共享,因此为了检查I/o问题,我将所有内容复制到共享之外的其他目录中,但仍然不起作用。只使用python编写了一段代码,打印了文件的第一行,效果很好。因此tf在打开/读取文件时应该不会有问题。f=open('file0.csv','r')print(f.readline())f.close()1-我稍后会在2上尝试-它工作了,添加该位后没有问题谢谢
import tensorflow as tf
filename_queue = tf.train.string_input_producer(["./iris.data"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [tf.constant([], dtype=tf.float32), # Column 0
tf.constant([], dtype=tf.float32), # Column 1
tf.constant([], dtype=tf.float32), # Column 2
tf.constant([], dtype=tf.float32),
tf.constant([], dtype=tf.string)]
col1, col2, col3, col4, col5 = tf.decode_csv(
value, record_defaults=record_defaults)
features = tf.pack([col1, col2, col3, col4])
config = tf.ConfigProto(inter_op_parallelism_threads=2)
with tf.Session(config=config) as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# Retrieve a single instance:
example, label = sess.run([features, col5])
print(example, label)
coord.request_stop()
coord.join(threads)