Search code examples
kerasneural-networkconv-neural-networkautoencoderdeconvolution

How to train non-shared autoencoder networks parallelly using single loss function


Train two non-shared neural networks together.

I have two different auto-encoder networks, one for camera images, which takes an input image and try to reproduce the same image by using the transpose convolutional method. The second network takes a sketch image and does the same thing. Both networks have identical structures but learn different filters.

autoencoder_p = Model(input_img, decoder_p(encoder_p(input_img)))
autoencoder_s = Model(input_img, decoder_s(encoder_s(input_img)))

autoencoder_p.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
autoencoder_p.fit(X_train_real, X_train_real, epochs=10, batch_size=1, validation_split=0.1)

autoencoder_s.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
autoencoder_s.fit(X_train_sim, X_train_sim, epochs=10, batch_size=1, validation_split=0.1)

The problem is I have to compile and train both networks one after another. Is there any possible way to compile and train both networks together. Because both networks are identical and use the same loss and training strategy.


Solution

  • Make a single model that contains both.

    Warning: your code seems to be using the same input image for both, which is not what you describe. Have two inputs input_img_p and input_img_s.

    twin_model = Model([input_img_p, input_img,_s],
                       [
                           decoder_p(encoder_p(input_img_p)),
                           decoder_s(encoder_p(input_img_s)),                             
                       ])
    
    twin_model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
    twin_model.fit([X_train_real, X_train_sim], [X_train_real, X_train_sim], ...)