ValueError:形状(?,83)和(?,128)不兼容

时间:2018-06-25 12:45:57

标签: python-3.x tensorflow

尝试使用TensorFlow创建LSTM时出现以下错误:
ValueError: Shapes (?, 83) and (?, 128) are incompatible

模型的输入具有以下形状:
(batch_size, time, features) / (50, 68, 83)

以下是该模型的相关代码:

x_text = tf.placeholder(tf.float32, [None, *text.shape[1:]])

cells = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.ResidualWrapper(
    tf.contrib.rnn.BasicLSTMCell(num_units=units))
    for units in [128, 256]
])

text_outputs, text_state = tf.nn.dynamic_rnn(
    cell=cells,
    inputs=x_text,
    dtype=tf.float32,
)

我已经尝试了很长时间才能弄清楚出了什么问题,但是我做不到。我搜索了整个互联网(不,真的!),似乎没有人遇到形状(?, a) and (?, b)是问题的相同问题,而是解决方案无济于事的所有其他组合。

哦-这是堆栈跟踪:

Traceback (most recent call last):
  File "residual_hierachical_rnn.py", line 97, in <module>
    dtype=tf.float32,
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py", line 627, in dynamic_rnn
    dtype=dtype)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py", line 824, in _dynamic_rnn_loop
    swap_memory=swap_memory)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 3224, in while_loop
    result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 2956, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 2893, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 3194, in <lambda>
    body = lambda i, lv: (i + 1, orig_body(*lv))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py", line 795, in _time_step
    (output, new_state) = call_cell()
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py", line 781, in <lambda>
    call_cell = lambda: cell(input_t, state)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 232, in __call__
    return super(RNNCell, self).__call__(inputs, state)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/layers/base.py", line 717, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1292, in call
    cur_inp, new_state = cell(cur_inp, cur_state)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1168, in __call__
    res_outputs = (self._residual_fn or default_residual_fn)(inputs, outputs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1166, in default_residual_fn
    nest.map_structure(assert_shape_match, inputs, outputs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py", line 375, in map_structure
    structure[0], [func(*x) for x in entries])
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py", line 375, in <listcomp>
    structure[0], [func(*x) for x in entries])
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1163, in assert_shape_match
    inp.get_shape().assert_is_compatible_with(out.get_shape())
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py", line 844, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (?, 83) and (?, 128) are incompatible

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您的LSTM单元的单位大小应与功能部件的数量匹配。要解决此问题,请使用[83, 256]

此外,我知道LSTM层之间的连接是完全连接的,但是据我所知,最好使层中的单位大小保持一致,以减少混乱。换句话说,考虑使用[83, 83]作为单位尺寸。

答案 1 :(得分:0)

正如MPKenning所说,您的LSTM单元应该与您的输入功能相匹配。

但是您使用ResidualWrapper的事实迫使您保持相同的深度,因为它所做的是将单元格的输入和输出相加。

如果您删除ResidualWrapper,它将与[83, 256]一起使用:

cells = tf.contrib.rnn.MultiRNNCell([
    tf.contrib.rnn.BasicLSTMCell(num_units=units)
    for units in [83, 256]
])