"de-embed" 个单词在 TensorFlow 中如何
How do "de-embed" words in TensorFlow
我正在尝试按照 Language Modeling on the TensorFlow site. I see it runs and the cost goes down and it is working great, but I do not see any way to actually get the predictions from the model. I tried following the instructions at 的教程进行操作,但是从 session.run 返回的张量是浮点值,例如 0.017842259,并且字典将单词映射到整数,因此不起作用。
如何从张量流模型中获取预测词?
编辑:我在四处搜索后发现了这个 explanation,我只是不确定在此示例的上下文中 x 和 y 是什么。他们在这个例子中似乎没有使用与解释中相同的约定。
您提到的张量是 loss
,它定义了网络的训练方式。对于预测,您需要访问包含下一个单词概率的张量 probabilities
。如果这是分类问题,您只需执行 argmax
即可获得最高概率。但是,为了也给较低概率的单词一个生成的机会,通常使用某种采样。
编辑:我假设您使用的代码是 this。在这种情况下,如果您查看第 148 行 (logits
),只需对其应用 softmax
函数即可将其转换为概率——如 tensorflow 网站的伪代码所示。希望这有帮助。
所以在浏览了一堆其他类似的帖子后,我明白了这一点。首先,文档中解释的代码与 GitHub 存储库中的代码不同。当前代码的工作原理是使用内部数据初始化模型,而不是在模型运行时将数据传递给模型。
所以基本上为了完成我想做的事情,我恢复了我的代码以提交 9274f5a (also do the same for reader.py). Then I followed the steps taken in to get the probabilities
tensor in my run_epoch
function. Additionally, I followed 以将 vocabulary
传递给我的 main
函数。从那里,我使用 vocabulary = {v: k for k, v in vocabulary.items()}
反转字典并将其传递给 run_epoch
.
最后,我们可以通过运行 current_word = vocabulary[np.argmax(prob, 1)]
得到run_epoch
中的预测词,其中prob
是session.run()
[=22=返回的tensor ]
编辑:这样恢复代码不应该是一个永久的解决方案,我绝对建议使用上面的@Prophecies 答案来获得 probabilities
张量。但是,如果要获取单词映射,则需要像我这里那样传递词汇表。
我正在尝试按照 Language Modeling on the TensorFlow site. I see it runs and the cost goes down and it is working great, but I do not see any way to actually get the predictions from the model. I tried following the instructions at
如何从张量流模型中获取预测词?
编辑:我在四处搜索后发现了这个 explanation,我只是不确定在此示例的上下文中 x 和 y 是什么。他们在这个例子中似乎没有使用与解释中相同的约定。
您提到的张量是 loss
,它定义了网络的训练方式。对于预测,您需要访问包含下一个单词概率的张量 probabilities
。如果这是分类问题,您只需执行 argmax
即可获得最高概率。但是,为了也给较低概率的单词一个生成的机会,通常使用某种采样。
编辑:我假设您使用的代码是 this。在这种情况下,如果您查看第 148 行 (logits
),只需对其应用 softmax
函数即可将其转换为概率——如 tensorflow 网站的伪代码所示。希望这有帮助。
所以在浏览了一堆其他类似的帖子后,我明白了这一点。首先,文档中解释的代码与 GitHub 存储库中的代码不同。当前代码的工作原理是使用内部数据初始化模型,而不是在模型运行时将数据传递给模型。
所以基本上为了完成我想做的事情,我恢复了我的代码以提交 9274f5a (also do the same for reader.py). Then I followed the steps taken in probabilities
tensor in my run_epoch
function. Additionally, I followed vocabulary
传递给我的 main
函数。从那里,我使用 vocabulary = {v: k for k, v in vocabulary.items()}
反转字典并将其传递给 run_epoch
.
最后,我们可以通过运行 current_word = vocabulary[np.argmax(prob, 1)]
得到run_epoch
中的预测词,其中prob
是session.run()
[=22=返回的tensor ]
编辑:这样恢复代码不应该是一个永久的解决方案,我绝对建议使用上面的@Prophecies 答案来获得 probabilities
张量。但是,如果要获取单词映射,则需要像我这里那样传递词汇表。