检查输入时出错:预期density_1_input具有2维,但数组的形状为(25000,700,50)

时间:2019-12-24 09:35:06

标签: python mlp

trainData.shape =(25000,700,50),形状如下:

[[[ 0.7095   0.863    0.712   ...  0.02715 -1.305    0.5195 ]
  [-0.66     1.715   -1.934   ...  0.5684   0.754    0.2593 ]
  [-0.3533   2.256   -1.292   ... -0.2708   0.6714  -1.128  ]
  ...
  [ 0.       0.       0.      ...  0.       0.       0.     ]
  [ 0.       0.       0.      ...  0.       0.       0.     ]
  [ 0.       0.       0.      ...  0.       0.       0.     ]]
  ...

trainLabel.shape =(25000,),,形状如下:

[1. 1. 1. ... 0. 0. 0.]

使用它们来训练MLP模型,我应该如何重塑trainData和trainLabel?详细代码如下:

def MySimpleMLP(feature=700, vec_size=50):
    auc_roc = LSTM.as_keras_metric(tf.compat.v1.metrics.auc)

    model = Sequential()

    model.add(Dense(32, activation='relu', input_shape=(feature * vec_size,)))
    model.add(Dropout(0.2))
    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='softmax'))

    model.compile(loss="binary_crossentropy", optimizer="adam", metrics=[auc_roc])
    return model

 ......       

 model.fit(trainData, trainLabel, validation_split=0.2, epochs=10, batch_size=64, verbose=2)

请帮助。

1 个答案:

答案 0 :(得分:0)

尝试像这样添加Flatten层:

def MySimpleMLP(feature=700, vec_size=50):
    auc_roc = LSTM.as_keras_metric(tf.compat.v1.metrics.auc)

    model = Sequential()

    model.add(Dense(32, activation='relu', input_shape=(feature * vec_size,)))
    model.add(Dropout(0.2))
    model.add(Dense(32, activation='relu'))
    model.add(Flatten())
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='softmax'))

    model.compile(loss="binary_crossentropy", optimizer="adam", metrics=[auc_roc])
    return model

 ......       

 model.fit(trainData, trainLabel, validation_split=0.2, epochs=10, batch_size=64, verbose=2)

Flatten将(num_of_samples,64,32,32)数组转换为(num_of_samples,64 * 32 * 32)数组,即它使数组成为2D,这正是您所需要的。