Very low accuracy cnn

Viewed 40

I'm trying to use CNNs for text classification. 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. My model uses categorical_crossentropy for loss 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)

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.

1 Answers

I found the problem and now it's fixed. For anyone having the same problem keras metrics is case-sensitive. Replacing 'Accuracy' with 'accuracy' makes the model work fine.

Related