Basically, I want a generator that re-read the magic method _len__ at each epoch to recalculate how many batches will be done that epoch.
Here is a snippet of code:
import tensorflow as tf
import numpy as np
class GeneratorFile(tf.keras.utils.Sequence):
def __init__(self, file_list):
self.file_list = file_list
self.desired_file = self.file_list[0]
print('This should be file 1:', self.desired_file)
def __len__(self):
if self.desired_file == 'file1':
return 2
else:
return 3
def on_epoch_end(self):
self.desired_file = self.file_list[1]
print('This should be file 2:', self.desired_file)
def __getitem__(self, item):
return np.zeros((16, 1)), np.zeros((16,))
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, input_dim=1, activation="softmax"))
model.compile(
optimizer='Adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
model_file_train = ['file1', 'file2']
generator_train = GeneratorFile(model_file_train)
model.fit(generator_train, epochs=2, initial_epoch = 0)
In the len magic method, I want to switch the number of batches in that epoch when the file has changed, which it does at the end of the first epoch. However, currently, len is run once at the beginning of the training process and then never again. Is there anything to change this?