1

我打算从包含“m”个示例的训练集中为我的深度学习神经网络程序创建小批量。我努力了:

# First Shuffle (X, Y)
permutation = list(np.random.permutation(m))
shuffled_X = X[:, permutation]
shuffled_Y = Y[:, permutation].reshape((1,m))

# Partition (shuffled_X, shuffled_Y). Minus the end case where mini-batch will contain lesser number of training samples.
num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning
for k in range(0, num_complete_minibatches):
    ### START CODE HERE ### (approx. 2 lines)
    mini_batch_X = shuffled_X[mini_batch_size*k:mini_batch_size*(k+2)]
    mini_batch_Y = shuffled_Y[mini_batch_size*k:mini_batch_size*(k+2)]

但这给了我以下结果:

shape of the 1st mini_batch_X: (128, 148)
shape of the 2nd mini_batch_X: (128, 148)
shape of the 3rd mini_batch_X: (12288, 148)
shape of the 1st mini_batch_Y: (1, 148)
shape of the 2nd mini_batch_Y: (0, 148)
shape of the 3rd mini_batch_Y: (1, 148)
mini batch sanity check: [ 0.90085595 -0.7612069   0.2344157 ]

预期的输出是:

shape of the 1st mini_batch_X   (12288, 64)
shape of the 2nd mini_batch_X   (12288, 64)
shape of the 3rd mini_batch_X   (12288, 20)
shape of the 1st mini_batch_Y   (1, 64)
shape of the 2nd mini_batch_Y   (1, 64)
shape of the 3rd mini_batch_Y   (1, 20)
mini batch sanity check [ 0.90085595 -0.7612069 0.2344157 ] 

我确定我已经实施但无法弄清楚的切片有问题。任何帮助深表感谢。谢谢!

4

1 回答 1

1

我认为您没有正确切片 numpy 数组。最初,当您以这种方式改组数组时是正确的。您不想对第一个维度进行切片,因此请保持原样使用:并使用<Start Index>:<End Index>. 这就是我在下面的代码中所做的。

for k in range(num_complete_minibatches+1):
### START CODE HERE ### (approx. 2 lines)
    mini_batch_X = shuffled_X[:,mini_batch_size*(k):mini_batch_size*(k+1)]
    mini_batch_Y = shuffled_Y[:,mini_batch_size*(k):mini_batch_size*(k+1)]
    print(mini_batch_X.shape,mini_batch_Y.shape)
于 2020-07-02T18:13:59.650 回答