采用tensorflow训练深度学习的网络的时候,一般数据集加载都有现成的程序,但是,我们应该知道整个加载流程,在参考一些学习资料之后,对整个流程做个总结。tensorflow有三种数据读取方式,如下进行阐述:
(一)preloaded data:
直接由tensorflow产生数据,然后运行:
import tensorflow as tf
sess = tf.Session()
#设计计算图及产生数据
x1 = tf.constant([1,2,3])
x2 = tf.constant([4,5,6])
y = tf.add(x1,x2)
#session计算
print (sess.run(y))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
(二)feeding:
由python产生数据,然后由placeholder喂给tensorflow:
import tensorflow as tf
sess = tf.Session()
#设计计算图
x1 = tf.placeholder(tf.int16)
x2 = tf.placeholder(tf.int16)#占位符x1,x2
y = tf.add(x1,x2)
#python产生数据
p1 = [1,2,3]
p2 = [4,5,6]
#session计算
print (sess.run(y,feed_dict={x1: p1, x2: p2}))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
(三)reading from file:
比较可知,方法(一)直接在图里面由tensorflow产生数据并计算,当数据量大的时候会出现图传输错误问题。方法(二)当采用占位符的时候,数据量过大也会是很大的传输开销。故可采用tensorflow直接在从文件中读取数据,并解码成可使用的格式。
(1)在开始之前,先讲解几个函数进行对比区分:
(a)tf.train.batch和tf.train.shuffle_batch
tf.train.batch([example, label], batch_size=batch_size, capacity=capacity)
- 1
example, label: 表示样本和样本标签,这个可以是一个样本和一个样本标签
batch_size:返回的一个batch样本集的样本个数
capacity:队列的长度
函数返回值都是一个按照输入顺序的batch的样本和样本标签。
tf.train.shuffle_batch([example, label], batch_size=batch_size, capacity=capacity, min_after_dequeue)
- 1
min_after_dequeue:是出队后,队列至少剩下的数据个数,小于capacity参数的值,否则会出错。也就是说这个函数的输出结果是一个乱序的样本排列的batch,不是按照顺序排列的,返回值都是一个随机的batch的样本及其对应的样本标签。
(b)tf.train.batch_join和tf.train.shuffle_batch_join
tf.train.batch与tf.train.shuffle_batch函数是单个Reader读取,但是可以多线程。tf.train.batch_join与tf.train.shuffle_batch_join可设置多Reader读取,每个Reader使用一个线程。至于两种方法的效率,单Reader时,2个线程就达到了速度的极限。多Reader时,2个Reader就达到了极限。所以并不是线程越多越快,甚至更多的线程反而会使效率下降。
(2)程序实现
(a)首先在终端直接构造数据:
##构造3个文件,A.csv,B.csv,C.csv
echo -e "Alpha1,A1\nAlpha2,A2\nAlpha3,A3">A.csv
echo -e "Bee1,B1\nBee2,B2\nBee3,B3">B.csv
echo -e "Sea1,C1\nSea2,C2\nSea3,C3">C.csv
echo -e "1,1,1,1,1\n0,1,0,1,0\n0,0,1,0,1\n1,0,0,1,0\n0,0,0,0,0">D.csv
- 1
- 2
- 3
- 4
- 5
(b)在.py文件中操作
(b11)单个读取,单个样本(tf.train.batch)
import tensorflow as tf
#生成一个先入先出队列和一个Queuerunner,生成文件名队列
filenames = ['A.csv', 'B.csv', 'C.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
#定义reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
#定义 decoder
example, label = tf.decode_csv(value, record_defaults=[['null'],['null']])#['null']解析为string类型 ,[1]为整型,[1.0]解析为浮点。
example_batch, label_batch = tf.train.batch([example, label], batch_size=1, capacity=200, num_threads=2)#保证样本和标签一一对应
#运行图
with tf.Session() as sess:
coord = tf.train.Coordinator()#创建一个协调器,管理线程
threads = tf.train.start_queue_runners(coord=coord)#启动QueueRunner,此时文件名队列已经进队
for i in range(10):
e_val, l_val = sess.run([example_batch, label_batch])
print (e_val, l_val)
coord.request_stop()
coord.join(threads)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
结果:按顺序输出
[b'Alpha1'] [b'A1']
[b'Alpha2'] [b'A2']
[b'Alpha3'] [b'A3']
[b'Bee1'] [b'B1']
[b'Bee2'] [b'B2']
[b'Bee3'] [b'B3']
[b'Sea1'] [b'C1']
[b'Sea2'] [b'C2']
[b'Sea3'] [b'C3']
[b'Alpha1'] [b'A1']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
(b12)单个读取,单个样本(tf.train.shuffle_batch)
import tensorflow as tf
#生成一个先入先出队列和一个Queuerunner,生成文件名队列
filenames = ['A.csv', 'B.csv', 'C.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
#定义reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
#定义 decoder
example, label = tf.decode_csv(value, record_defaults=[['null'],['null']])
example_batch, label_batch = tf.train.shuffle_batch([example, label], batch_size=1, capacity=200,min_after_dequeue=100, num_threads=2)
#运行图
with tf.Session() as sess:
coord = tf.train.Coordinator()#创建一个协调器,管理线程
threads = tf.train.start_queue_runners(coord=coord)#启动QueueRunner,此时文件名队列已经进队
for i in range(10):
e_val, l_val = sess.run([example_batch, label_batch])
print (e_val, l_val)
coord.request_stop()
coord.join(threads)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
结果:随机顺序输出
[b'Sea1'] [b'C1']
[b'Sea3'] [b'C3']
[b'Alpha3'] [b'A3']
[b'Sea1'] [b'C1']
[b'Sea3'] [b'C3']
[b'Alpha1'] [b'A1']
[b'Bee1'] [b'B1']
[b'Bee3'] [b'B3']
[b'Sea2'] [b'C2']
[b'Alpha1'] [b'A1']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
(b2)单个读取,多个样本:只需要修改上上述的 batch_size的值
(b31)多个读取,多个样本(tf.train.batch_join)
import tensorflow as tf
#生成一个先入先出队列和一个Queuerunner,生成文件名队列
filenames = ['A.csv', 'B.csv', 'C.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
#定义reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
#定义 decoder
record_defaults = [['null'], ['null']]
#定义了多种解码器,每个解码器跟一个reader相连
example_list = [tf.decode_csv(value, record_defaults=record_defaults) for _ in range(2)] # Reader设置为2
# 使用tf.train.batch_join(),可以使用多个reader,并行读取数据。每个Reader使用一个线程。
example_batch, label_batch = tf.train.batch_join(example_list, batch_size=5)
#运行图
with tf.Session() as sess:
coord = tf.train.Coordinator()#创建一个协调器,管理线程
threads = tf.train.start_queue_runners(coord=coord)#启动QueueRunner,此时文件名队列已经进队
for i in range(10):
e_val, l_val = sess.run([example_batch, label_batch])
print (e_val, l_val)
coord.request_stop()
coord.join(threads)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
结果:按顺序输出
[b'Alpha1' b'Alpha2' b'Alpha3' b'Bee1' b'Bee2'] [b'A1' b'A2' b'A3' b'B1' b'B2']
[b'Bee3' b'Sea1' b'Sea2' b'Sea3' b'Alpha1'] [b'B3' b'C1' b'C2' b'C3' b'A1']
[b'Alpha2' b'Alpha3' b'Bee1' b'Bee2' b'Bee3'] [b'A2' b'A3' b'B1' b'B2' b'B3']
[b'Sea1' b'Sea2' b'Sea3' b'Alpha1' b'Alpha2'] [b'C1' b'C2' b'C3' b'A1' b'A2']
[b'Alpha3' b'Bee1' b'Bee2' b'Bee3' b'Sea1'] [b'A3' b'B1' b'B2' b'B3' b'C1']
[b'Sea2' b'Sea3' b'Alpha1' b'Alpha2' b'Alpha3'] [b'C2' b'C3' b'A1' b'A2' b'A3']
[b'Bee1' b'Bee2' b'Bee3' b'Sea1' b'Sea2'] [b'B1' b'B2' b'B3' b'C1' b'C2']
[b'Sea3' b'Alpha1' b'Alpha2' b'Alpha3' b'Bee1'] [b'C3' b'A1' b'A2' b'A3' b'B1']
[b'Bee2' b'Bee3' b'Sea1' b'Sea2' b'Sea3'] [b'B2' b'B3' b'C1' b'C2' b'C3']
[b'Alpha1' b'Alpha2' b'Alpha3' b'Bee1' b'Bee2'] [b'A1' b'A2' b'A3' b'B1' b'B2']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
(b32)多个读取,多个样本(tf.train.shuffle_batch_join)
import tensorflow as tf
#生成一个先入先出队列和一个Queuerunner,生成文件名队列
filenames = ['A.csv', 'B.csv', 'C.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
#定义reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
#定义 decoder
record_defaults = [['null'], ['null']]
#定义了多种解码器,每个解码器跟一个reader相连
example_list = [tf.decode_csv(value, record_defaults=record_defaults) for _ in range(3)] # Reader设置为3
# 使用tf.train.batch_join(),可以使用多个reader,并行读取数据。每个Reader使用一个线程。
example_batch, label_batch = tf.train.shuffle_batch_join(example_list, batch_size=5,capacity=200,min_after_dequeue=100)
#运行图
with tf.Session() as sess:
coord = tf.train.Coordinator()#创建一个协调器,管理线程
threads = tf.train.start_queue_runners(coord=coord)#启动QueueRunner,此时文件名队列已经进队
for i in range(10):
e_val, l_val = sess.run([example_batch, label_batch])
print (e_val, l_val)
coord.request_stop()
coord.join(threads)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
结果:随机顺序输出
[b'Bee3' b'Alpha2' b'Bee2' b'Alpha2' b'Sea1'] [b'B3' b'A2' b'B2' b'A2' b'C1']
[b'Alpha1' b'Alpha3' b'Sea2' b'Alpha1' b'Alpha3'] [b'A1' b'A3' b'C2' b'A1' b'A3']
[b'Bee3' b'Alpha3' b'Alpha3' b'Bee2' b'Bee3'] [b'B3' b'A3' b'A3' b'B2' b'B3']
[b'Sea3' b'Sea2' b'Sea1' b'Bee2' b'Sea3'] [b'C3' b'C2' b'C1' b'B2' b'C3']
[b'Bee2' b'Bee2' b'Sea1' b'Alpha1' b'Alpha3'] [b'B2' b'B2' b'C1' b'A1' b'A3']
[b'Alpha3' b'Alpha1' b'Bee1' b'Bee1' b'Alpha2'] [b'A3' b'A1' b'B1' b'B1' b'A2']
[b'Bee3' b'Alpha2' b'Alpha1' b'Sea3' b'Sea1'] [b'B3' b'A2' b'A1' b'C3' b'C1']
[b'Bee1' b'Bee3' b'Sea1' b'Bee1' b'Bee3'] [b'B1' b'B3' b'C1' b'B1' b'B3']
[b'Sea2' b'Bee2' b'Alpha3' b'Alpha2' b'Alpha1'] [b'C2' b'B2' b'A3' b'A2' b'A1']
[b'Bee2' b'Bee3' b'Bee2' b'Bee3' b'Bee2'] [b'B2' b'B3' b'B2' b'B3' b'B2']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
(b4)迭代控制,设置epoch参数,指定我们的样本在训练的时候只能被用多少轮
import tensorflow as tf
filenames = ['A.csv', 'B.csv', 'C.csv']
#num_epoch: 设置迭代数
filename_queue = tf.train.string_input_producer(filenames, shuffle=False,num_epochs=3)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
record_defaults = [['null'], ['null']]
#定义了多种解码器,每个解码器跟一个reader相连
example_list = [tf.decode_csv(value, record_defaults=record_defaults) for _ in range(2)] # Reader设置为2
# 使用tf.train.batch_join(),可以使用多个reader,并行读取数据。每个Reader使用一个线程。
example_batch, label_batch = tf.train.batch_join(example_list, batch_size=1)
#初始化本地变量
init_local_op = tf.initialize_local_variables()#如果不初始化,运行就会报错
with tf.Session() as sess:
sess.run(init_local_op) #初始化
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
try:
while not coord.should_stop():
e_val, l_val = sess.run([example_batch, label_batch])
print (e_val, l_val)
except tf.errors.OutOfRangeError:
print('Epochs Complete!')
finally:
coord.request_stop()
coord.join(threads)
coord.request_stop()
coord.join(threads)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
(b5)使用队列读取csv
import tensorflow as tf
# 生成一个先入先出队列和一个QueueRunner,生成文件名队列
filenames = ['D.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
# 定义Reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# 定义Decoder
record_defaults = [[1], [1], [1], [1], [1]] #解析为整数
col1, col2, col3, col4, col5 = tf.decode_csv(value,record_defaults=record_defaults)
features = tf.stack([col1, col2, col3])#前3列数据,后2列标签
label = tf.stack([col4,col5])
example_batch, label_batch = tf.train.shuffle_batch([features,label], batch_size=1, capacity=200, min_after_dequeue=100, num_threads=2)
# 运行Graph
with tf.Session() as sess:
coord = tf.train.Coordinator() #创建一个协调器,管理线程
threads = tf.train.start_queue_runners(coord=coord)
for i in range(10):
e_val,l_val = sess.run([example_batch, label_batch])
print (e_val,l_val)
coord.request_stop()
coord.join(threads)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
结果:
[[0 1 0]] [[1 0]]
[[1 0 0]] [[1 0]]
[[1 1 1]] [[1 1]]
[[1 1 1]] [[1 1]]
[[0 0 1]] [[0 1]]
[[0 0 1]] [[0 1]]
[[1 1 1]] [[1 1]]
[[0 1 0]] [[1 0]]
[[0 1 0]] [[1 0]]
[[1 0 0]] [[1 0]]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
参考资料:
http://blog.csdn.net/lujiandong1/article/details/53376802
http://blog.csdn.net/wuguangbin1230/article/details/72810706
http://blog.sina.com.cn/s/blog_6ca0f5eb0102wppn.html
http://blog.csdn.net/ying86615791/article/details/73864381