How do I reference the epoch number within a Keras generator?
我正在构建一个由Keras fit_generator函数使用的Python生成器。 我想使用生成器中的当前纪元计数来调整值。 有没有办法参考这个号码?
1 2 3 4 5 6 7 8 9 10 11 12 | def generate_arrays_from_file(path): while 1: f = open(path) for line in f: x, y = process_line(line) epoch_number = ? x = x + epoch_number yield (x, y) f.close() model.fit_generator(generate_arrays_from_file('/my_file.txt'), samples_per_epoch=10000, nb_epoch=10) |
怎么样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def generate_arrays_from_file(path, samples_per_epoch): samples_produced_in_current_epoch = 0 epoch_number = 1 while 1: f = open(path) for line in f: x, y = process_line(line) x = x + epoch_number samples_produced += x.shape[0] if samples_produced_in_current_epoch > samples_per_epoch: epoch_number += 1 samples_produced_in_current_epoch = 0 yield (x, y) f.close() |