Which of 3 classes does the model.predict() output corresponds to?

Viewed 279

When i load a trained model and feed it with new data to predict (model.predict(textstr)) i get:

[[0.3345264  0.33339804 0.33207548]]

The model was trained with a dataframe that looks like:

                text  sent
1     Textstring1...     1
2     Textstring2...     2
3     Textstring3...     0
4     Textstring4...     0
5     Textstring5...     2

How can i tell which class (sent, train value) corresponds to the output i get? Does the 0.3345264 value above corresponds to sent 0 as an answer from the model?

Here are some details of the model and its configuration:

 model = tf.keras.Sequential([
            tf.keras.layers.Embedding(VOC, EMB_SIZE),
            tf.keras.layers.GlobalAveragePooling1D(),
            tf.keras.layers.Dense(node1,activation='relu'),
            tf.keras.layers.Dropout(dropout),
            tf.keras.layers.Dense(3, activation='softmax')])

 model.compile(optimizer='adadelta',
              loss='sparse_categorical_crossentropy',
              metrics=['sparse_categorical_accuracy'])

Thanks in advance.

Update Edit2: I used tokenizer like so to create train_seqs:

      tokenizer = tf.keras.preprocessing.text.Tokenizer(
            num_words=WORDS
          , oov_token='<UNK>')
        tokenizer.fit_on_texts(train_df['text'])


    #convert text data to numerical indexes
        train_seqs=tokenizer.texts_to_sequences(train_df['text'])
        test_seqs=tokenizer.texts_to_sequences(test_df['text'])

#pad data up to SEQ_LEN (note that we truncate if there are more than SEQ_LEN tokens)
    train_seqs=tf.keras.preprocessing.sequence.pad_sequences(
       train_seqs
       , maxlen=SEQ_LEN
       , padding="post")
    test_seqs=tf.keras.preprocessing.sequence.pad_sequences(
       test_seqs
       , maxlen=SEQ_LEN
       , padding="post")

train_seqs
Out[12]: 
array([[ 144,    8,   46, ...,   42,    3, 1734],
       [   6,  315,  277, ...,   44, 2247, 2095],
       [   5,   18,  162, ...,  159,   56, 1483],
       ...,
       [   9,  132,   76, ...,  194,  234, 1628],
       [ 660,   66,    7, ...,    0,    0,    0],
       [ 514,  879,  126, ...,    6,   68,  590]], dtype=int32)

train_df['sent'].values
Out[13]: array([1, 0, 2, ..., 0, 1, 0])

history = model.fit(train_seqs, train_df['sent'].values
                    , batch_size=BATCH_SIZE
                    , epochs=EPOCHS
                    , validation_split=0.2
                    , callbacks=callbacks)
1 Answers

If I understand it correctly, you want to predict the sent column, which seems to be a categorical variable whose values may be 0, 1 or 2. Your final layer is of 3 neurons (with a softmax activation), so you will have 3 outputs per inference. I assume you used something like One Hot Encoding for the ground truth outputs. The order of the predictions will be the same as the one set by your One Hot Encoder.

How did you prepare your dataset ground truth outputs?

UPDATE:

It is actually a nice question as there not much documentation about it. I've searched and I found a similar question on SO, so I asked for more details here:

In case of using sparse_categorical_crossentropy, the category you have assigned to number 0 is actually class 0; the category you have assigned to number 1 is actually class 1; and so on. For example, if you use LabelEncoder from sklearn, you can find out this mapping via .classes_ attribute (see the documentation and examples).

Hope this helps!

Related