I have a huge dataset that I have to provide Keras in the form of a generator, because it does not fit in memory. However, using fit_generator , I cannot reproduce the results obtained during a regular workout with model.fit . Also, each era lasts much longer.
I implemented a minimal example. Maybe someone can show me where the problem is.
import random import numpy from keras.layers import Dense from keras.models import Sequential random.seed(23465298) numpy.random.seed(23465298) no_features = 5 no_examples = 1000 def get_model(): network = Sequential() network.add(Dense(8, input_dim=no_features, activation='relu')) network.add(Dense(1, activation='sigmoid')) network.compile(loss='binary_crossentropy', optimizer='adam') return network def get_data(): example_input = [[float(f_i == e_i % no_features) for f_i in range(no_features)] for e_i in range(no_examples)] example_target = [[float(t_i % 2)] for t_i in range(no_examples)] return example_input, example_target def data_gen(all_inputs, all_targets, batch_size=10): input_batch = numpy.zeros((batch_size, no_features)) target_batch = numpy.zeros((batch_size, 1)) while True: for example_index, each_example in enumerate(zip(all_inputs, all_targets)): each_input, each_target = each_example wrapped = example_index % batch_size input_batch[wrapped] = each_input target_batch[wrapped] = each_target if wrapped == batch_size - 1: yield input_batch, target_batch if __name__ == "__main__": input_data, target_data = get_data() g = data_gen(input_data, target_data, batch_size=10) model = get_model() model.fit(input_data, target_data, epochs=15, batch_size=10)
On my computer, model.fit always ends the 10th era with a loss of 0.6939 and after approx. 2-3 seconds
However, the model.fit_generator method works much longer and completes the last era with a different loss ( 0.6931 ).
I don’t understand why the results in both approaches are different. It may not seem like a big difference, but I have to be sure that the same data with the same network gives the same result, regardless of the usual training or using the generator.
Update: @Alex R. gave an answer to part of the original problem (some performance problems, as well as changing the results at each start). However, since the main problem remains, I just adjusted the question and the name accordingly.