Keras .p​​redict将单词嵌入返回字符串 [英] Keras .predict with word embeddings back to string

查看:59
本文介绍了Keras .p​​redict将单词嵌入返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里关注该教程:

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)

然后尝试预测:

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

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

我得到如下数组的数组(也许是词embeddings?).这些是什么,如何将它们转换回字符串?

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]

推荐答案

将适合的标签保存在编码器中:

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)

预测将为您提供分类向量.通过使用inverse_transform,您将从原始输入中获​​取标签类型:

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)

这篇关于Keras .p​​redict将单词嵌入返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆