【问题标题】:Operands could not broadcast together操作数不能一起广播
【发布时间】:2019-04-20 07:56:10
【问题描述】:

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

我正在使用我已经在其他模型中使用过(并且有效)的相同功能,但这次崩溃了。

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: 现在我再次尝试使用 5 层 NN 和形状

但是,我在这部分代码epoch_cost += minibatch_cost/num_minibatches 处收到此错误

完整的代码如下:

        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)

提前致谢

【问题讨论】:

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


    【解决方案1】:

    我解决了这个问题:

    epoch_cost += np.mean(minibatch_cost)/num_minibatches
    

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

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多