如何使用 tensorflow 的数据集 API 迭代器作为(递归)神经网络的输入?

How to use tensorflow's Dataset API Iterator as an input of a (recurrent) neural network?

当使用张量流的数据集 API 迭代器时,我的目标是定义一个 RNN,它在迭代器的 get_next() 张量上运行作为其输入(参见代码中的 (1))。

但是,简单地将 dynamic_rnn 定义为 get_next() 作为其输入会导致错误:ValueError: Initializer for variable rnn/basic_lstm_cell/kernel/ is from inside a control-flow construct, such as a loop or conditional. When creating a variable inside a loop or conditional, use a lambda as the initializer.

现在我知道一个解决方法是简单地为 next_batch 创建一个占位符,然后 eval() 张量(因为你不能传递张量本身)并使用 [=19 传递它=](请参阅代码中的 X(2))。 但是,如果我理解正确的话,这不是一个有效的解决方案,因为我们首先评估然后重新初始化张量。

有没有办法:

  1. 直接在迭代器的输出之上定义dynamic_rnn

或:

  1. 以某种方式直接将现有的 get_next() 张量传递给作为 dynamic_rnn?
  2. 输入的占位符

完整的工作示例; (1) 版本是我想要的,但它没有,而 (2) 是可行的解决方法。

import tensorflow as tf

from tensorflow.contrib.rnn import BasicLSTMCell
from tensorflow.python.data import Iterator

data = [ [[1], [2], [3]], [[4], [5], [6]], [[1], [2], [3]] ]
dataset = tf.data.Dataset.from_tensor_slices(data)
dataset = dataset.batch(2)
iterator = Iterator.from_structure(dataset.output_types,
                                   dataset.output_shapes)
next_batch = iterator.get_next()
iterator_init = iterator.make_initializer(dataset)

# (2):
X = tf.placeholder(tf.float32, shape=(None, 3, 1))

cell = BasicLSTMCell(num_units=8)

# (1):
# outputs, states = lstm_outputs, lstm_states = tf.nn.dynamic_rnn(cell, next_batch, dtype=tf.float32)

# (2):
outputs, states = lstm_outputs, lstm_states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    sess.run(iterator_init)

    # (1):
    # o, s = sess.run([outputs, states])
    # o, s = sess.run([outputs, states])

    # (2):
    o, s = sess.run([outputs, states], feed_dict={X: next_batch.eval()})
    o, s = sess.run([outputs, states], feed_dict={X: next_batch.eval()})

(使用tensorflow 1.4.0,Python 3.6。)

非常感谢:)

原来神秘的错误很可能是 tensorflow 中的错误,请参阅 https://github.com/tensorflow/tensorflow/issues/14729。更具体地说,错误实际上来自输入错误的数据类型(在我上面的示例中,data 数组包含 int32 值,但它应该包含浮点数)。

而不是得到 ValueError: Initializer for variable rnn/basic_lstm_cell/kernel/ is from inside a control-flow construct 错误,
张量流应该 return:
TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [int32, float32] that don't all match.(参见 1)。

要解决这个问题,只需更改
data = [ [[1], [2], [3]], [[4], [5], [6]], [[1], [2], [3]] ]

data = np.array([[ [1], [2], [3]], [[4], [5], [6]], [[1], [2], [3]] ], dtype=np.float32)

然后下面的代码应该可以正常工作:

import tensorflow as tf
import numpy as np

from tensorflow.contrib.rnn import BasicLSTMCell
from tensorflow.python.data import Iterator

data = np.array([[ [1], [2], [3]], [[4], [5], [6]], [[1], [2], [3]] ], dtype=np.float32)
dataset = tf.data.Dataset.from_tensor_slices(data)
dataset = dataset.batch(2)
iterator = Iterator.from_structure(dataset.output_types,
                                   dataset.output_shapes)
next_batch = iterator.get_next()
iterator_init = iterator.make_initializer(dataset)

# (2):
# X = tf.placeholder(tf.float32, shape=(None, 3, 1))

cell = BasicLSTMCell(num_units=8)

# (1):
outputs, states = lstm_outputs, lstm_states = tf.nn.dynamic_rnn(cell, next_batch, dtype=tf.float32)

# (2):
# outputs, states = lstm_outputs, lstm_states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    sess.run(iterator_init)

    # (1):
    o, s = sess.run([outputs, states])
    o, s = sess.run([outputs, states])

    # (2):
    # o, s = sess.run([outputs, states], feed_dict={X: next_batch.eval()})
    # o, s = sess.run([outputs, states], feed_dict={X: next_batch.eval()})