将单位(神经元)添加到 Keras 中的现有模型

Add units (Neurons) to an existing model in Keras

我已经训练了一个模型,我想向它的隐藏单元添加更多单元并训练它更多的时期。我正在实施一个建设性的学习算法。如何将神经元添加到现有模型隐藏层?还有一种方法可以只训练添加的单元参数而其他参数被冻结吗? (在 KERAS 中)

def create_first_sub_NN(X):
    sub_input = tf.keras.Input(shape=(X.shape[1],))
    h = Dense(1, activation="sigmoid",name="hidden")(sub_input)
    h = tf.keras.Model(inputs=sub_input, outputs=h)
    m_combined = tf.keras.layers.concatenate([h.input, h.output])
    out = Dense(1, activation="relu")(m_combined)
    out = tf.keras.Model(inputs=sub_input, outputs=out)
    return out

def train_current_model(model,input_groups,Y,error_thr):
    opt = keras.optimizers.Adam(learning_rate=0.01)
    callbacks = stopAtLossValue()
    # overfitCallback = EarlyStopping(monitor='loss', min_delta=5, 
    patience=10) # if for 10 epochs the error did not decreased more than 5, then stop the current network training
    model.compile(optimizer=opt, loss='mean_absolute_error')

    model.fit(input_groups, train_label, epochs=100, batch_size=32,callbacks=[callbacks])

enter code here

model = create_first_sub_NN(X1_train)
keras.utils.plot_model(model, "first.png",show_shapes=True)
print(model.summary())
list_of_inputs = [sub_X_list[0]]
train_current_model(model, list_of_inputs, train_label, 0.1)
# how to add number of units in my hidden layer for the
enter code here

我想重复将神经元添加到我的隐藏层,直到我的网络错误低于阈值。

我解决了这个问题。我们可以添加另一个连接到下一层和上一层的 Dense 层,然后将新层与旧层连接起来,而不是向当前层添加神经元。