Keras:准确度不变,验证准确度保持为0

时间:2019-06-03 18:35:48

标签: python tensorflow machine-learning keras

我的ML模型需要预测MNIST像素值行和MFCC声波值行是否引用相同的数字(0到9)。我们有一个带有True和False的布尔数组,它表示MNIST行和Audio行中的数字是否相同。

我们为MNIST图像构建一个CNN,为音频文件构建一个RNN。连接此模型的输出,并通过S型激活添加具有1个节点的最终密集层。如果数据引用相同的数字,则期望的输出为True;如果数据引用的不是相同的数字,则期望的输出为False。

但是,我们的训练准确性始终保持在50%左右,并且在许多时期我们的验证准确性均保持在0.0000。我似乎找不到解决方案。我的代码如下:

inputCNN = tf.keras.Input(shape=(28, 28, 1))
CNN = tf.keras.layers.Conv2D(32, (3,3), activation='relu')(inputCNN)
CNN = tf.keras.layers.MaxPooling2D(2,2)(CNN)
CNN = tf.keras.layers.Conv2D(64, (3,3), activation='relu')(CNN)
CNN = tf.keras.layers.MaxPooling2D(2,2)(CNN)
CNN = tf.keras.layers.Conv2D(64, (3,3), activation='relu')(CNN)
CNN = tf.keras.layers.Flatten()(CNN)
CNN = tf.keras.layers.Dense(64, activation='relu')(CNN)
CNN = tf.keras.layers.Dense(10, activation='softmax')(CNN)
modelCNN = tf.keras.Model(inputs=inputCNN, outputs=CNN)

inputRNN = tf.keras.Input(shape=(93,13))
RNN = tf.keras.layers.LSTM(128,return_sequences=True)(inputRNN)
RNN = tf.keras.layers.Dropout(0.2)(RNN)
RNN = tf.keras.layers.LSTM(128)(RNN)
RNN = tf.keras.layers.Dropout(0.2)(RNN)
RNN = tf.keras.layers.Dense(64, activation='relu')(RNN)
RNN = tf.keras.layers.Dense(10, activation='softmax')(RNN)
modelRNN = tf.keras.Model(inputs=inputRNN, outputs=RNN)
# print(modelRNN.summary())

""" now combine the layers """
combined = tf.keras.layers.concatenate([modelRNN.output, modelCNN.output])

final_dense = tf.keras.layers.Dense(1, activation='sigmoid')(combined)

final_model = tf.keras.Model(inputs=[modelCNN.input, modelRNN.input], outputs=final_dense)

final_model.compile(optimizer='adam',
                    loss='binary_crossentropy',
                    metrics=['accuracy'])



final_model.fit([MNIST_traincopy, RNN_traincopy], booleanarray, validation_split = 0.1, epochs= 20, batch_size=32)

modelsummary看起来像这样:

Layer (type)                 Output Shape              Param #   
=================================================================
input_50 (InputLayer)        (None, 93, 13)            0         
_________________________________________________________________
lstm_26 (LSTM)               (None, 93, 128)           72704     
_________________________________________________________________
dropout_9 (Dropout)          (None, 93, 128)           0         
_________________________________________________________________
lstm_27 (LSTM)               (None, 128)               131584    
_________________________________________________________________
dropout_10 (Dropout)         (None, 128)               0         
_________________________________________________________________
flatten_29 (Flatten)         (None, 128)               0         
_________________________________________________________________
dense_74 (Dense)             (None, 64)                8256      
_________________________________________________________________
dense_75 (Dense)             (None, 10)                650       
=================================================================
Total params: 213,194
Trainable params: 213,194
Non-trainable params: 0

1 个答案:

答案 0 :(得分:-1)

您好,原因可能是您没有为模型提供优化器,例如随机梯度后代,学习率衰减,momentun或netsterov,请在final_model.compile之前添加此代码行,但在导入之前新币:

from keras.optimizers import SGD

然后构建您的优化器,尝试更改此参数,但是要使lr保持在0.01 / 0.001的较高水平,则很难使模型很好地泛化,反之亦然,较小的lr很好,但是需要更多的训练时间:

opt = SGD(lr=0.05, decay=0.05 / epocas, momentum=0.9, nesterov=True)

然后将优化器添加到模型中并删除“ adam”:

optimizer=opt

更改如下:

final_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

然后重试训练您的模型。 最好的问候