Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:尺寸必须相等,但在自制图层期间为64和32_Python_Tensorflow_Keras - Fatal编程技术网

Python ValueError:尺寸必须相等,但在自制图层期间为64和32

Python ValueError:尺寸必须相等,但在自制图层期间为64和32,python,tensorflow,keras,Python,Tensorflow,Keras,我在google colab笔记本中使用tensorflow 2.2.0。我有一些自制的激活层,但当我尝试使用特定的激活层时,问题出现了: class MPELU(tf.keras.layers.Layer): """ Multiple Parametric Exponential Linear Units. """ def __init__(self, channel_wise=True, **kwargs): super(MPELU, self).__init__(**kwargs

我在google colab笔记本中使用tensorflow 2.2.0。我有一些自制的激活层,但当我尝试使用特定的激活层时,问题出现了:

class MPELU(tf.keras.layers.Layer):
"""
Multiple Parametric Exponential Linear Units.
""" 
def __init__(self, channel_wise=True, **kwargs):
    super(MPELU, self).__init__(**kwargs)
    self.channel_wise = channel_wise

def build(self, input_shape):
    shape = [1]

    if self.channel_wise:
        shape = [int(input_shape[-1])]  # Number of channels

    self.alpha = self.add_weight(name='alpha', shape=shape, dtype=K.floatx(),
                                 initializer=tf.keras.initializers.RandomUniform(minval=-1, maxval=1),
                                 trainable=True)
    self.beta = self.add_weight(name='beta', shape=shape, dtype=K.floatx(),
                                initializer=tf.keras.initializers.RandomUniform(minval=0.0, maxval=1),
                                trainable=True)

    # Finish buildidng
    super(MPELU, self).build(input_shape)


@tf.function
def call(self, inputs, **kwargs):
    positive = tf.keras.activations.relu(inputs)
    negative = self.alpha * (K.exp(-tf.keras.activations.relu(-inputs) * cons_greater_zero(self.beta)) - 1)

    return positive + negative

def compute_output_shape(self, input_shape):
    return input_shape
错误代码如下所示:

    ValueError: Dimensions must be equal, but are 64 and 32 for '{{node mpelu/mul_10}} = Mul[T=DT_FLOAT](mpelu/Neg_11, mpelu/add_10)' with input shapes: [?,4,4,64], [32].
我在每次调用中使用打印函数打印形状,结果如下:

(None, 16, 16, 32)
(None, 7, 7, 32)
(None, 7, 7, 32)
(None, 7, 7, 32)
(None, 7, 7, 32)
(None, 4, 4, 64)

你也可以在每个调用函数中添加代码和形状。好的,这部分有点复杂。完整的代码在github上,是一个用于创建顺序深入学习实验的套件:我使用beckamresnet层架构(在net_keras.py和resnet.py中定义)和激活函数(在activations.py中定义)。调用fit和compile的主实验文件位于experience.py中。希望它有帮助,很抱歉我不能给出确切的代码。你能不能也添加代码,以及如何在每个调用函数中生成该形状。好的,这部分有点复杂。完整的代码在github上,是一个用于创建顺序深入学习实验的套件:我使用beckamresnet层架构(在net_keras.py和resnet.py中定义)和激活函数(在activations.py中定义)。调用fit和compile的主实验文件位于experience.py中。希望能有帮助,很抱歉我不能给出准确的代码。