首页 > 其他 > 详细

Tensorflow学习2 数据读取

时间:2020-05-20 00:10:46      阅读:109      评论:0      收藏:0      [点我收藏+]

=======数据说明=======

text1.txt

0.10, 0.21, 0.56, 0.72, 0.31
0.23, 0.34, 0.3, 0.76, 0.21

text2.txt

0.60, 0.92, 0.12, 0.83, 0.11
0.89, 0.71, 0.90, 0.40, 0.55

======================

技术分享图片
#搭建输入管道
import tensorflow as tf
import numpy as np
graph = tf.Graph()
session = tf.compat.v1.InteractiveSession(graph = graph)
定义图与会话对象

1)文件读取

filenames = [text%d.txt%i for i in range(1,3)]
filename_queue = tf.compat.v1.train.string_input_producer(filenames,capacity=2,shuffle=True,name=string_input_producer)
reader = tf.compat.v1.TextLineReader()
_,value = reader.read(filename_queue,name=text_read)

根据文件名生产文件名队列filename_queue

读取器类reader将文件队列作为参数。实验数据中一行为一个数据,因此使用TextLineReader

value返回读取器从每一行读到的值

2)文本解码

record_defaults = [[-1.0]]*5
col1,col2,col3,col4,col5 = tf.compat.v1.decode_csv(value,record_defaults = record_defaults)

record_default,指定分割后每个属性的类型,也可以不指定类型[]

decode_csv,value原始是string,解析为数字

3)转成训练格式

features = tf.stack([col1,col2,col3,col4,col5])
x = tf.compat.v1.train.shuffle_batch([features],batch_size=3,capacity=5,name=batch,min_after_dequeue=1,num_threads=1)

stack方法将各个列拼一个张量

shuffle_batch将张量打乱批量输出;train.batch不打乱数据输出

4)线程管理

coord = tf.train.Coordinator()
threads = tf.compat.v1.train.start_queue_runners(coord = coord,sess = session)

任务结束后需要显示的停止线程,加入主线程

coord.request_stop()
coord.join(threads)

5)作为模型输入

W = tf.compat.v1.Variable(tf.compat.v1.random_uniform(shape = [5,5], minval = -0.1, maxval = 0.1, dtype = tf.float32),name = W)
b = tf.compat.v1.Variable(tf.zeros(shape = [5], dtype = tf.float32), name = b)
#variable 变量需要初始化random_uniform在minval-maxval之间均匀采样
h = tf.nn.sigmoid(tf.matmul(x,W)+b)
tf.compat.v1.global_variables_initializer().run() 
for step in range(3):
    x_eval,h_eval = session.run([x,h])
    print(==========Step%d===========%step)
    print(data(x))
    print(x_eval)
    print(data(h))
    print(h_eval)
==========Step0===========
Evaluated data(x)
[[0.6  0.92 0.12 0.83 0.11]
 [0.1  0.21 0.56 0.72 0.31]
 [0.23 0.34 0.3  0.76 0.21]]
Evaluated data(h)
[[0.5236712  0.5169494  0.4832768  0.49572295 0.50606287]
 [0.5182559  0.51618594 0.48345992 0.5071334  0.499376  ]
 [0.5158963  0.51570374 0.4850265  0.50364953 0.50234705]]
==========Step1===========
Evaluated data(x)
[[0.89 0.71 0.9  0.4  0.55]
 [0.6  0.92 0.12 0.83 0.11]
 [0.23 0.34 0.3  0.76 0.21]]
Evaluated data(h)
[[0.5350291  0.5160627  0.48820648 0.49639583 0.50967765]
 [0.5236712  0.5169494  0.4832768  0.49572295 0.50606287]
 [0.5158963  0.51570374 0.4850265  0.50364953 0.50234705]]
==========Step2===========
Evaluated data(x)
[[0.1  0.21 0.56 0.72 0.31]
 [0.89 0.71 0.9  0.4  0.55]
 [0.6  0.92 0.12 0.83 0.11]]
Evaluated data(h)
[[0.5182559  0.51618594 0.48345992 0.5071334  0.499376  ]
 [0.5350291  0.5160627  0.48820648 0.49639583 0.50967765]
 [0.5236712  0.5169494  0.4832768  0.49572295 0.50606287]]

6)采坑指南

出现如下报错一般为数据本身出错,如某行数据列数不符合要求

RandomShuffleQueue ‘_55_batch/random_shuffle_queue‘ is closed and has insufficient elements (requested 3, current size 2)

Tensorflow学习2 数据读取

原文:https://www.cnblogs.com/jessyswing/p/12920396.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!