0

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?

4
  • How many classes your classifier was trained on? Mar 27, 2020 at 17:45
  • @YoelNisanov I have about ~5000 unique tags Mar 27, 2020 at 17:46
  • and what is the length of predictions[5, :]? Mar 27, 2020 at 17:46
  • @YoelNisanov 5015 Mar 27, 2020 at 17:49

1 Answer 1

1

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

4
  • I have tried that I get errors like these ValueError: y contains previously unseen labels: [4068]. but Not on ever prediction. Mar 27, 2020 at 17:52
  • Do I use the inverse transform of the training data? Mar 27, 2020 at 17:54
  • Thank you for answering I found my mistake. I needed to use the label_encoder of the training data in order to get the prediction. Mar 27, 2020 at 17:56
  • np, happy I could help! (: Mar 27, 2020 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.