我正在尝试使用CNN进行文本分类。模型采用字符级字嵌入+词嵌入,然后利用CNN层提取特征,然后进行密集层和softmax激活进行分类。我的模型使用categorical_crossentropy作为损失函数。
cnns = [
[64, 3, 2],
[128, 3, -1],
[256, 5, 3],
[256, 5, -1],
[512, 5, 3],
]
nb_classes = 2
input_word = Input(shape = (default_max_len_words,), name='input_word')
input_chw = Input(shape = (default_max_len_words, default_max_len_subwords), name='input_chw')
embedding_word = Embedding(input_dim=size_of_word_vocab, output_dim=default_emb_dim, input_length=default_max_len_words, name='word_emb') (input_word)
embedding_chw = Embedding(input_dim=size_of_char_vocab, output_dim=default_emb_dim, input_length=default_max_len_subwords, name='chw_emb') (input_chw)
reduced = tf.keras.layers.Lambda(lambda x: tf.reduce_sum(x, axis=2), name='reduction')(embedding_chw)
x = Add(name='adding')([embedding_word, reduced])
for f, ks, ps in cnns:
x = Conv1D(filters=f, kernel_size=ks, padding='valid', activation='relu') (x)
x = BatchNormalization() (x)
if ps != -1:
x = MaxPooling1D(pool_size=ps) (x)
x = Flatten() (x)
x = Dense(256, activation='relu') (x)
x = Dense(128, activation='relu') (x)
x = Dense(2, activation='softmax') (x)
model = keras.Model(inputs=[input_word, input_chw], outputs=x, name='temp')
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['Accuracy'])经过10个年代,精度和损失不再改变,准确度很低(约16 %)
loss: 0.0162 - accuracy: 0.1983 - val_loss: 1.8428 - val_accuracy: 0.0814
我已经查过我的数据了。没有南。在训练前对数据进行整理。
发布于 2022-09-12 00:18:09
我发现了问题现在解决了。对于任何有相同问题的人,keras度量都是区分大小写的。用“精度”代替“精度”使模型工作得很好。
https://stackoverflow.com/questions/73680525
复制相似问题