【问题标题】:Input Dimensions Tensorflow v1.8 ConvLSTMCell输入尺寸 Tensorflow v1.8 ConvLSTMCell
【发布时间】:2018-10-24 15:50:15
【问题描述】:

ConvLSTMCell Official Docs

GitHub _conv where the error occurs

问题

我正在试验tensorflow r1.8 中的ConvLSTMCell。我继续生成的错误发生在ConvLSTMCell__call__ 方法中。调用_conv 方法并引发错误。

ValueError: Conv Linear Expects 3D, 4D, 5D

错误是从unstacked 输入引发的。 unstacked(在此示例中)的维度为 [BATCH_SIZE, N_INPUTS] = [2,5]。我正在使用tf.unstack 生成ConvLSTMCell 需要的所需序列。

为什么要使用tf.unstack

如果输入数组没有被unstacked,下面的TypeError被提升。

TypeError: inputs must be a sequence

问题

我在格式上遗漏了什么?我已经阅读了相关问题,但没有找到任何可以指导我进行有效实施的内容。

  • 占位符尺寸是否正确?

  • 我应该拆垛还是有更好的方法?

  • 我是否为ConvLSTMCell 提供了正确的输入维度?

代码

# Parameters
TIME_STEPS = 28
N_INPUT = 5
N_HIDDEN = 128
LEARNING_RATE = 0.001
NUM_UNITS = 28
CHANNEL = 1
tf.reset_default_graph()

# Input placeholders
x = tf.placeholder(tf.float32, [BATCH_SIZE, TIME_STEPS, N_INPUT])
y = tf.placeholder(tf.float32, [None, 1])

# Format input as a sequence for LSTM Input
unstacked = tf.unstack(x, TIME_STEPS, 1) # shape=(timesteps, batch, inputs)

# Convolutional LSTM Layer
lstm_layer = tf.contrib.rnn.ConvLSTMCell(
        conv_ndims=1,
        input_shape=[BATCH_SIZE, N_INPUT],
        output_channels=5,
        kernel_shape=[7,5]
    )

# Error is generated when the lstm_layer is invoked 
outputs, _ = tf.contrib.rnn.static_rnn(
    lstm_layer, 
    unstacked,
    dtype=tf.float32) 

错误信息

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-83-3568a097e4ea> in <module>()
         10     lstm_layer,
         11     unstacked,
    ---> 12     dtype=tf.float32) 

    ~/miniconda3/envs/MultivariateTimeSeries/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py in static_rnn(cell, inputs, initial_state, dtype, sequence_length, scope)
       1322             state_size=cell.state_size)
       1323       else:
    -> 1324         (output, state) = call_cell()
       1325 
       1326       outputs.append(output)

    ~/miniconda3/envs/MultivariateTimeSeries/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py in <lambda>()
       1309         varscope.reuse_variables()
       1310       # pylint: disable=cell-var-from-loop
    -> 1311       call_cell = lambda: cell(input_, state)
       1312       # pylint: enable=cell-var-from-loop
       1313       if sequence_length is not None:

    ~/miniconda3/envs/MultivariateTimeSeries/lib/python3.6/site-packages/tensorflow/python/ops/rnn_cell_impl.py in __call__(self, inputs, state, scope)
        230         setattr(self, scope_attrname, scope)
        231       with scope:
    --> 232         return super(RNNCell, self).__call__(inputs, state)
        233 
        234   def _rnn_get_variable(self, getter, *args, **kwargs):

    ~/miniconda3/envs/MultivariateTimeSeries/lib/python3.6/site-packages/tensorflow/python/layers/base.py in __call__(self, inputs, *args, **kwargs)
        715 
        716         if not in_deferred_mode:
    --> 717           outputs = self.call(inputs, *args, **kwargs)
        718           if outputs is None:
        719             raise ValueError('A layer\'s `call` method should return a Tensor '

    ~/miniconda3/envs/MultivariateTimeSeries/lib/python3.6/site-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py in call(self, inputs, state, scope)
       2110     cell, hidden = state
       2111     new_hidden = _conv([inputs, hidden], self._kernel_shape,
    -> 2112                        4 * self._output_channels, self._use_bias)
       2113     gates = array_ops.split(
       2114         value=new_hidden, num_or_size_splits=4, axis=self._conv_ndims + 1)

    ~/miniconda3/envs/MultivariateTimeSeries/lib/python3.6/site-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py in _conv(args, filter_size, num_features, bias, bias_start)
       2184     if len(shape) not in [3, 4, 5]:
       2185       raise ValueError("Conv Linear expects 3D, 4D "
    -> 2186                        "or 5D arguments: %s" % str(shapes))
       2187     if len(shape) != len(shapes[0]):
       2188       raise ValueError("Conv Linear expects all args "

    ValueError: Conv Linear expects 3D, 4D or 5D arguments: [[2, 5], [2, 2, 5]]

【问题讨论】:

    标签: python-3.x tensorflow machine-learning time-series lstm


    【解决方案1】:

    这是一个带有几个调整的示例,它至少通过了静态形状检查:

    import tensorflow as tf
    
    # Parameters
    TIME_STEPS = 28
    N_INPUT = 5
    N_HIDDEN = 128
    LEARNING_RATE = 0.001
    NUM_UNITS = 28
    CHANNEL = 1
    BATCH_SIZE = 16
    
    # Input placeholders
    x = tf.placeholder(tf.float32, [BATCH_SIZE, TIME_STEPS, N_INPUT])
    y = tf.placeholder(tf.float32, [None, 1])
    
    # Format input as a sequence for LSTM Input
    unstacked = tf.unstack(x[..., None], TIME_STEPS, 1) # shape=(timesteps, batch, inputs)
    
    # Convolutional LSTM Layer
    lstm_layer = tf.contrib.rnn.ConvLSTMCell(
            conv_ndims=1,
            input_shape=[N_INPUT, 1],
            output_channels=5,
            kernel_shape=[7]
        )
    
    # Error is generated when the lstm_layer is invoked
    outputs, _ = tf.contrib.rnn.static_rnn(
        lstm_layer,
        unstacked,
        dtype=tf.float32)
    

    注意事项:

    • input_shape 不包含批量维度 (see docstring)
    • 输入需要通道维度。可以将它作为输入之一(这就是我所做的)。
    • 不确定kernel_shape 上的一维以上对于一维卷积意味着什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 2017-08-30
      • 2019-09-06
      相关资源
      最近更新 更多