TensorFlow dynamic_rnn回归值:ValueError维度不匹配 [英] TensorFlow dynamic_rnn regressor: ValueError dimension mismatch

查看:268
本文介绍了TensorFlow dynamic_rnn回归值:ValueError维度不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个玩具LSTM模型进行回归. 这个不错的教程已经太复杂了对于初学者.

I would like to build a toy LSTM model for regression. This nice tutorial is already too complicated for a beginner.

给出长度为time_steps的序列,预测下一个值.考虑time_steps=3及其序列:

Given a sequence of length time_steps, predict the next value. Consider time_steps=3 and the sequences:

array([
   [[  1.],
    [  2.],
    [  3.]],

   [[  2.],
    [  3.],
    [  4.]],
    ...

目标值应为:

array([  4.,   5., ...

我定义以下模型:

# Network Parameters
time_steps = 3 
num_neurons= 64 #(arbitrary)
n_features = 1

# tf Graph input
x = tf.placeholder("float", [None, time_steps, n_features])
y = tf.placeholder("float", [None, 1])

# Define weights
weights = {
   'out': tf.Variable(tf.random_normal([n_hidden, 1]))
} 
biases = {
   'out': tf.Variable(tf.random_normal([1]))
}

#LSTM model
def lstm_model(X, weights, biases, learning_rate=0.01, optimizer='Adagrad'):

  # Prepare data shape to match `rnn` function requirements
  # Current data input shape: (batch_size, time_steps, n_features)
  # Required shape: 'time_steps' tensors list of shape (batch_size, n_features)
  # Permuting batch_size and time_steps
  input dimension: Tensor("Placeholder_:0", shape=(?, 3, 1), dtype=float32)

  X = tf.transpose(X, [1, 0, 2])
  transposed dimension: Tensor("transpose_41:0", shape=(3, ?, 1), dtype=float32)

  # Reshaping to (time_steps*batch_size, n_features)
  X = tf.reshape(X, [-1, n_features])
  reshaped dimension: Tensor("Reshape_:0", shape=(?, 1), dtype=float32)

  # Split to get a list of 'time_steps' tensors of shape (batch_size, n_features)
  X = tf.split(0, time_steps, X)
  splitted dimension: [<tf.Tensor 'split_:0' shape=(?, 1) dtype=float32>, <tf.Tensor 'split_:1' shape=(?, 1) dtype=float32>, <tf.Tensor 'split_:2' shape=(?, 1) dtype=float32>]

  # LSTM cell
  cell = tf.nn.rnn_cell.LSTMCell(num_neurons) #Or GRUCell(num_neurons)

  output, state = tf.nn.dynamic_rnn(cell=cell, inputs=X, dtype=tf.float32)

  output = tf.transpose(output, [1, 0, 2])
  last = tf.gather(output, int(output.get_shape()[0]) - 1)


  return tf.matmul(last, weights['out']) + biases['out']

我们用pred = lstm_model(x, weights, biases)实例化LSTM模型,我得到以下信息:

We instantiating the LSTM model with pred = lstm_model(x, weights, biases) I get the following:

---> output, state = tf.nn.dynamic_rnn(cell=cell, inputs=X, dtype=tf.float32)
ValueError: Dimension must be 2 but is 3 for 'transpose_42' (op: 'Transpose') with input shapes: [?,1], [3]

1)您知道问题出在哪里吗?

1) Do you know what the problem is?

2)将LSTM输出乘以权重是否会得出回归?

2) Will multiplying the LSTM output by the weights yield the regression?

推荐答案

如注释中所述, tf.nn.dynamic_rnn(cell, inputs, ...) 函数需要一个三维张量列表 * 作为其inputs参数,其中尺寸默认情况下解释为batch_size x x num_features. (如果传递time_major=True,它们将被解释为num_timesteps x batch_size x num_features.)因此,不需要在原始占位符中进行的预处理,您可以直接传递oring X值.到tf.nn.dynamic_rnn().

As discussed in the comments, the tf.nn.dynamic_rnn(cell, inputs, ...) function expects a list of three-dimensional tensors* as its inputs argument, where the dimensions are interpreted by default as batch_size x num_timesteps x num_features. (If you pass time_major=True, they are interpreted as num_timesteps x batch_size x num_features.) Therefore the preprocessing you've done in the original placeholder is unnecessary, and you can pass the oriding X value directly to tf.nn.dynamic_rnn().

* 从技术上讲,它除了可以接受列表之外还可以接受复杂的嵌套结构,但是叶子元素必须是三维张量. **

* Technically it can accept complicated nested structures in addition to lists, but the leaf elements must be three-dimensional tensors.**

** 调查此问题发现了tf.nn.dynamic_rnn()实现中的错误.原则上,输入至少具有两个维度就足够了,但是time_major=False路径假定将输入转置为主要时间形式时它们恰好具有三个维度,并且这是错误消息错误会导致程序中出现错误.我们正在努力修复该问题.

** Investigating this turned up a bug in the implementation of tf.nn.dynamic_rnn(). In principle, it should be sufficient for the inputs to have at least two dimensions, but the time_major=False path assumes that they have exactly three dimensions when it transposes the input into the time-major form, and it was the error message that this bug inadvertently causes that showed up in your program. We're working on getting that fixed.

这篇关于TensorFlow dynamic_rnn回归值:ValueError维度不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆