Tensorflow读取csv文件时报错的解决办法

这几天在折腾tensorflow,发现在读取csv文件时报错:
tensorflow.python.framework.errors_impl.OutOfRangeError: FIFOQueue
先看一下源码:

import os
import tensorflow as tf
file_name = os.listdir("E:\\program\\python\\tensorflow\\csv_test_data\\")
file_list = [os.path.join("E:\\program\\python\\tensorflow\\csv_test_data\\", file) for file in file_name]

file_queue = tf.train.string_input_producer(file_list) # file_list: csv文件路径列表

reader = tf.TextLineReader(skip_header_lines=1) # skip_header_lines 指定跳过几行
key, value = reader.read(file_queue) # key:行号 value: 内容

records = [[1.0], [1]] # 指定每一列的类型,1.0表示是浮点型,缺失则为1.0, 1表示整型,缺失则为1,“None”表示字符串,缺失则为None
example, label = tf.decode_csv(value, record_defaults=records) # 返回的是一行的数据

example_batch, label_batch = tf.train.batch([example, label], batch_size=6, num_threads=2, capacity=6) # batch_size:要读取多少行 num_threads:指定多少个子线程 capacity:指定队列容量

with tf.Session() as sess:
    # init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    # sess.run(init_op)
    coord = tf.train.Coordinator()  # 定义一个线程协调器
    threads = tf.train.start_queue_runners(sess, coord=coord)  # 开启读取文件的线程
    data = sess.run([example_batch, label_batch])  # 获取数据
    coord.request_stop()  # 请求关闭线程
    coord.join(threads)  # 主线程等待子线程结束

这个代码在网上很常见,居然也会报错,我也是一脸无奈啊。根据错误去搜索解决方案,发现了一篇有价值的文章【https://blog.csdn.net/qq_34638161/article/details/80387829】,按照文中的方法去尝试解决,发现不奏效,虽然事后证明它给出的原因是对的–没有进行变量初始化。然后去看它的参考文献,发现其中的一篇文章能够解决。【https://tutel.me/c/programming/questions/45189978/tftrainbatch+is+showing+insufficient+elements】
直接看最后一个人的回答即可,【@Mohamed Lakhal 2017-07-19 14:36:35】。
正确的代码应该是:

import os
import tensorflow as tf
file_name = os.listdir("E:\\program\\python\\tensorflow\\csv_test_data\\")
file_list = [os.path.join("E:\\program\\python\\tensorflow\\csv_test_data\\", file) for file in file_name]

file_queue = tf.train.string_input_producer(file_list) # file_list: csv文件路径列表

reader = tf.TextLineReader(skip_header_lines=1) # skip_header_lines 指定跳过几行
key, value = reader.read(file_queue) # key:行号 value: 内容

records = [[1.0], [1]] # 指定每一列的类型,1.0表示是浮点型,缺失则为1.0, 1表示整型,缺失则为1,“None”表示字符串,缺失则为None
example, label = tf.decode_csv(value, record_defaults=records) # 返回的是一行的数据

example_batch, label_batch = tf.train.batch([example, label], batch_size=6, num_threads=2, capacity=6) # batch_size:要读取多少行 num_threads:指定多少个子线程 capacity:指定队列容量

global_init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run(global_init_op)
    sess.run(local_init_op)

    # init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    # sess.run(init_op)
    coord = tf.train.Coordinator()  # 定义一个线程协调器
    threads = tf.train.start_queue_runners(sess, coord=coord)  # 开启读取文件的线程
    data = sess.run([example_batch, label_batch])  # 获取数据
    coord.request_stop()  # 请求关闭线程
    coord.join(threads)  # 主线程等待子线程结束

能够发现问题是一个层级,能够解决发现的问题是另一个层级,努力吧,骚年!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值