训练Keras顺序模型时损失不会减少

时间:2019-04-29 20:14:25

标签: python pandas keras classification

我正在创建一个非常简单的2层前馈网络,但是发现丢失根本没有更新。我有一些想法,但我想获得其他反馈/指导。

有关数据的详细信息:

X_train: (336876,158)

X_dev: (42109,158)

Y_train计数:

0    285793
1     51083
Name: default, dtype: int64

Y_dev计数:

0    35724
1     6385
Name: default, dtype: int64

这是我的模型架构:

# define the architecture of the network
model = Sequential()
model.add(Dense(50, input_dim=X_train.shape[1], init="uniform", activation="relu"))
model.add(Dense(3print("[INFO] compiling model...")

adam = Adam(lr=0.01)
model.compile(loss="binary_crossentropy", optimizer=adam,
    metrics=['accuracy'])
model.fit(np.array(X_train), np.array(Y_train), epochs=12, batch_size=128,  verbose=1)Dense(1, activation = 'sigmoid'))

现在,有了这个,我在前几个时期后的损失如下:

Epoch 1/12
336876/336876 [==============================] - 8s - loss: 2.4441 - acc: 0.8484     
Epoch 2/12
336876/336876 [==============================] - 7s - loss: 2.4441 - acc: 0.8484     
Epoch 3/12
336876/336876 [==============================] - 6s - loss: 2.4441 - acc: 0.8484     
Epoch 4/12
336876/336876 [==============================] - 7s - loss: 2.4441 - acc: 0.8484     
Epoch 5/12
336876/336876 [==============================] - 7s - loss: 2.4441 - acc: 0.8484     
Epoch 6/12
336876/336876 [==============================] - 7s - loss: 2.4441 - acc: 0.8484     
Epoch 7/12
336876/336876 [==============================] - 7s - loss: 2.4441 - acc: 0.8484     
Epoch 8/12
336876/336876 [==============================] - 6s - loss: 2.4441 - acc: 0.8484     
Epoch 9/12
336876/336876 [==============================] - 6s - loss: 2.4441 - acc: 0.8484    

当我在此之后测试模型时,我的f1_score为0。我的主要思想是我可能需要更多数据,但我仍然希望它的性能比现在测试集更好。可能是过度拟合吗?我添加了辍学,但那里也没有运气。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

乍看之下,我相信您的学习率太高了。另外,请考虑规范化数据,尤其是在不同功能具有不同值范围的情况下(请查看Scaling)。另外,请考虑根据标签是否为多类来更改图层激活。假设您的代码是这种形式的(问题描述中似乎有一些错别字):

# define the architecture of the network
model = Sequential()
#also what is the init="uniform" argument? I did not find this in keras documentation, consider removing this.
model.add(Dense(50, input_dim=X_train.shape[1], init="uniform", 
activation="relu"))
model.add(Dense(1, activation = 'sigmoid')))
#a slightly more conservative learning rate, play around with this.
adam = Adam(lr=0.0001)
model.compile(loss="binary_crossentropy", optimizer=adam,
metrics=['accuracy'])
model.fit(np.array(X_train), np.array(Y_train), epochs=12, batch_size=128,  
verbose=1)

这应该导致损失收敛。如果没有,请考虑加深神经网络(考虑您可能需要多少个参数)。

答案 1 :(得分:0)

在编译模型之前,请考虑添加分类层。

model.add(Dense(1, activation = 'sigmoid'))
adam = Adam(lr=0.01)
model.compile(loss="binary_crossentropy", optimizer=adam,
    metrics=['accuracy'])
model.fit(np.array(X_train), np.array(Y_train), epochs=12, batch_size=128, verbose=1)