4

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)

请帮忙。

4

1 回答 1

2

尝试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,这正是您所需要的。

于 2019-12-24T11:55:29.987 回答