Keras 自定义图层输入形状兼容性问题

Keras custom layer input shape compatibility problem

我正在尝试在 keras 中编写自定义激活层。问题是,我尝试用 sigmoid 和 relu 激活函数来做到这一点。这些示例实际上是相同的,但一个有效而另一个无效。 工作示例是:

class ParamRelu(Layer):
    def __init__(self, alpha, **kwargs):
        super(ParamRelu, self).__init__(**kwargs)
        self.alpha = K.cast_to_floatx(alpha)

    def call(self, inputs):
        return K.sigmoid(self.alpha * inputs) * inputs

    def get_config(self):
        config = {'alpha': float(self.alpha)}
        base_config = super(ParamRelu, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    def compute_output_shape(self, input_shape):
        return input_shape


def aafcnn(alpha_row):
    (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
    x_train = x_train[:, :, :, np.newaxis] / 255.0
    x_test = x_test[:, :, :, np.newaxis] / 255.0
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)

    model = Sequential()

    model.add(Conv2D(filters=16, kernel_size=3, padding='same', input_shape=(28, 28, 1)))
    model.add(ParamRelu(alpha=alpha_row[0]))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Conv2D(filters=32, kernel_size=3, padding='same'))
    model.add(ParamRelu(alpha=alpha_row[1]))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Conv2D(filters=64, kernel_size=3, padding='same'))
    model.add(ParamRelu(alpha=alpha_row[2]))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Flatten())
    model.add(Dense(50, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

    model.summary()

    model.fit(x_train, y_train, epochs=1, validation_split=0.1)

    _, test_acc = model.evaluate(x_test, y_test)
    print(test_acc)


alpha_matrix = np.random.rand(10, 3)
for i in range(10):
    aafcnn(alpha_matrix[i])

这行得通。 这不是:

class ParamRelu(Layer):
    def __init__(self, alpha, **kwargs):
        super(ParamRelu, self).__init__(**kwargs)
        self.alpha = K.cast_to_floatx(alpha)

    def call(self, inputs):
        return K.max((self.alpha * inputs), 0)

    def get_config(self):
        config = {'alpha': float(self.alpha)}
        base_config = super(ParamRelu, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    def compute_output_shape(self, input_shape):
        return input_shape


def aafcnn(alpha_row):
    (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
    x_train = x_train[:, :, :, np.newaxis] / 255.0
    x_test = x_test[:, :, :, np.newaxis] / 255.0
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)

    model = Sequential()

    model.add(Conv2D(filters=16, kernel_size=3, padding='same', input_shape=(28, 28, 1)))
    model.add(ParamRelu(alpha=alpha_row[0]))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Conv2D(filters=32, kernel_size=3, padding='same'))
    model.add(ParamRelu(alpha=alpha_row[1]))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Conv2D(filters=64, kernel_size=3, padding='same'))
    model.add(ParamRelu(alpha=alpha_row[2]))
    model.add(MaxPooling2D(pool_size=2))
    model.add(Flatten())
    model.add(Dense(50, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

    model.summary()

    model.fit(x_train, y_train, epochs=1, validation_split=0.1)

    _, test_acc = model.evaluate(x_test, y_test)
    print(test_acc)


alpha_matrix = np.random.rand(10, 3)
for i in range(10):
    aafcnn(alpha_matrix[i])

错误是:

ValueError: Input 0 of layer max_pooling2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [28, 28, 16]

我尝试使用 input_shape=(None, 28, 28, 1) 而不是 input_shape=(28, 28, 1),但在这种情况下错误变为:

ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, None, 28, 28, 1]

我做错了什么?

问题是,在第二种情况下,行:

return K.max((self.alpha * inputs), 0)

你在axis=0.

中将尺寸减小一

因此 max_pooling2d 将无法获得所需的 4D 输入。