val_loss减半,但val_acc保持不变

时间:2019-03-08 15:04:43

标签: python machine-learning keras neural-network deep-learning

我正在训练一个神经网络,并获得以下输出。损失和val_loss都在减少,这让我很高兴。但是,val_acc保持不变。那有什么原因呢?我的数据非常不平衡,但是我正在通过sklearn compute_class_weight函数对其进行加权。

Train on 109056 samples, validate on 27136 samples
Epoch 1/200
- 1174s - loss: 1.0353 - acc: 0.5843 - val_loss: 1.0749 - val_acc: 0.7871

Epoch 00001: val_acc improved from -inf to 0.78711, saving model to 
nn_best_weights.h5
Epoch 2/200
- 1174s - loss: 1.0122 - acc: 0.6001 - val_loss: 1.0642 - val_acc: 0.9084

Epoch 00002: val_acc improved from 0.78711 to 0.90842, saving model to 
nn_best_weights.h5
Epoch 3/200
- 1176s - loss: 0.9974 - acc: 0.5885 - val_loss: 1.0445 - val_acc: 0.9257

Epoch 00003: val_acc improved from 0.90842 to 0.92571, saving model to 
nn_best_weights.h5
Epoch 4/200
- 1177s - loss: 0.9834 - acc: 0.5760 - val_loss: 1.0071 - val_acc: 0.9260

Epoch 00004: val_acc improved from 0.92571 to 0.92597, saving model to 
nn_best_weights.h5
Epoch 5/200
- 1182s - loss: 0.9688 - acc: 0.5639 - val_loss: 1.0175 - val_acc: 0.9260

Epoch 00005: val_acc did not improve from 0.92597
Epoch 6/200
- 1177s - loss: 0.9449 - acc: 0.5602 - val_loss: 0.9976 - val_acc: 0.9246

Epoch 00006: val_acc did not improve from 0.92597
Epoch 7/200
- 1186s - loss: 0.9070 - acc: 0.5598 - val_loss: 0.9667 - val_acc: 0.9258

Epoch 00007: val_acc did not improve from 0.92597
Epoch 8/200
- 1178s - loss: 0.8541 - acc: 0.5663 - val_loss: 0.9254 - val_acc: 0.9221

Epoch 00008: val_acc did not improve from 0.92597
Epoch 9/200
- 1171s - loss: 0.7859 - acc: 0.5853 - val_loss: 0.8686 - val_acc: 0.9237

Epoch 00009: val_acc did not improve from 0.92597
Epoch 10/200
- 1172s - loss: 0.7161 - acc: 0.6139 - val_loss: 0.8119 - val_acc: 0.9260

Epoch 00010: val_acc did not improve from 0.92597
Epoch 11/200
- 1168s - loss: 0.6500 - acc: 0.6416 - val_loss: 0.7531 - val_acc: 0.9259

Epoch 00011: val_acc did not improve from 0.92597
Epoch 12/200
- 1164s - loss: 0.5967 - acc: 0.6676 - val_loss: 0.7904 - val_acc: 0.9260

Epoch 00012: val_acc did not improve from 0.92597
Epoch 13/200
- 1175s - loss: 0.5608 - acc: 0.6848 - val_loss: 0.7589 - val_acc: 0.9259

Epoch 00013: val_acc did not improve from 0.92597
Epoch 14/200
- 1221s - loss: 0.5377 - acc: 0.6980 - val_loss: 0.7811 - val_acc: 0.9260

Epoch 00014: val_acc did not improve from 0.92597

我的模型如下。我知道内核很大,但这是有目的的,因为数据是以某种方式构造的。

    cnn = Sequential()
    cnn.add(Conv2D(16, kernel_size=(2, 100), padding='same', data_format="channels_first", input_shape=(1,10, 100)))
    cnn.add(LeakyReLU(alpha=0.01))
    cnn.add(BatchNormalization())
    cnn.add(Conv2D(16, (1, 1)))
    cnn.add(LeakyReLU(alpha=0.01))
    cnn.add(Conv2D(16, (1, 8)))
    cnn.add(Flatten()) 
    rnn = Sequential()
    rnn = LSTM(100, return_sequences=False, dropout=0.2)
    dense = Sequential()
    dense.add(Dense(3, activation='softmax'))
    main_input = Input(batch_shape=(512, 1, 1, 10, 100)) 
    model = TimeDistributed(cnn)(main_input) 
    model = rnn(model) 
    model = dense(model) 
    replica = Model(inputs=main_input, outputs=model)
    replica.compile(loss='categorical_crossentropy', optimizer='adam',  metrics=['accuracy'])

2 个答案:

答案 0 :(得分:1)

很难回答不知道您的模型的问题。

可能的答案是:

  • 您的模型没有错。这可能是最高的    您可以获得的准确性。
  • 您的数据可能不平衡,也可能不混洗。高于acc的val_acc越高,表明训练,评估可能有问题    并测试拆分。在开始时,火车精度往往比val_acc高。然后val_acc是否赶上了;)我还可以指出您的数据集中没有太多方差,那么您可能会有这种行为。
  • 您的学习率可能很高。尝试减少它。

我想模型要最小化的实际指标是损失,因此在优化过程中,您应该跟踪损失并监视其改进。

检查this链接以获取有关如何检查模型的更多信息。

答案 1 :(得分:1)

似乎是学习率过高,缺少局部最小值并阻止神经网络改善学习的情况:

Gradient

如果您可以自定义优化器,那就好了,

learning_rate = 0.008
decay_rate = 5e-6
momentum = 0.65

sgd = SGD(lr=learning_rate,momentum=momentum, decay=decay_rate, nesterov=False)
model.compile(loss="categorical_crossentropy", optimizer=sgd,metrics=['accuracy'])  

此外,增加卷积数。重量可能已饱和。