我已经为二进制文本分类构建了CNN模型,我的模型如下。作为参考,embedding_matrix包含预训练的词向量权重。
model = Sequential()
model.add(Embedding(num_words, EMBEDDING_DIM, weights =
[embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable = False)) #numbers match up to tokenizer
model.add(Dropout(0.3))
model.add(Conv1D(64, 5, activation='relu'))
model.add(MaxPooling1D(pool_size=4))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))
编译并评估模型。
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer="adam", metrics=['acc'])
model.fit(data, np.array(HIT), validation_split = VALIDATION_SPLIT, epochs = 3, batch_size=20, verbose = 2)
score = model.evaluate(data2, np.array(HIT2), verbose = 2)
print("Test Loss = ", score[0])
print("Test Accuracy = ", score[1])
在各个时期都没有发生变化。模型摘要和培训日志发布在下面。
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 300, 100) 1813200
_________________________________________________________________
dropout_1 (Dropout) (None, 300, 100) 0
_________________________________________________________________
conv1d_1 (Conv1D) (None, 296, 64) 32064
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 74, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 4736) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 4737
=================================================================
Total params: 1,850,001
Trainable params: 36,801
Non-trainable params: 1,813,200
_________________________________________________________________
Train on 3919 samples, validate on 560 samples
Epoch 1/5
- 4s - loss: 9.5353 - acc: 0.4019 - val_loss: 10.1633 - val_acc: 0.3625
Epoch 2/5
- 3s - loss: 9.5353 - acc: 0.4019 - val_loss: 10.1633 - val_acc: 0.3625
Epoch 3/5
- 3s - loss: 9.5353 - acc: 0.4019 - val_loss: 10.1633 - val_acc: 0.3625
Epoch 4/5
- 3s - loss: 9.5353 - acc: 0.4019 - val_loss: 10.1633 - val_acc: 0.3625
Epoch 5/5
- 3s - loss: 9.5353 - acc: 0.4019 - val_loss: 10.1633 - val_acc: 0.3625
我尝试使用不同的优化器,更改批处理大小,并更改CNN超参数,尽管在各个时期损耗,acc,val_loss和val_acc仍然保持不变。但是,当在最后一层使用S型函数时,跨历元的准确性和损失都会提高。