0

Different with link1 and link2, now, I have two network with different loss functions respectively, and I want to fit these two models alternately in one batch.

To be specific, if there is one model, A. I train it by the following pseudo code:

model = some_value # initial
for e in 1:epoch
    for b in 1:batch
        model = train(A, model)

The above procedure can be realized only by one line of code in keras:

model.fit(X_train, Y_train,
          batch_size=32, epoch=10)

Now, I have two models, A and B. I train them by the following pseudo code:

model_A = some_value # initial
model_B = some_value # initial
for e in 1:epoch
    for b in 1:batch
        model_A = train(A, model_B) # I using the model_B in the loss function of neural network model_A
        model_B = train(A, model_A) # I using the model_A in the loss function of neural network model_B

How to realize this procedure in keras?

Tim Xu
  • 11
  • 1

1 Answers1

0
batchlen = int(len(X_train)/batches)
for e in range(0,epochs):
    for b in range(0,batches):
        model_A.fit(
          X_train[b*batchlen:(b+1)*batchlen], 
          Y_train[b*batchlen:(b+1)*batchlen], 
          initial_epoch=e, 
          epochs=e+1)
        model_B.fit(X_train[b*batchlen:(b+1)*batchlen], Y_train[b*batchlen:(b+1)*batchlen], initial_epoch=e, epochs=e+1)

Way better is to use fit_generator along with a generator to feed X_train, Y_train. The result should be something like

for e in range(0,epochs):
    model_A.fit_generator(
      your_generator(X_train, Y_train), 
      initial_epoch=e, 
      epochs=e+1, 
      steps_per_epoch=len(X_train)/(batch_size))
    model_B.fit_generator(your_generator(X_train, Y_train), initial_epoch=e, epochs=e+1, steps_per_epoch=len(X_train)/(batch_size))
dcolazin
  • 831
  • 1
  • 10
  • 25