【问题标题】:How to use word embeddings for prediction in Tensorflow如何在 Tensorflow 中使用词嵌入进行预测
【发布时间】:2016-05-15 23:21:22
【问题描述】:

我正在尝试学习 Tensorflow 教程,但在尝试增强 RNN/language model tutorial 以便预测句子中的下一个单词时遇到了困难。本教程使用词嵌入作为词的表示。

由于模型学习词嵌入,我假设我添加的任何类型的预测都会输出相同的嵌入。我不知道如何从这些嵌入转换回数据集中的单词 id。我见过的唯一示例保留了一个内存数据结构,它与 wordid -> 嵌入的映射相反,并将其用于查找。这显然不适用于所有问题。有没有更好的办法?

【问题讨论】:

    标签: tensorflow word-embedding


    【解决方案1】:

    假设您在词汇表中同时拥有 word_to_idxidx_to_word,这就是您所做工作的伪代码

    假设预测的输入是“这是样本”

    batch_size = 1
    num_steps = 3 # i.e each step for the word in "this is sample"
    hidden_size = 1500
    vocab_size = 10000
    translate the `word_to_idx` for the input `"this is sample"`
    get the word embeddings for the each word in the input
    Input to model will be word embedding of size 1x1500 at each time step
    Output of model at each time step will be of size 1x1500
    y_pred is output at last step from model for the given input 
    adding projection to output (i.e y_pred x Weights(hidden_size, vocab_size) + bias(vocab_size, )  = 1x10000)
    now sample the output through the below function to get the index with max probability
    generate idx_to_word from the index we just got
    use the generated word along with the previous input to generate next word until you get `<eos>` or some predefined sentence stopping.
    

    这是一个从here 采样的示例:

    def sample(a, temperature=1.0):
        # helper function to sample an index from a probability array
        a = np.log(a) / temperature
        a = np.exp(a) / np.sum(np.exp(a))
        return np.argmax(np.random.multinomial(1, a, 1))
    

    【讨论】:

    • 感谢您的回答!我想我明白了,但要花一两天时间来尝试它。可以试一试后标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多