首页 > 解决方案 > ValueError:Dimensions must be equal,但在自制层中是64和32

问题描述

我在谷歌 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)

标签: pythontensorflowkeras

解决方案


推荐阅读