【发布时间】:2019-07-05 01:22:11
【问题描述】:
我正在使用 Keras 中的数据生成器来训练具有大型数据集的模型。但是我在第一个纪元的最后一批中每次都收到错误Error when checking input: expected input_8 to have 4 dimensions, but got array with shape ()。但是我检查了我的数据集文件,它没有空数组,那么空数组是怎么来的呢?我什至尝试在生成数组时打印它们,其中很少有显示为空的。这是我的数据生成器代码:
class data_generator(Sequence):
def __init__(self,data_file,type_data,batch_size,shuffle=True):
self.data_file = data_file
self.type_data = type_data
self.batch_size = batch_size
self.shuffle = shuffle
self.on_epoch_end()
def on_epoch_end(self):
if self.type_data == "train":
self.indices = np.arange(3450000)
else:
self.indices = np.arange(345000)
if self.shuffle:
np.random.shuffle(self.indices)
def __data__generation(self,indices):
return X,Y
def __len__(self):
if self.type_data == "train":
return int(np.ceil(10000 / float(self.batch_size)))
else:
return int(np.ceil(1000 / float(self.batch_size)))
def __getitem__(self,index):
#print(self.indices[(index)*self.batch_size], self.indices[(index+1)*self.batch_size])
X = np.array(HDF5Matrix(self.data_file, self.type_data + "_X", start = self.indices[index*self.batch_size], end = self.indices[(index+1)*self.batch_size]))
Y = np.array(HDF5Matrix(self.data_file, self.type_data + "_Y", start = self.indices[index*self.batch_size], end = self.indices[(index+1)*self.batch_size]))
#print(X.shape, Y.shape)
return X,Y
这是我启动 fit 生成器的代码:
train_generator = data_generator("drive/My Drive/Dataset/dataset.h5", "train", 20)
eval_generator = data_generator("drive/My Drive/Dataset/dataset.h5", "eval", 20)
model = create_model()
history = model.fit_generator(generator = train_generator,epochs = 100,validation_data=eval_generator,use_multiprocessing=False)
我该如何解决这个问题?还有用于训练大型数据集的数据生成器的替代方法吗?数据生成器有很多错误,并给出了很多错误。
【问题讨论】:
-
测试您的数据生成器。在将其传递给
fit_generator之前,请确保它可以运行完整迭代,同时返回预期结果。具体测试train_generator[len(generator) - 1]。您的代码显示 eval 生成器已改组,但火车没有改组……这与您想要的相反。
标签: python tensorflow keras