我有以下代码:
import tensorflow as tf
def create_Mini(input_shape):
inputs = tf.keras.Input(shape=input_shape) # initialize the input shape to be "channels last"
x = tf.keras.layers.Conv2D( # conv1
filters=4, kernel_size=(8, 8),
activity_regularizer=tf.nn.lrn,
name="conv2d_1")(inputs)
x = tf.keras.layers.Conv2D( # conv2
filters=16, kernel_size=(4, 4),
activity_regularizer=tf.nn.lrn,
name="conv2d_2")(x)
x = tf.keras.layers.GlobalAvgPool2D( # global average pooling
name="gapool_2")(x)
outputs = tf.keras.layers.Dense(256, # fc3
activation='softmax',
name="fc_3")(x)
return tf.keras.Model(inputs=inputs, outputs=outputs)
if __name__ == "__main__":
nfft = 512
frames = 300
input_shape = (nfft, frames, 1)
model = create_Mini(input_shape)
model.compile(
optimizer='rmsprop',
loss='sparse_categorical_crossentropy')
运行代码时,出现以下错误,我不知道如何解决:
Traceback (most recent call last):
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1610, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 1 in both shapes must be equal, but are 505 and 502. Shapes are [?,505,293,4] and [?,502,290,16].
From merging shape 0 with other shapes. for 'loss/AddN' (op: 'AddN') with input shapes: [?,505,293,4], [?,502,290,16].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 40, in <module>
loss='sparse_categorical_crossentropy')
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 373, in compile
self._compile_weights_loss_and_weighted_metrics()
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1653, in _compile_weights_loss_and_weighted_metrics
self.total_loss = self._prepare_total_loss(masks)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1762, in _prepare_total_loss
math_ops.add_n(custom_losses))
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\util\dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\ops\math_ops.py", line 3018, in add_n
return gen_math_ops.add_n(inputs, name=name)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py", line 477, in add_n
"AddN", inputs=inputs, name=name)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 793, in _apply_op_helper
op_def=op_def)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 548, in create_op
compute_device)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3429, in _create_op_internal
op_def=op_def)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1773, in __init__
control_input_ops)
File "D:\Anaconda\envs\tf_2.0\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1613, in _create_c_op
raise ValueError(str(e))
ValueError: Dimension 1 in both shapes must be equal, but are 505 and 502. Shapes are [?,505,293,4] and [?,502,290,16].
From merging shape 0 with other shapes. for 'loss/AddN' (op: 'AddN') with input shapes: [?,505,293,4], [?,502,290,16].
问题是由tf.nn.lrn引起的吗?我尝试将其替换为“ l1”,并且效果很好。 但是我不明白为什么以及如何将其修复为与tf.nn.lrn一起使用。
您对我为什么收到此错误以及如何解决该错误有任何想法吗?
谢谢。