【问题标题】:Theano MiniBatch Iterator not workingTheano MiniBatch 迭代器不工作
【发布时间】:2017-01-12 15:55:18
【问题描述】:

Theano MiniBatch 迭代器不工作

我编写了一个小批量迭代器来从我的神经网络中获取预测结果。 但是,我做了一些测试,发现了一些错误。

基本上:

If batch_size > amount of inputs  : error

我编写了一个脚本来在我的代码中显示这个错误。如下图:

import numpy as np

def minibatch_iterator_predictor(inputs, batch_size):
    assert len(inputs) > 0

    for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
        excerpt = slice(start_idx, start_idx + batch_size)
        yield inputs[excerpt]


def test(x, batch_size):
    prediction = np.empty((x.shape[0], 2), dtype=np.float32)

    index = 0
    for batch in minibatch_iterator_predictor(inputs=x, batch_size=batch_size):
        inputs = batch

        # y = self.predict_function(inputs)
        y = inputs

        prediction[index * batch_size:batch_size * (index + 1), :] = y[:]
        index += 1
    return prediction

######################################
#TEST SCRIPT
######################################

#Input
arr = np.zeros(shape=(10, 2))

arr[0] = [1, 0]
arr[1] = [2, 0]
arr[2] = [3, 0]
arr[3] = [4, 0]
arr[4] = [5, 0]
arr[5] = [6, 0]
arr[6] = [7, 0]
arr[7] = [8, 0]
arr[8] = [9, 0]
arr[9] = [10, 0]

###############################################

batch_size = 5
print "\nBatch_size ", batch_size
r = test(x=arr, batch_size=batch_size)

#Debug
for k in xrange(r.shape[0]):
        print str(k) + " : " + str(r[k])

##Assert

assert arr.shape[0] == r.shape[0]

for k in xrange(0,r.shape[0]):
    print r[k] == arr[k]

这里是测试

对于 batch_size = 10:

Batch_size  10
0 : [ 1.  0.]
1 : [ 2.  0.]
2 : [ 3.  0.]
3 : [ 4.  0.]
4 : [ 5.  0.]
5 : [ 6.  0.]
6 : [ 7.  0.]
7 : [ 8.  0.]
8 : [ 9.  0.]
9 : [ 10.   0.]

对于 batch_size = 11:

0 : [  1.13876845e-37   0.00000000e+00]
1 : [  1.14048027e-37   0.00000000e+00]
2 : [  1.14048745e-37   0.00000000e+00]
3 : [  9.65151604e-38   0.00000000e+00]
4 : [  1.14002468e-37   0.00000000e+00]
5 : [  1.14340036e-37   0.00000000e+00]
6 : [  1.14343264e-37   0.00000000e+00]
7 : [  8.02794698e-38   0.00000000e+00]
8 : [  8.02794698e-38   0.00000000e+00]
9 : [  8.02794698e-38   0.00000000e+00]

对于 Batch_size 12

0 : [  1.13876845e-37   0.00000000e+00]
1 : [  1.14048027e-37   0.00000000e+00]
2 : [  1.14048745e-37   0.00000000e+00]
3 : [  9.65151604e-38   0.00000000e+00]
4 : [  1.14002468e-37   0.00000000e+00]
5 : [  1.14340036e-37   0.00000000e+00]
6 : [  1.14343264e-37   0.00000000e+00]
7 : [  8.10141537e-38   0.00000000e+00]
8 : [  8.10141537e-38   0.00000000e+00]
9 : [  8.10141537e-38   0.00000000e+00]

我该如何解决这个问题?

【问题讨论】:

    标签: python theano lasagne


    【解决方案1】:

    请尝试在问题中更具体。您究竟想解决什么问题?

    没有任何错误。 当批大小大于输入时,函数minibatch_iterator_predictor 会产生一个空迭代器,并且不会执行循环for batch in minibatch_iterator_predictor(inputs=x, batch_size=batch_size)

    当 batch_size 大于输入数量时,你得到的只是初始化中的零:prediction = np.empty((x.shape[0], 2), dtype=np.float32)

    您可以做的是将 max batch_size 限制为输入的数量:

    def minibatch_iterator_predictor(inputs, batch_size):
        assert len(inputs) > 0
        if batch_size > len(inputs):
            batch_size = len(inputs)
    
        for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
            excerpt = slice(start_idx, start_idx + batch_size)
            yield inputs[excerpt]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2010-11-16
      • 2012-08-28
      • 1970-01-01
      • 2015-10-22
      相关资源
      最近更新 更多