keras 极低精度cnn

irtuqstp  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(145)

我尝试使用CNN来进行文本分类。模型使用字符级单词嵌入+单词嵌入,然后使用CNN层来提取特征,接着使用Dense层和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
我已经查过我的数据了。没有南。而且数据在训练前被打乱了。

nimxete2

nimxete21#

我发现了这个问题,现在它已经修复了。对于任何有同样问题的人来说,keras度量是区分大小写的。用“accuracy "替换”Accuracy"会使模型工作得很好。

相关问题