如何在张量流中循环穿越可变长度的占位符



假设我的输入张量如下:

x_inputs = tf.placeholder(tf.float32, shape=[1, None], name='x_inputs')

这是一个具有变量长度输入的占位符。

另外,我有一个嵌入矩阵:

embeddings = tf.Variable(
    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0),
    name='embeddings')

我想为输入序列的每个元素查找嵌入式,并将它们添加在一起以构建一个嵌入向量。类似于(python(的东西:

embed = tf.zeros([batch_size, embedding_size])
for element in range(num_inputs):
  embed += tf.nn.embedding_lookup(embeddings,
                                  tf.cast(x_inputs[:, element], tf.int32))

如果num_inputs已固定,一切正常。问题在于,在训练过程中更改了num_inputs的值,我需要在图表内进行计算。我不知道如何根据x_inputs中的输入值计算图表中的上述for loop

tf.nn.embeddings_lookup可以处理变量大小参数:

x_inputs = tf.placeholder(tf.int32, shape=[1, None], name='x_inputs')
embeddings = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
iterSession = tf.InteractiveSession()
embed = tf.squeeze(tf.nn.embeddings_lookup(embeddings, x_inputs))
iterSession.run(embed, feed_dict={x_inputs: [[0, 1, 0, 2]]})
# array([[1, 2, 3],
#       [4, 5, 6],
#       [1, 2, 3],
#       [7, 8, 9]], dtype=int32)
iterSession.run(tf.reduce_sum(embed, axis=0), feed_dict={x_inputs: [[0, 1, 0, 2]]})
# array([13, 17, 21], dtype=int32)

请注意, x_inputs是直接 tf.int32的类型,避免使用。


如果您希望在执行过程中长度保持动态,并且以嵌入方式为例,则可以使用tf.foldl

编写它。
embed = tf.foldl(lambda a, x: a + tf.nn.embedding_lookup(embeddings, x),
                 tf.reshape(x_inputs, (-1,)), 
                 initializer=[[0, 0, 0]])
iterSession.run(embed, feed_dict={x_inputs: [[0, 1, 0, 2]]})
# array([[13, 17, 21]], dtype=int32)

最新更新