我正在构建一个由Keras fit_generator函数使用的Python生成器。我想使用生成器中的当前纪元计数来调整值。有没有办法引用这个数字?
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)
答案 0 :(得分:2)
怎么样:
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()