如何为自己的数据实现 tensorflow 的 next_batch [英] how to implement tensorflow's next_batch for own data
问题描述
在 tensorflow MNIST 教程中mnist.train.next_batch(100)
函数非常方便.我现在正在尝试自己实现一个简单的分类.我的训练数据放在一个 numpy 数组中.我怎样才能为我自己的数据实现类似的功能来给我下一批?
In the tensorflow MNIST tutorial the mnist.train.next_batch(100)
function comes very handy. I am now trying to implement a simple classification myself. I have my training data in a numpy array. How could I implement a similar function for my own data to give me the next batch?
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
Xtr, Ytr = loadData()
for it in range(1000):
batch_x = Xtr.next_batch(100)
batch_y = Ytr.next_batch(100)
推荐答案
您发布的链接说:我们从我们的训练集中获得了一批"一百个随机数据点".在我的例子中,我使用了一个全局函数(不是你例子中的方法),所以语法上会有区别.
The link you posted says: "we get a "batch" of one hundred random data points from our training set". In my example I use a global function (not a method like in your example) so there will be a difference in syntax.
在我的函数中,您需要传递所需的样本数量和数据数组.
In my function you'll need to pass the number of samples wanted and the data array.
这是正确的代码,确保样本具有正确的标签:
Here is the correct code, which ensures samples have correct labels:
import numpy as np
def next_batch(num, data, labels):
'''
Return a total of `num` random samples and labels.
'''
idx = np.arange(0 , len(data))
np.random.shuffle(idx)
idx = idx[:num]
data_shuffle = [data[ i] for i in idx]
labels_shuffle = [labels[ i] for i in idx]
return np.asarray(data_shuffle), np.asarray(labels_shuffle)
Xtr, Ytr = np.arange(0, 10), np.arange(0, 100).reshape(10, 10)
print(Xtr)
print(Ytr)
Xtr, Ytr = next_batch(5, Xtr, Ytr)
print('
5 random samples')
print(Xtr)
print(Ytr)
还有一个演示运行:
[0 1 2 3 4 5 6 7 8 9]
[[ 0 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]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
5 random samples
[9 1 5 6 7]
[[90 91 92 93 94 95 96 97 98 99]
[10 11 12 13 14 15 16 17 18 19]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]]
这篇关于如何为自己的数据实现 tensorflow 的 next_batch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!