我正在尝试使用.fit_generator
训练自动编码器。我为此编写了自己的生成器,但是在调用VAE.fit_generator(generator=training_generator, validation_data=testing_generator)
时发生以下错误:
ValueError:空的训练数据
这是我的生成器的代码:
class DataGenerator(tf.keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs, data_dir, batch_size=32, dim=(16,9216), shuffle=True, n_music=3, gen_dir="./generated/", epoch=0):
'Initialization'
self.dim = dim
self.batch_size = batch_size
self.list_IDs = list_IDs
self.data_dir = data_dir
self.shuffle = shuffle
self.n_music = n_music
self.gen_dir = gen_dir
self.epoch = epoch
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
# Find list of IDs
list_IDs_temp = [self.list_IDs[k] for k in indexes]
# Generate data
X = self.__data_generation(list_IDs_temp)
return X
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
pass
self.epoch += 1
sample = decoder(np.random.normal(size=[self.n_music, 120]))
outputs_exp = np.expand_dims(sample, 0)
outputs_exp = np.expand_dims(outputs_exp, 0)
outputs_rsp = np.reshape(outputs_exp, (-1, 96, 16*96))
for song in range(outputs_rsp.shape[0]):
plt.imsave(self.gen_dir + str(self.epoch) + ".png", outputs_rsp[song])
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
# Initialization
X = np.empty((self.batch_size, *self.dim))
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
X[i] = np.load(self.data_dir + ID + '.npy')
return X
我按照this指南创建了生成器。