### 添加MaxPooling 2D – ValueError: 新数组的总大小必须保持不变

我创建了以下模型:

def create_model(input_shape = (224, 224, 3)):    input_img = Input(shape=input_shape)    model = efnB0_model (input_img)    model = MaxPooling2D(pool_size=(2, 2), strides=2)(model)    backbone = Flatten() (model)    backbone = model    branches = []    for i in range(7):            branches.append(backbone)            branches[i] = Dense(360, name="branch_"+str(i)+"_Dense_360")(branches[i])            branches[i] = Activation("relu") (branches[i])            branches[i] = BatchNormalization()(branches[i])            branches[i] = Dropout(0.2)(branches[i])                       branches[i] = Dense(35, activation = "softmax", name="branch_"+str(i)+"_output")(branches[i])            output = Concatenate(axis=1)(branches)    output = Reshape((7, 35))(output)    model = Model(input_img, output)    return model

当我现在运行:

model = create_model()

我得到了这个错误:

---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-82-834f03506210> in <module>()----> 1 model = create_model()4 frames/usr/local/lib/python3.6/dist-packages/keras/layers/core.py in _fix_unknown_dimension(self, input_shape, output_shape)    385             output_shape[unknown] = original // known    386         elif original != known:--> 387             raise ValueError(msg)    388     389         return tuple(output_shape)ValueError: total size of new array must be unchanged

在此之前,我的模型如下,并且我没有遇到这个错误:

def create_model(input_shape = (224, 224, 3)):    input_img = Input(shape=input_shape)    model = efnB0_model (input_img)    model = GlobalAveragePooling2D(name='avg_pool')(model)    model = Dropout(0.2)(model)    backbone = model    branches = []    for i in range(7):            branches.append(backbone)            branches[i] = Dense(360, name="branch_"+str(i)+"_Dense_360")(branches[i])            branches[i] = Activation("relu") (branches[i])            branches[i] = BatchNormalization()(branches[i])            branches[i] = Dropout(0.2)(branches[i])                      branches[i] = Dense(35, activation = "softmax", name="branch_"+str(i)+"_output")(branches[i])            output = Concatenate(axis=1)(branches)    output = Reshape((7, 35))(output)    model = Model(input_img, output)    return model

因此,这个错误似乎是由于添加了MaxPooling2D层以及删除了GlobalAveragePoolingDropout层而发生的。

我应该如何修改我的代码?

谢谢!


回答:

错误在这里 backbone = Flatten()(model)

用以下代码修正:

model = Flatten()(model)

这里是完整的代码: https://colab.research.google.com/drive/12Fa-h12nCsPO1xkPEVnX99iE7jNDuo0A?usp=sharing

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注