zoukankan      html  css  js  c++  java
  • TensorFlow------读取CSV文件实例

    TensorFlow之读取CSV文件实例:

    import tensorflow as tf
    import os
    
    
    def csvread(filelist):
        '''
        读取CSV文件
        :param filename:  路径+文件名的列表
        :return: 读取内容
        '''
    
        # 1. 构造文件的队列
        file_queue = tf.train.string_input_producer(filelist)
    
        # 2. 构造csv阅读器读取队列数据(按一行)
        reader = tf.TextLineReader()
    
        key,value = reader.read(file_queue)
    
        # 3.对每行内容解码
        # record_defaults:指定每一个样本的每一列的类型,指定默认值[['None'],[4.0]]
        records = [['None'],['None']]
    
        example,label = tf.decode_csv(value,record_defaults=records)
    
        # batch_size跟队列,数据的数量没有影响,只决定这批次取多少数据
        # 4. 想要读取多个数据,就需要批处理
        example_batch,label_batch = tf.train.batch([example,label],batch_size=9,num_threads=1,capacity=9)
        # print(example,label)
        return example_batch,label_batch
    
    if __name__ == '__main__':
        # 找到文件,构建列表
        filename = os.listdir('./data/csvdata/')
    
        # 拼接路径 重新组成列表
        filelist = [os.path.join('./data/csvdata/',file) for file in filename]
    
        # 调用函数传参
        example_batch,label_batch = csvread(filelist)
    
        # 开启会话
        with tf.Session() as sess:
            # 定义一个线程协调器
            coord = tf.train.Coordinator()
    
            # 开启读文件的线程
            threads = tf.train.start_queue_runners(sess,coord=coord)
    
            # 打印读取的内容
            print(sess.run([example_batch,label_batch]))
    
    
            # 回收子线程
            coord.request_stop()
    
            coord.join(threads)
  • 相关阅读:
    git 通过 fork 解决代码冲突
    vue-cli3.0 多页面和单页面的配置以及相互之间的切换
    关于切换环境变量的问题( 以vue-cli3.0 为例)
    vue-router 钩子
    Android eMMC 分区详解(转载)
    《PE总结 》– 重定位表(转载)
    Linux 文件系统
    爬虫登录,立FLAG
    ios tweak 开发
    ios app 砸壳
  • 原文地址:https://www.cnblogs.com/fwl8888/p/9762445.html
Copyright © 2011-2022 走看看