当前位置:网站首页>Tensorflow中CSV文件数据读取
Tensorflow中CSV文件数据读取
2022-06-25 22:08:00 【smileapples】
Tensorflow中CSV文件数据读取主要步骤:
1、找到文件 构造文件列表
2、构建一个文件队列
3、构建文件阅读器 读取队列内容(一行)
4、文件解码
5、批处理读取大量数据
代码实现如下:
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2"
tf = tf.compat.v1
def read_csvfile(filelist):
''' 读取CSV文件 :param file_list:文件名列表 :return: '''
# 1构造文件队列
filequeue = tf.train.string_input_producer(filelist)
# 2构造csv阅读器读取
reader = tf.TextLineReader()
key,value = reader.read(filequeue)
# print(key,value)
# 3解码文件
records = [["None"],["None"]]
example,label = tf.decode_csv(value,record_defaults=records) #record_defaults指定每一个样本每一列的类型
# print(example,label)
# 4批处理读取多个数据
example_batch, label_batch = tf.train.batch([example, label], batch_size=10, num_threads=2, capacity=9)
# print(example_batch, label_batch)
return example_batch,label_batch
if __name__ == "__main__":
# 0获取文件 构建文件列表
# 获取文件名
filenames = os.listdir("../data/csvdata/")
# print(filenames)
#拼接路径形成完整文件名称
filelist = [os.path.join("../data/csvdata",filename) for filename in filenames]
# print(filelist)
example_batch,label_batch = read_csvfile(filelist)
# 开启会话,运行结果
with tf.Session() as sess:
# 定义一个线程协调器
coord = tf.train.Coordinator()
# 开启读取文件的线程
filereadthreads = tf.train.start_queue_runners(sess,coord=coord)
# 输出读取的内容
print(sess.run([example_batch,label_batch]))
# 回收子线程
coord.request_stop()
coord.join(filereadthreads)
边栏推荐
- Database - mongodb
- C. Planar Reflections-CodeCraft-21 and Codeforces Round #711 (Div. 2)
- Analysis and comprehensive summary of full type equivalent judgment in go
- sqlServer2008中float类型数据与datetime类型数据互转
- Customize the qcombobox drop-down box, right align the display, and slide the drop-down list
- Solving typeerror: Unicode objects must be encoded before hashing
- php性能优化
- Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]
- C# IO Stream 流(二)扩展类_封装器
- mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
猜你喜欢

C# IO Stream 流(一)基础概念_基本定义

6.常用指令(上)v-cloak,v-once,v-pre

18亿像素火星全景超高清NASA放出,非常震撼

使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容

idea Kotlin版本升级

为什么Integer的比较最好使用equals

Hbuilderx uses the gaude map to obtain the current location

Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or

第六章 习题(678)【微机原理】【习题】

UE4 learning records create a role and control its movement
随机推荐
如何进行流程创新,以最经济的方式提升产品体验?
Two ways to center block level elements
两种块级元素居中的方式
先序线索二叉树
php进程间传递文件描述符
动态验证码
Solve 'tuple' object has no attribute 'lower‘
xtrabackup的备份还原
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
Px4 simulation basis
Talk about how to hot restart a spoole or PHP cli process
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
Run the dronekit flight control application on Shumei faction under px4-jmavsim software simulation environment
String object (constant) pool
debezium
How does excel translate Chinese words into English automatically? This formula teaches you
Database - mongodb
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
产品经理如何把控产品开发的进度
php中使用google protobuf协议环境配置