千万里不及你
一种可能的解决方案是实现自定义层,该层将卷积拆分为单独的卷积,并将每个通道(具有一个输出通道的卷积)设置为 或 设置为 。例如:number of filterstrainablenot trainableimport tensorflow as tfimport numpy as npclass Conv2DExtended(tf.keras.layers.Layer): def __init__(self, filters, kernel_size, **kwargs): self.filters = filters self.conv_layers = [tf.keras.layers.Conv2D(1, kernel_size, **kwargs) for _ in range(filters)] super().__init__() def build(self, input_shape): _ = [l.build(input_shape) for l in self.conv_layers] super().build(input_shape) def set_trainable(self, channels): """Sets trainable channels.""" for i in channels: self.conv_layers[i].trainable = True def set_non_trainable(self, channels): """Sets not trainable channels.""" for i in channels: self.conv_layers[i].trainable = False def call(self, inputs): results = [l(inputs) for l in self.conv_layers] return tf.concat(results, -1)和用法示例:inputs = tf.keras.layers.Input((28, 28, 1))conv = Conv2DExtended(filters=4, kernel_size=(3, 3))conv.set_non_trainable([1, 2]) # only channels 0 and 3 are trainableres = conv(inputs)res = tf.keras.layers.Flatten()(res)res = tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)(res)model = tf.keras.models.Model(inputs, res)model.compile(optimizer=tf.keras.optimizers.SGD(), loss='binary_crossentropy', metrics=['accuracy'])model.fit(np.random.normal(0, 1, (10, 28, 28, 1)), np.random.randint(0, 2, (10)), batch_size=2, epochs=5)