Я пытаюсь написать пользовательский слой активации в керасе. Проблема в том, что я пытался сделать это с сигмоидом и с помощью функции активации 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]
Что я делаю не так?