TensorFlow 的 tf.nn.dynamic_rnn 运算符的输入张量是如何构造的? [英] How is the input tensor for TensorFlow's tf.nn.dynamic_rnn operator structured?
问题描述
我正在尝试使用 tf.nn.dynamic_rnn
图操作在 TensorFlow 0.9.0 中使用词嵌入和递归神经网络编写语言模型,但我不明白 input
张量是结构化的.
I am trying to write a language model using word embeddings and recursive neural networks in TensorFlow 0.9.0 using the tf.nn.dynamic_rnn
graph operation, but I don't understand how the input
tensor is structured.
假设我有一个 n 个词的语料库.我将每个单词嵌入一个长度为 e 的向量中,并且我希望我的 RNN 展开到 t 个时间步长.假设我使用默认的 time_major = False
参数,我的 input
张量 [batch_size, max_time, input_size]
会有什么形状?
Let's say I have a corpus of n words. I embed each word in a vector of length e, and I want my RNN to unroll to t time steps. Assuming I use the default time_major = False
parameter, what shape would my input
tensor [batch_size, max_time, input_size]
have?
也许一个特定的小例子会使这个问题更清楚.假设我有一个由 n=8 个单词组成的语料库,看起来像这样.
Maybe a specific tiny example will make this question clearer. Say I have a corpus consisting of n=8 words that looks like this.
1, 2, 3, 3, 2, 1, 1, 2
假设我将其嵌入大小为 e=3 的向量中,嵌入量为 1 -> [10, 10, 10], 2 -> [20, 20, 20] 和 3 -> [30, 30, 30],我的 input
张量会是什么样子?
Say I embed it in a vector of size e=3 with the embeddings 1 -> [10, 10, 10], 2 -> [20, 20, 20], and 3 -> [30, 30, 30], what would my input
tensor look like?
我已经阅读了 TensorFlow循环神经网络教程,但不使用tf.nn.dynamic_rnn
.我还阅读了 tf.nn.dynamic_rnn
的文档,但发现它令人困惑.特别是我不确定这里的max_time"和input_size"是什么意思.
I've read the TensorFlow Recurrent Neural Network tutorial, but that doesn't use tf.nn.dynamic_rnn
. I've also read the documentation for tf.nn.dynamic_rnn
, but find it confusing. In particular I'm not sure what "max_time" and "input_size" mean here.
谁能用n、t和e给出input
张量的形状,和/或使用我描述的小型语料库中的数据初始化张量的示例?
Can anyone give the shape of the input
tensor in terms of n, t, and e, and/or an example of what that tensor would look like initialized with data from the small corpus I describe?
TensorFlow 0.9.0、Python 3.5.1、OS X 10.11.5
推荐答案
在您的情况下,它看起来像 batch_size = 1
,因为您正在查看一个示例.所以 max_time
是 n=8
而 input_size
是输入深度,在你的例子中是 e=3
.所以你想构造一个 input
张量,它的形状是 [1, 8, 3]
.它是batch_major,所以第一个维度(批量维度)是1
.例如,如果您同时有另一个输入,带有 n=6
个单词,那么您可以通过将第二个示例填充为 8
个单词(通过填充最后 2 个词嵌入为零),您的 inputs
大小为 [2, 8, 3]
.
In your case, it looks like batch_size = 1
, since you're looking at a single example. So max_time
is n=8
and input_size
is the input depth, in your case e=3
. So you would want to construct an input
tensor which is shaped [1, 8, 3]
. It's batch_major, so the first dimension (the batch dimension) is 1
. If, say, you had another input at the same time, with n=6
words, then you would combine the two by padding this second example to 8
words (by padding zeros for the last 2 word embeddings) and you would have an inputs
size of [2, 8, 3]
.
这篇关于TensorFlow 的 tf.nn.dynamic_rnn 运算符的输入张量是如何构造的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!