【问题标题】:Keras .predict with word embeddings back to stringKeras .p​​redict 将单词嵌入返回到字符串
【发布时间】:2017-04-18 23:41:50
【问题描述】:

我正在学习这里的教程:https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html,使用不同的数据集。我正在尝试预测新随机字符串的标签。

我正在做一些不同的标签:

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]

【问题讨论】:

    标签: tensorflow keras


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 2017-10-31
      • 2014-03-29
      • 2020-03-04
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      相关资源
      最近更新 更多