简体   繁体   English

精度很低的cnn

[英]Very low accuracy cnn

I'm trying to use CNNs for text classification.我正在尝试使用 CNN 进行文本分类。 Model uses char-level word embedding + word embedding and then CNN layer is used to get features extracted followed by Dense layers and softmax activation for classification. Model 使用 char 级词嵌入 + 词嵌入,然后使用 CNN 层提取特征,然后使用 Dense 层和softmax激活进行分类。 My model uses categorical_crossentropy for loss function.我的 model 使用categorical_crossentropy损失 function。

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'])

After 10 epochs accuracy and loss doesn't change anymore and accuracy is really low (about 16 percent)在 10 个 epoch 之后,准确度和损失不再改变,准确度非常低(大约 16%)

loss: 0.0162 - accuracy: 0.1983 - val_loss: 1.8428 - val_accuracy: 0.0814

I already checked my data.我已经检查了我的数据。 There is no Nan.没有南。 And the data is shuffled before training.并且数据在训练之前被洗牌。

I found the problem and now it's fixed.我发现了问题,现在已经解决了。 For anyone having the same problem keras metrics is case-sensitive.对于任何有同样问题的人 keras 指标是区分大小写的。 Replacing 'Accuracy' with 'accuracy' makes the model work fine.用“准确度”替换“准确度”使 model 工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM