训练后Keras模型无法学习

时间:2020-08-03 04:46:52

标签: python-3.x tensorflow keras nlp

我正在训练一个用于句子分类任务的keras模型。问题是,尽管它给出了94%的准确度,但却没有学到任何东西。当我给出一个新句子(数据集中不存在)时,它给出的概率相同(在model.prediction步骤中)。我不知道为什么会这样。

这是我的模特

model = Sequential()
model.add(Embedding(max_words, 30, input_length=max_len))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Bidirectional(LSTM(32)))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='sigmoid'))
model.summary()

这里max_words = 2000max_len=300

这是模型摘要

Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_3 (Embedding)      (None, 300, 30)           60000     
_________________________________________________________________
batch_normalization_5 (Batch (None, 300, 30)           120       
_________________________________________________________________
activation_5 (Activation)    (None, 300, 30)           0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 300, 30)           0         
_________________________________________________________________
bidirectional_3 (Bidirection (None, 64)                16128     
_________________________________________________________________
batch_normalization_6 (Batch (None, 64)                256       
_________________________________________________________________
activation_6 (Activation)    (None, 64)                0         
_________________________________________________________________
dropout_4 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_3 (Dense)              (None, 2)                 130       
=================================================================
Total params: 76,634
Trainable params: 76,446
Non-trainable params: 188

这是代码,我的数据集大小为20k,正在测试中的数据为10%。

model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'], optimizer = 'adam')
history = model.fit(sequences_matrix, Y_train, batch_size=256, epochs=50, validation_split=0.1)

1 个答案:

答案 0 :(得分:1)

尝试将最后一层的激活功能从S形更改为softmax。它与您正在使用的损耗(分类交叉熵)不太匹配。如果使用S形,则只需要一个单位,并且应该使用二进制交叉熵损失。