我正在尝试保存模型中包含 2 个 dropout 层的 TF Keras 模型(这是一个注意模型 - 第一个 dropout 用于注意权重,第二个用于该层的整体输出)。但是,当我使用此模型并使用 2 个参数调用它时,我收到一条错误消息,指出我的输入之间存在维度不匹配,我不确定这是从哪里来的。
我真的无法发布代码。我知道这有点含糊,但我可以在下面发布回溯。任何关于我应该考虑的检查/可能出错的建议将不胜感激。谢谢!
我的一般代码是:
inputs = tf.keras.Input(shape=(...), name="inputs")
mask = tf.keras.Input(shape=(..., name="mask")
# perform attention computation
attention = ...
attention = tf.keras.layers.Dropout(rate=0.1)(attention)
outputs = tf.keras.layers.Dense(units=..., activation="relu")(attention + inputs)
outputs = Normalize(outputs)
model = tf.keras.Model([inputs, mask], outputs)
model_inputs = tf.keras.Input(shape=(...), name="model_inputs")
model_outputs = tf.keras.Dropout(rate=0.1)(model_inputs)
model_mask = tf.keras.Input(shape=(...), name="model_mask")
model_outputs = model([outputs, model_mask])
错误:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 562, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py", line 822, in call
mask=masks)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py", line 1010, in _run_internal_graph
output_tensors = layer.call(computed_tensor, **kwargs)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1822, in call
return self._make_op(inputs)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1837, in _make_op
c_op = ops._create_c_op(graph, self.node_def, inputs, control_inputs=[])
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1664, in _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 256 and 0 for 'encode_layer_0/tf_op_layer_tony/cond/dropout/random_uniform/mul/tony/cond/dropout/random_uniform/mul' (op: 'Mul') with input shapes: [?,?,256], [0].
编辑:修复了“代码”。我的意思是写model_inputs。我在这里发布的代码不是实际的代码,它只是有点相似。这很可能是我进行注意力权重计算的地方,但我不确定要寻找什么,因为代码有点长。谢谢!