"); //-->
tensorflow中的读取数据的队列,不论是对正喜欢Python开发的人来说,还是对已经走入工作中的Python开发工程师来说都是比较难懂的。原因可能之大家对这方面的经验不足吧。下面扣丁学堂Python培训小编就和大家分享一下关于Tensorflow中的tf.train.batch函数的使用。

tensorflow中的读取数据的队列,简单的说,就是计算图是从一个管道中读取数据的,录入管道是用的现成的方法,读取也是。为了保证多线程的时候从一个管道读取数据不会乱吧,所以这种时候读取的时候需要线程管理的相关操作。今天给大家分享一个简单的操作,就是给一个有序的数据,看看读出来是不是有序的,结果发现是有序的,所以直接给代码:
import tensorflow as tf
import numpy as np
def generate_data():
num = 25
label = np.asarray(range(0, num))
images = np.random.random([num, 5, 5, 3])
print('label size :{}, image size {}'.format(label.shape, images.shape))
return label, images
def get_batch_data():
label, images = generate_data()
images = tf.cast(images, tf.float32)
label = tf.cast(label, tf.int32)
input_queue = tf.train.slice_input_producer([images, label], shuffle=False)
image_batch, label_batch = tf.train.batch(input_queue, batch_size=10, num_threads=1, capacity=64)
return image_batch, label_batch
image_batch, label_batch = get_batch_data()
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess, coord)
i = 0
try:
while not coord.should_stop():
image_batch_v, label_batch_v = sess.run([image_batch, label_batch])
i += 1
for j in range(10):
print(image_batch_v.shape, label_batch_v[j])
except tf.errors.OutOfRangeError:
print("done")
finally:
coord.request_stop()
coord.join(threads)
记得那个slice_input_producer方法,默认是要shuffle的哈。
Besides, I would like to comment this code.
1: there is a parameter ‘num_epochs' in slice_input_producer, which controls how many epochs the slice_input_producer method would work. when this method runs the specified epochs, it would report the OutOfRangeRrror. I think it would be useful for our control the training epochs.
2: the output of this method is one single image, we could operate this single image with tensorflow API, such as normalization, crops, and so on, then this single image is feed to batch method, a batch of images for training or testing wouldbe received.
tf.train.batch和tf.train.shuffle_batch的区别用法
tf.train.batch([example, label], batch_size=batch_size, capacity=capacity):[example, label]表示样本和样本标签,这个可以是一个样本和一个样本标签,batch_size是返回的一个batch样本集的样本个数。capacity是队列中的容量。这主要是按顺序组合成一个batch
tf.train.shuffle_batch([example, label], batch_size=batch_size, capacity=capacity, min_after_dequeue)。这里面的参数和上面的一样的意思。不一样的是这个参数min_after_dequeue,一定要保证这参数小于capacity参数的值,否则会出错。这个代表队列中的元素大于它的时候就输出乱的顺序的batch。也就是说这个函数的输出结果是一个乱序的样本排列的batch,不是按照顺序排列的。
上面的函数返回值都是一个batch的样本和样本标签,只是一个是按照顺序,另外一个是随机的。
以上内容是小编简单整理的,小伙伴们先了解一下,如果想要了解更多内容的话可以登录扣丁学堂官网查看详细信息。
扣丁学堂Python在线学习是专业的Python培训机构,不仅有专业的老师和与时俱进的课程体系,还有大量的Python在线教程供学员观看学习。如果你想学好Python开发技术高薪就业的话,那就不要再犹豫了,快到报名扣丁学堂Python在线学习吧。扣丁学堂python学习交流群:816572891。微信号:codingbb
专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们
相关推荐
示波器旋钮掌控术:硬件测试秘笈
光电隔离的电话自动录音接口电路图
英伟达计划到 2026 年使用光在 AI GPU 之间进行通信
专家解读Analogix DisplayPort转VGA接口
中国新的稀土规则要求每月报告产品流向
电压负反馈偏置电路图
全国大学生“飞思卡尔杯”智能汽车竞赛宣传片
[原理图] USB音箱
单调谐放大电路图
[e-book][Linux] Securing and Optimizing Linux - RedHat Editi.part5.rar
示波器探头的作用以及工作原理
Re: 遥控器,距离15m,功能256种,应用什么片子????
[e-book][Linux] Securing and Optimizing Linux - RedHat Editi.part2.rar
LabVIEW讲坛第三季——第十一期:多核时代下的并行编程(上)
ASIC市场龙头厂先抢先赢 第二梯队营收进帐恐等2028年
多家一线资本入场!AI 玩具领军企业跃然创新完成 2 亿元 A 轮融资
[e-book][Linux] Securing and Optimizing Linux - RedHat Editi.part3.rar
48V系统千亿市场爆发!博世、特斯拉、比亚迪竞逐高效电源新战场
求cyclone开发板原理图和PCB!
用示波器也可以测试“电源环路稳定性”
[求助]ADS的路径问题
了解数字示波器,了解泰克
双调谐放大电路图
深圳招聘熟悉c++工程师
[e-book][Linux] Securing and Optimizing Linux - RedHat Editi.part4.rar
----ASP.NET最近非常火热,PHP加入了商业操作来竞争!
LabVIEW讲坛第三季——第十二期:多核时代下的并行编程(下)
据报道英特尔开始授权玻璃基板技术,可能为三星和 Absolics 带来潜在提升
XTR101构成的具有RTD冷端补偿的热电偶输入电路图
示波器能测量什么?