Keras .predict with word embeddings back to string

2020-07-31 04:44发布

问题:

I'm following the tutorial here: https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html, using a different data set. I'm trying to predict the label for a new random string.

I'm doing labelling a bit different:

encoder = LabelEncoder()
encoder.fit(labels)
encoded_Y = encoder.transform(labels)
dummy_y = np_utils.to_categorical(encoded_Y)

And then trying to predict like:

string = "I am a cat"
query = tokenizer.texts_to_sequences(string)
query = pad_sequences(query, maxlen=50)

prediction = model.predict(query)
print(prediction)

I get back an array of arrays like below (perhaps the word embeddings?). What are those and how can I translate them back to a string?

[[ 0.03039312  0.02099193  0.02320454  0.02183384  0.01965107  0.01830118
   0.0170384   0.01979697  0.01764384  0.02244077  0.0162186   0.02672437
   0.02190582  0.01630476  0.01388928  0.01655456  0.011678    0.02256939
   0.02161663  0.01649982  0.02086013  0.0161493   0.01821378  0.01440909
   0.01879989  0.01217389  0.02032642  0.01405699  0.01393504  0.01957162
   0.01818203  0.01698637  0.02639499  0.02102267  0.01956343  0.01588933
   0.01635705  0.01391534  0.01587612  0.01677094  0.01908684  0.02032183
   0.01798265  0.02017053  0.01600159  0.01576616  0.01373934  0.01596323
   0.01386674  0.01532488  0.01638312  0.0172212   0.01432543  0.01893282
   0.02020231]

回答1:

Save the fitted labels in the encoder:

    encoder = LabelEncoder() 
    encoder = encoder.fit(labels)
    encoded_Y = encoder.transform(labels)
    dummy_y = np_utils.to_categorical(encoded_Y)

Prediction will give you a class vector. And by using the inverse_transform you will get the label type as from your original input:

    prediction = model.predict_classes(query)
    label = encoder.inverse_transform(prediction)