如何从 numpy 数组创建混洗的批处理列表以提供 tensorflow 字典 [英] How to create shuffled batch list from numpy arrays to feed tensorflow dictionnary

查看:30
本文介绍了如何从 numpy 数组创建混洗的批处理列表以提供 tensorflow 字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tensorflow 开发分类器.我的输入和输出是 numpy 数组,示例为行,参数为列.到目前为止,我的代码工作正常,我可以通过直接输入 100% 的输入和输出数组来训练我的网络.现在,我想创建一个洗牌批次列表来提供 tensorflow,但是这一步对我来说不是很清楚.

I am working on a classifier with Tensorflow. My input and output are numpy arrays with examples as rows and parameter as columns. My code is working correctly until now, and I could train my network by directly feeding 100% of my input and output arrays. Now, I would like to create a list of shuffled batches to feed tensorflow, but this step is not so clear for me.

以下是否会创建张量列表(我的批次)?

Does the following creates lists of tensors (my batches)?

input_batch,output_batch=tf.train.shuffle_batch([input,output],batch_size=100,capacity=50,min_after_dequeue=10,enqueue_many=True,allow_smaller_final_batch=True)

然后我试图将这些批次提供给 tensorflow :

Then I am trying to feed this batches to tensorflow :

for epoch in range(0,2000):
     sess.run(train_step,feed_dict={X:input_batch.eval(),Yreal:output_batch.eval()})

但是在这一行之后什么也没有发生.

But nothing happens after this line.

如何正确地从 numpy 数组准备批次并将其提供给 tensorflow 字典?

How can I correctly prepare batches from numpy arrays and feed it to tensorflow dictionnary?

推荐答案

我终于找到了如何为每个 epoch 随机创建不同的批次.我发布了我的解决方案,因为它可能对其他人有用.所以这就是诀窍.

I finally found how to create different batches randomly for each epoch. I post my solution as it may be useful for someone else. So here is the trick.

for epoch in range(0,2000):
    permutation=np.random.permutation(input_size)
    permutation=permutation[0:batch_size]
    batch=[train_set[permutation],train_label[permutation]]
    sess.run(train_step,feed_dict={X:batch[0],Yreal:batch[1]})

这可能不是最性感的方式,但它确实有效.我们在每个时期创建了一个随机列表(排列),从中提取我们的批次.然后将每个批次输入 tensorflow.

It may not be the sexiest way, but it's working. We have a random list (permutation) created at each epoch from which we extract our batches. Each batch is then fed to tensorflow.

这篇关于如何从 numpy 数组创建混洗的批处理列表以提供 tensorflow 字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆