在 Tensorflow 中——是否可以在一个层中锁定特定的卷积过滤器,或者完全删除它们?

In Tensorflow - Is it possible to lock specific convolution filters in a layer, or to remove them altogether?

在 Tensorflow 中使用迁移学习时,我知道可以锁定层以防止进一步训练,方法是:

for layer in pre_trained_model.layers:
    layer.trainable = False

是否可以改为锁定图层中的特定过滤器? 如 - 如果整个图层包含 64 个过滤器,是否可以:

一种可能的解决方案是实现自定义层,将卷积拆分为单独的 number of filters 卷积并将每个通道(这是一个具有一个输出通道的卷积)设置为 trainablenot trainable.例如:

import tensorflow as tf
import numpy as np

class 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 trainable
res = 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)