首页 > 解决方案 > keras 中的神经网络不收敛

问题描述

我正在 Keras 中构建一个简单的神经网络,如下所示:

# create model
model = Sequential()
model.add(Dense(1000, input_dim=x_train.shape[1], activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Compile model
model.compile(loss='mean_squared_error', metrics=['accuracy'], optimizer='RMSprop')
# Fit the model
model.fit(x_train, y_train, epochs=20, batch_size=700, verbose=2)
# evaluate the model
scores = model.evaluate(x_test, y_test, verbose=0)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

使用数据的形状为:

x_train = (49972, 601) 
y_train = (49972, 1)

我的问题是网络没有收敛,精度固定在 0.0168,如下所示:

Epoch 1/20
 - 1s - loss: 3.2222 - acc: 0.0174
Epoch 2/20
 - 1s - loss: 3.1757 - acc: 0.0187
Epoch 3/20
 - 1s - loss: 3.1731 - acc: 0.0212
Epoch 4/20
 - 1s - loss: 3.1721 - acc: 0.0220
Epoch 5/20
 - 1s - loss: 3.1716 - acc: 0.0225
Epoch 6/20
 - 1s - loss: 3.1711 - acc: 0.0235
Epoch 7/20
 - 1s - loss: 3.1698 - acc: 0.0245
Epoch 8/20
 - 1s - loss: 3.1690 - acc: 0.0251
Epoch 9/20
 - 1s - loss: 3.1686 - acc: 0.0257
Epoch 10/20
 - 1s - loss: 3.1679 - acc: 0.0261
Epoch 11/20
 - 1s - loss: 3.1674 - acc: 0.0267
Epoch 12/20
 - 1s - loss: 3.1667 - acc: 0.0277
Epoch 13/20
 - 1s - loss: 3.1656 - acc: 0.0285
Epoch 14/20
 - 1s - loss: 3.1653 - acc: 0.0288
Epoch 15/20
 - 1s - loss: 3.1653 - acc: 0.0291

我使用 Sklearn 库用相同的数据构建了相同的结构,它运行良好,显示出高于 0.5 的准确度:

model = Pipeline([
        ('classifier', MLPClassifier(hidden_layer_sizes=(1000), activation='relu',
                                     max_iter=20, verbose=2, batch_size=700, random_state=0))
    ])

我完全确定我对两个模型都使用了相同的数据,这就是我准备它的方式:

def load_data():
    le = preprocessing.LabelEncoder()
    with open('_DATA_train.txt', 'rb') as fp:
        train = pickle.load(fp)
    with open('_DATA_test.txt', 'rb') as fp:
        test = pickle.load(fp)

    x_train = train[:,0:(train.shape[1]-1)]
    y_train = train[:,(train.shape[1]-1)]
    y_train = le.fit_transform(y_train).reshape([-1,1])

    x_test = test[:,0:(test.shape[1]-1)]
    y_test = test[:,(test.shape[1]-1)]
    y_test = le.fit_transform(y_test).reshape([-1,1])

    print(x_train.shape, '  ' , y_train.shape)
    print(x_test.shape, '  ' , y_test.shape)
    return x_train, y_train, x_test, y_test

Keras 结构有什么问题?

编辑:

这是一个多类分类问题:y_training [0 ,1, 2, 3]

标签: pythonscikit-learnneural-networkkeras

解决方案


对于多类问题,您的标签应该是一个热编码的。例如,如果选项是 [0 ,1, 2, 3] 并且标签是 1 那么它应该是 [0, 1, 0, 0]。

你的最后一层应该是一个有 4 个单位和激活 softmax 的密集层。

model.add(Dense(4, activation='softmax'))

你的损失应该是 categorical_crossentropy

model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='RMSprop')

推荐阅读