【问题标题】:Tensorflow RNN output tensor shapesTensorflow RNN 输出张量形状
【发布时间】:2017-08-08 10:53:27
【问题描述】:

我对 tensorflow 很陌生,我不太了解如何塑造张量,以便将输出作为单个数字。基本上,我的循环网络应该猜到下一个数字。相反,每次预测都会返回一个包含五个数字的列表?我猜我的一个或多个张量形状不正确。

我的输入数据被格式化为大约 2000 个列表,每个列表有 5 个特征,如下所示:

[
  np.array ([
              [1],[2],[3],[4],[5]
            ]) 
]

这是 RNN 的代码:

cell_units = 400
batch_size = 5
no_of_epochs = 500

data = tf.placeholder (tf.float32, [None, 5, 1])
target = tf.placeholder (tf.float32, [None, 1, 1])


weight = tf.Variable (tf.random_normal ([cell_units, 5, 1]))
bias = tf.Variable (tf.random_normal([1, 1]))


cell = tf.contrib.rnn.BasicRNNCell (num_units = cell_units)

output, states = tf.nn.dynamic_rnn (cell, data, dtype=tf.float32)


output = tf.transpose (output, [1, 0, 2])


activation = tf.matmul (output, weight) + bias


cost = tf.reduce_mean (
                        (
                            tf.log (tf.square (activation - target))
                        )
                      )

optimizer = tf.train.AdamOptimizer (learning_rate = 0.01).minimize(cost)

with tf.Session () as sess:

    sess.run (tf.global_variables_initializer ())
    no_of_batches = int (len (train_x) / batch_size)

    for i in range(no_of_epochs):
        start = 0
        for j in range(no_of_batches):
            inp = train_x [start:start+batch_size]
            out = train_y [start:start+batch_size]
            start += batch_size

            sess.run (optimizer, {data: inp, target: out})

【问题讨论】:

  • 最后一个单元格的输出output[-1],如果你只是在寻找最后一个输出

标签: python tensorflow neural-network shape recurrent-neural-network


【解决方案1】:

tf.nn.dynamic_rnn 需要形状为 [batch_size, max_time, ...] 的输入。在您的示例中,batch_size 是动态的(即未知),max_time5(即时间步数)。自然,RNN 的输出包含 5 条目,每个输入步骤一个:[None, 5, cell_units]

正如@Ishant Mrinal 建议的那样,您可以选择最后一个输出步骤。

weight = tf.Variable (tf.random_normal ([cell_units, 1]))
bias = tf.Variable (tf.random_normal([1, 1]))

cell = tf.contrib.rnn.BasicRNNCell (num_units = cell_units)
output, states = tf.nn.dynamic_rnn (cell, data, dtype=tf.float32)
# Get the last step (4th index).
output = tf.squeeze(tf.transpose (output, [0, 2, 1])[:,:,4]) # Shape of [batch_size, cell_units].
activation = tf.matmul (output, weight) + bias

activation 的形状为[batch_size, 1]

【讨论】:

  • 谢谢!这很有帮助!虽然当我运行此代码时,我得到一个新错误: InvalidArgumentError(参见上文的追溯):In[0] 不是矩阵 [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device= "/job:localhost/replica:0/task:0/cpu:0"](Squeeze, Variable/read)]]
  • 错误的来源是什么?哪条线?我创建了 dummy output 并测试了代码 sn-p。为我工作。
  • 这个:activation = tf.matmul (output, weight) + bias 在训练模型时效果很好,只有在尝试预测下一个数字时才会出现错误...
  • 你能分享代码sn-p吗? activation对应模型预测,即下一个数字。
  • with tf.Session () as sess: sess.run (tf.global_variables_initializer ()) no_of_batches = int (len (train_x) / batch_size) for i in range(no_of_epochs): start = 0 for j in range(no_of_batches): inp = train_x [start:start+batch_size] out = train_y [start:start+batch_size] start += batch_size sess.run(optimizer, {data: inp, target: out}) print sess.run (activation, {data: lol}) 坦克你的帮助! ('lol' 与 train_x 格式相同)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多