将新节点添加到 Keras 模型中的输出层之一

Add new nodes to one of the output layers in a Keras model

我有一个通过 Keras Functional API 定义的自定义 ResNet 模型。我的模型也有多个输出。输出数组的最后一个元素是具有 num_class 个节点的全连接密集层。我希望能够增加这一层的节点数。这是创建我的网络的相关代码:

from tensorflow.keras import layers, models, Input, regularizers

res = []
inputs = Input(shape=(height, width, channels), name='data')
x = MyLayer()(inputs)
# ... other layers
x = MyLayer()(x)
res.append(x)
# ... other layers
x = layers.Dense(num_class, name='fc1', use_bias=True)(x)
res.append(x)
model = models.Model(inputs=inputs, outputs=[res[-2], res[-3], res[-4], res[-1]])

在问题 Adding new nodes to output layer in Keras 中,我找到了与我正在搜索的内容类似的答案,我将在下面添加:

def add_outputs(self, n_new_outputs):
    #Increment the number of outputs
    self.n_outputs += n_new_outputs
    weights = self.model.get_layer('fc8').get_weights()
    #Adding new weights, weights will be 0 and the connections random
    shape = weights[0].shape[0]
    weights[1] = np.concatenate((weights[1], np.zeros(n_new_outputs)), axis=0)
    weights[0] = np.concatenate((weights[0], -0.0001 * np.random.random_sample((shape, n_new_outputs)) + 0.0001), axis=1)
    #Deleting the old output layer
    self.model.layers.pop()
    last_layer = self.model.get_layer('batchnormalization_1').output
    #New output layer
    out = Dense(self.n_outputs, activation='softmax', name='fc8')(last_layer)
    self.model = Model(input=self.model.input, output=out)
    #set weights to the layer
    self.model.get_layer('fc8').set_weights(weights)
    print(weights[0])

但是在这个问题中只有一层作为输出,我不确定如何用我的架构复制它。

这是我想出的解决方案。我将要保留的图层分配给变量:

from tensorflow.keras import layers, models, Input, regularizers

inputs = Input(shape=(height, width, channels), name='data')
x = MyLayer()(inputs)
# ... other layers
a = MyLayer(name="a")(x)
b = MyLayer(name="b")(a)
c = MyLayer(name="c")(b)
x = layers.Dense(num_class, name='fc1', use_bias=True)(c)
model = models.Model(inputs=inputs, outputs=[c, b, a, x])

然后为了增加最后一层的节点数就调用函数increment_classes。节点总数将是 old_num_classnum_class.

的总和
def increment_classes(model, old_num_class, num_class):
    weights = model.get_layer("fc1").get_weights() 
    new_num_class = old_num_class + num_class

    # Adding new weights, weights will be 0 and the connections random
    shape = weights[0].shape[0]
    weights[1] = np.concatenate((weights[1], np.zeros(num_class)), axis=0)
    weights[0] = np.concatenate((weights[0], -0.0001 * np.random.random_sample((shape, num_class)) + 0.0001), axis=1)

    # Deleting the old dense output layer
    model.layers.pop()

    # get the output layers
    a = model.get_layer("a").output
    b = model.get_layer("b").output
    c = model.get_layer('c').output

    # Replace dense output layer (x)
    out = layers.Dense(new_num_class, name='fc1', use_bias=True)(c)

    model = models.Model(inputs=model.input, outputs=[c, b, a, out]) 

    # set weights to the layer
    model.get_layer('fc1').set_weights(weights)
    return model