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



尝试使用 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

提前非常感谢您的任何帮助!!

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

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

如果删除ResidualWrapper它应该适用于[83, 256]

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

LSTM 像元的单位大小应与要素数量相匹配。要解决此问题,请使用[83, 256]

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

最新更新