1

I have a large dataset to train on Keras and in order to avoid the "Memory Error", I have tried to use the fit_generator function. It's weird that when I use fit_generator, the loss seems doesn't change, but the fit function works well.

Whatever the fir_gen or fit function, the dataset and other code are the same.

And this is an lstm - seq2seq model.

I have searched for a long time and I found other two questions which seem as same as me. 1.Keras: network doesn't train with fit_generator() According to this article, I change my batch_size but it doesn't work. And when I try to change the 'yield' to return, it throws me an error. 2.Keras doesn't train using fit_generator() This article actually doesn't have an answer.

model.fit_generator(generate_train(batch_size=200), 
                    steps_per_epoch=5,
                    epochs=100, 
                    verbose=1, 
                    callbacks=callbacks_list, 
                    class_weight=None, 
                    max_queue_size=10, 
                    workers=1, 
                    use_multiprocessing=False, 
                    shuffle=False, 
                    initial_epoch=initial_epoch
                    )
def generate_train(batch_size):
    steps=0
    context_ = np.load(main_path + 'middle_data/context_indexes.npy')
    final_target_ = np.load(main_path + 'middle_data/target_indexes.npy')
    context_ = context_[:1000]
    final_target_ = final_target_[:1000]
    while True:
        context = context_[steps:steps+batch_size]
        final_target = final_target_[steps:steps+batch_size]

        processing. . .
        outs = . . .

        yield [context, final_target], outs
        steps += batch_size
        if steps == 1000:
            steps = 0

when I use fit():

Epoch 1/30 loss: 2.5948 - acc: 0.0583 
Epoch 2/30 loss: 2.0840 - acc: 0.0836 
Epoch 3/30 loss: 1.9226 - acc: 0.0998 
Epoch 4/30 loss: 1.8286 - acc: 0.1086 
Epoch 5/30 loss: 1.7399 - acc: 0.1139 
Epoch 6/30 loss: 1.6509 - acc: 0.1192 
Epoch 7/30 loss: 1.5518 - acc: 0.1247 
Epoch 8/30 loss: 1.4330 - acc: 0.1316 
Epoch 9/30 loss: 1.3117 - acc: 0.1454 
Epoch 10/30 loss: 1.1872 - acc: 0.1657 
Epoch 11/30 loss: 1.0720 - acc: 0.1893 
Epoch 12/30 loss: 0.9589 - acc: 0.2169 
. . . 

when I use fit_generator():

Epoch 1/100 loss: 3.4926 - acc: 0.0370
Epoch 2/100 loss: 2.7239 - acc: 0.0388
Epoch 3/100 loss: 2.6030 - acc: 0.0389
Epoch 4/100 loss: 2.5727 - acc: 0.0408
Epoch 5/100 loss: 2.5628 - acc: 0.0366
Epoch 6/100 loss: 2.5513 - acc: 0.0420
Epoch 7/100 loss: 2.5475 - acc: 0.0387
Epoch 8/100 loss: 2.5508 - acc: 0.0407
Epoch 9/100 loss: 2.5490 - acc: 0.0418
Epoch 10/100 loss: 2.5419 - acc: 0.0401
abunickabhi
  • 558
  • 2
  • 9
  • 31

1 Answers1

0

I have already found one solution to it.

I print all the data that come form generator by 'yield', and I found that I have mistakenly rewrite the training-data every epoch.

So the original data is changing which led to the stable loss.

abunickabhi
  • 558
  • 2
  • 9
  • 31