Search code examples
pythonkerasnlp

Keras model - How to get accuracy - NLP Task


I have made a Keras model for an NLP Multi-class classification problem. The data consists of titles and tags. I have trained the model on the titles to predict tags. I had converted the tags into one-hot using sklearn.preprocessing LabelEncoder, OneHotEncoder.

OneHot Encoding

def onehot(df):
    values = array(df)
    # integer encode
    label_encoder = LabelEncoder()
    integer_encoded = label_encoder.fit_transform(values)
    # binary encode
    onehot_encoder = OneHotEncoder(sparse=False)
    integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
    onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
    return label_encoder, onehot_encoded

I have used categorical_crossentropy and adam for my model. Here is the code for the model.

Keras CNN Model

def ConvNet(embeddings, max_sequence_length, num_words, embedding_dim, labels_index):
embedding_layer = Embedding(num_words, embedding_dim, weights=[embeddings],
                           input_length = max_sequence_length,
                           trainable = False)
sequence_input = Input(shape = (max_sequence_length,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
convs = []
filter_sizes = [2,3,4,5,6]
for filter_size in filter_sizes:
    l_conv = Conv1D(filters=200, kernel_size=filter_size, activation='relu')(embedded_sequences)
    l_pool = GlobalMaxPooling1D()(l_conv)
    convs.append(l_pool)

l_merge = concatenate(convs, axis=1)  
x = Dropout(0.1)(l_merge)  
x = Dense(128, activation='relu')(x)
x = Dropout(0.2)(x)

preds = Dense(labels_index, activation='sigmoid')(x)
model = Model(sequence_input, preds)    
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['acc'])    
model.summary()    
return model

Prediction

predictions = model.predict(test_cnn_data, batch_size=1024, verbose=1)  

I get an array like this from predictions

print(predictions[5, :])
Output:
array([8.8067267e-08, 5.1040554e-15, 1.9745098e-16, ..., 8.0959568e-17,
       2.1070798e-17, 1.1202571e-18], dtype=float32)

What I understand is that these are probabilities or confidence score that the following sentence belongs to this tag.

How do I convert the predicted arrays into tags so I can compare it with the test dataset tags to accuracy?


Solution

  • use np.argmax(predictions[5, :]) to get the index of the encoded tag that the model suggested to belong to the text.

    I think that label_encoder consists the tags them selfs inside

    so you could do something like that-

    print(label_encoder.inverse_transform(predictions[5, :]))
    

    add the inverse_transform to get the classes themselfs.


    use the label_encoder of train