【问题标题】:Tensorflow RNN tutorialTensorFlow RNN 教程
【发布时间】:2025-01-13 10:10:02
【问题描述】:

我关注RNN tutorial of Tensorflow。 我无法理解以下脚本中reader.py 中的function ptb_producer

    with tf.control_dependencies([assertion]):
      epoch_size = tf.identity(epoch_size, name="epoch_size")

    i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()
    x = tf.strided_slice(data, [0, i * num_steps],[batch_size, (i + 1) * num_steps])
    x.set_shape([batch_size, num_steps])
    y = tf.strided_slice(data, [0, i * num_steps + 1],[batch_size, (i + 1) * num_steps + 1])
    y.set_shape([batch_size, num_steps])
    return x, y

谁能解释tf.train.range_input_producer 在做什么?

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    几周以来,我一直在努力理解同一个教程。在我看来,造成如此困难的原因在于,从 TensorFlow 调用的所有函数都不会立即执行,而是将它们对应的操作节点添加到图中。

    根据official documentation,范围输入生产者“在队列中生成从0limit - 1 的整数”。所以,在我看来,有问题的代码i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() 创建了一个节点,它充当计数器,一旦执行,就会产生序列0:(epoch_size) 中的下一个数字。

    这用于从输入数据中获取下一批。原始数据被分成batch_size 行,因此在每次运行中batch_size 批次都提供给训练函数。在每个批次(行)中,大小为num_steps 的滑动窗口向前移动。计数器i 允许窗口在每次调用中向前移动num_steps

    xy 的形状都是 [batch_size, num_steps],因为它们每个都包含 batch_size 批次的 num_steps 步骤。变量x 是输入,y 是给定输入的预期输出(它是通过将窗口向左移动一项来产生的,因此iff x = data[i:(i + num_steps] then y = data[(i + 1):(i + num_steps + 1)]

    这对我来说是一场噩梦,但我希望这篇文章对未来的人们有所帮助。

    【讨论】: