keras fit_generator parameter steps_per_epoch

729 Views Asked by At

I want to use the keras model.fit_generator method for that I wrote my own generator and for the method I need to define the parameter "steps_per_epoch" I want to use every training data once for every epoch.

Now my problem is I generate the features in the generator I read wav-files and create the fft and before I start the training I don't know how much batches/samples I have. I can calculate the fft for every file before I start using the fit_generator method but every time I change my dataset(>20GB) I would need to recalculate the fft for every file and save the count for steps per epoch. Is there a better way how I can define that the fit_generator uses every sample only one time without calculate the steps per epoch? Or can my own generator pass the fit_generator when to start a new epoch?

Here is the code for my generator

def my_generator(filename_list):
    while True:
        for fname in filename_list:
                data, sr = librosa.load(fname)
                fft_result = librosa.core.stft(data)
                batches = features.create_batches(fft_result, batch_size)

                for i in range(len(batches)):
                    yield (batches[i], label)

model.fit_generator(my_generator(filename_list=filename_list, batch_size=batch_size), steps_per_epoch=100, epochs=10)
1

There are 1 best solutions below

0
On

For each file in the list, you have to calculate fft that has 'n' batches where 'n' is different for each file. If this is the case than:

  1. Navie method is to loop through the batch generator to calculate the actual number of batches. This process needs to be done only once. you can save that number for future use as well.
  2. The second method could be to assign an arbitrary number to step_per_epoch. That arbitrary number should be greater than or equal to the number of files in the list multiplied by the number of the batches each fft can generate. The number of fft batches could be an arbitrary number. This way, if you shuffle data after the external "for" loop completes, then after some epoch statistically speaking all training data would be seen by the model. By using early_stop you can have properly converged model where "epochs" should be a very large value, 1000 for example.