0

I have two functional Keras models in the same level (same input and output shape), one of them is pre-trained, I would like to combine them horizontally and then retrain the whole model. I mean I want to initialize the pretrained with its weights and the other one randomly. How can I horizontally combine them by adding them in branches (not concatenate)?

def define_model_a(input_shape, initializer, outputs = 1):
   input_layer = Input(shape=(input_shape))
   # first path
   path10 = input_layer
   path11 = Conv1D(filters=1, kernel_size=3, strides=1, padding="same", use_bias = True, kernel_initializer=initializer)(path10)
   path12 = Lambda(lambda x: abs(x))(path11)
   output = Add()([path10, path12])
   define_model_a = Model(inputs=input_layer, outputs=output)
   define_model_a._name = 'model_a'
   return define_model_a

def define_model_b(input_shape, initializer, outputs = 1):
    input_layer = Input(shape=(input_shape))
    # first path
    path10 = input_layer
    path11 = Conv1D(filters=1, kernel_size=3, strides=1, padding="same", use_bias = True, kernel_initializer=initializer)(path10)
    path12 = ReLU()(path11)
    path13 = Dense(1, use_bias = True)(path12)
    output = path13
    define_model_b = Model(inputs=input_layer, outputs=output)
    define_model_b._name = 'model_b'
    return define_model_b    

def define_merge_interpretation()
         ????
         ????
   output = Add()(model_a, model_b)
   model = Model(inputs=input_layer, outputs=output)
   return model


initializer = tf.keras.initializers.HeNormal()    
model_a = define_model_a(input_shape, initializer, outputs = 1)
model_b = define_model_b(input_shape, initializer, outputs = 1)   
model_a.load_weights(load_path)

merge_interpretation = def merge_interprettation( )
history = merge_interpretation.fit(......

As reference, I am looking for a final structure like this in the image, but with some pretrained branches. enter image description here

deijany91
  • 9
  • 4
  • Couldn't you just use the output of `model_a` and input for `model_b`? – Djinn Jun 22 '22 at 23:04
  • Hi @Djinn. No I can't, I want to fix one model and iteratively train several networks changing the extra model, thus I can see which combination is better for my problem, I am looking for some structure like this in the attached picture. – deijany91 Jun 23 '22 at 06:59
  • Sorry, I'm not fully understanding your question. Do you want something using complex topologies, using multichannel model (using one input) but not training one model? If so, do you have access to the layers in the pretrained model or the model itself when you construct your full model? You can prevent training on the pretrained model using `for layer in model.layers: layer.trainable = False` so that model remains fixed. Then you can continue with the rest, use a list containing your input data repeated per model. Unless I misunderstood something D: – Djinn Jun 23 '22 at 15:34

0 Answers0