Keras:新数组的总大小必须保持不变

时间:2019-06-06 22:30:08

标签: python tensorflow keras lstm

我正在尝试构建CRNN,但目前在Reshape()函数上遇到错误,提示我原始数组的大小必须保持不变

def CRNN(blockSize, blockCount, inputShape, trainGen, testGen, epochs):

model = Sequential()

# Conv Layer
channels = 32
for i in range(blockCount):
    for j in range(blockSize):
        if (i, j) == (0, 0):
            conv = Conv2D(channels, kernel_size=(5, 5),
                          input_shape=inputShape, padding='same')
        else:
            conv = Conv2D(channels, kernel_size=(5, 5), padding='same')
        model.add(conv)
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Dropout(0.15))
        if j == blockSize - 2:
            channels += 32
    model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
    model.add(Dropout(0.15))

# Feature aggregation across time
model.add(Reshape((9, 960)))

# LSTM layer
model.add(Bidirectional(LSTM(200), merge_mode='ave'))
model.add(Dropout(0.5))

# Linear classifier
model.add(Dense(4, activation='softmax'))


model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adam(),
              metrics=['accuracy']) # F1?


model.fit_generator(trainGen,
                    validation_data=testGen, steps_per_epoch = trainGen.x.size // 20,
                    validation_steps = testGen.x.size // 20,
                    epochs=epochs, verbose=1)
return model

我通过以下方式调用函数:

model = CRNN(4, 6, (285, 33, 1), trainGen, testGen, 1)

所以我的输入形状是(285,33,1)。

收到的技术错误是:

  

ValueError:新数组的总大小必须保持不变。

有什么方法可以动态获取值或只是找到合适的形状?

0 个答案:

没有答案