2017-04-18 46 views
1

我在這裏教程:https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html,使用不同的數據集。我試圖預測一個新的隨機字符串的標籤。Keras .p​​redict與字嵌入字符串

我做標註有一點不同:

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

然後試圖預測,如:(?也許這個詞的嵌入)

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

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

我得到類似下面的數組的數組。那些是什麼以及如何將它們翻譯回字符串?

[[ 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

保存標籤裝在編碼器:

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

預測會給你一個類載體。並且通過使用inverse_transform,您將從原始輸入中獲​​得標籤類型:

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