操作数不能一起广播

时间:2018-11-17 08:22:53

标签: python pandas numpy machine-learning deep-learning

我正在尝试使用小批量训练模型,但是出现了....错误。

我正在使用与其他模型相同的功能(并且已起作用),但是这次崩溃了。

def random_mini_batches(X, Y, mini_batch_size = 64):
"""
Creates a list of random minibatches from (X, Y)

Arguments:
X -- input data, of shape (input size, number of examples)
Y -- true "label" vector (1, number of examples)
mini_batch_size - size of the mini-batches, integer

Returns:
mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)
"""

m = X.shape[1]                  # number of training examples
mini_batches = []

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

# Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.
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):
    mini_batch_X = shuffled_X.iloc[:, k * mini_batch_size : k * mini_batch_size + mini_batch_size]
    mini_batch_Y = shuffled_Y[:, k * mini_batch_size : k * mini_batch_size + mini_batch_size]
    mini_batch = (mini_batch_X, mini_batch_Y)
    mini_batches.append(mini_batch)

# Handling the end case (last mini-batch < mini_batch_size)
if m % mini_batch_size != 0:
    mini_batch_X = shuffled_X.iloc[:, num_complete_minibatches * mini_batch_size : m]
    mini_batch_Y = shuffled_Y[:, num_complete_minibatches * mini_batch_size : m]
    mini_batch = (mini_batch_X, mini_batch_Y)
    mini_batches.append(mini_batch)

return mini_batches

我已经在具有20层的X和Y的NN中使用了此功能:X and Y shapes 现在,我尝试将其与5层NN和形状X and Y shapes of the new case

一起使用

但是,在代码epoch_cost += minibatch_cost/num_minibatches的这一部分出现了错误Error

完整的代码是这样的:

        for epoch in range(num_epochs):

        epoch_cost = 0
        num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set

        minibatches = random_mini_batches(X_train, Y_train, minibatch_size)

        for minibatch in minibatches:

            #Select a minibatch
            (minibatch_X, minibatch_Y) = minibatch

            _, minibatch_cost = sess.run([optimizer, cost], feed_dict = {X: minibatch_X, Y: minibatch_Y})

            epoch_cost += minibatch_cost/num_minibatches

    # Print the cost every epoch
        if print_cost == True and epoch % 100 == 0:
            print("Cost after epoch %i: %f" % (epoch, epoch_cost))
        if print_cost == True and epoch % 5 == 0:
            costs.append(epoch_cost)

预先感谢

1 个答案:

答案 0 :(得分:0)

我使用以下方法解决了该问题:

epoch_cost += np.mean(minibatch_cost)/num_minibatches

如果有人有其他解决方案,我很想听听。