Search code examples
kerasneural-networktransfer-learning

Add units (Neurons) to an existing model in Keras


I have trained a model and I want to add more units to it's hidden units and train it for some more epochs. I am implementing a constructive learning algorithm. How can I add neuron to an existing model hidden layer ? And also is there a way to only train the added units parameters and other parameters get freezed ? (In 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

I want to add neuron to my hidden layer repetitively, until my network error gets below the threshold.


Solution

  • I solved the problem. Instead of adding a neuron to the current layer, We can add another Dense layer which is connected to the next and previous layer and then concatenate the new layer with the old one.enter image description here