I want to combine the loss functions of two neural networks which is solving the same binary classification task. Let N1 and N2 be the two neural networks with 400 and 40 neurons in the hidden layer respectively and I want the loss function to be as follows: loss(N1) + alpha* loss(N2) alpha is a weight associated to the loss function of neural network N2
I tried with the following code by I am not sure whether it is correct or wrong
M1 = Input(shape=(num_features,),name='M1')
M2 = Dense(400, activation='relu',name='M2')(M1)
M3 = Dense(1, activation='sigmoid',name='M3')(M2)
C2 = Dense(40, activation='relu',name='C2')(M1)
C3 = Dense(1, activation='sigmoid',name='C3')(C2)
#O = Concatenate(axis=-1)([M3, C3])
merged = Model(inputs=[M1],outputs=[M3, C3])
plot_model(merged,to_file='architecture4.png',show_shapes=True)
loss1 = 'binary_crossentropy'
loss2 = 'binary_crossentropy'
merged.compile(optimizer='adam',loss=[loss1, loss2],loss_weights=[alpham, 1 - alpham], metrics=['accuracy'])
history = merged.fit(X_train, [y_train,y_train] ,batch_size=16, epochs=100)
score = merged.evaluate(X_test, [y_test,y_test], batch_size=16)