keras中的神经网络未收敛

时间:2018-07-06 12:11:07

标签: python scikit-learn neural-network 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]

1 个答案:

答案 0 :(得分:2)

对于多类问题,您的标签应采用一种热编码方式。例如,如果选项为[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')