【问题标题】:Keras data generator "Error when checking input: expected input_8 to have 4 dimensions, but got array with shape ()"Keras 数据生成器“检查输入时出错:预期 input_8 有 4 个维度,但得到的数组具有形状 ()”
【发布时间】: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


【解决方案1】:

代码几乎没有错误。我改变了它,现在它正在工作,但仍然不知道为什么会发生这个错误。这是新代码:

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):
      X = []
      Y = []
      for index in indices:
        X.append(np.array(HDF5Matrix(self.data_file, self.type_data + "_X", start = index, end = index + 1)[0]))
        Y.append(np.array(HDF5Matrix(self.data_file, self.type_data + "_Y", start = index, end = index + 1)[0]))
      X = np.array(X)
      Y = np.array(Y)
      return X,Y        

    def __len__(self):
      if self.type_data == "train":
        return int(np.ceil(3450000 / float(self.batch_size)))
      else:
        return int(np.ceil(345000 / float(self.batch_size)))

    def __getitem__(self,index):
        indices = self.indices[index*self.batch_size:(index+1)*self.batch_size]
        X, Y = self.__data__generation(indices)
        #print(X.shape, Y.shape, index)
        return X,Y

【讨论】:

    【解决方案2】:

    Keras 需要 while true(无限循环)来避免 StopIteration。但是在一个普通的生成器中,在正确的steps_per_epoch(sample_size//batch_size)之后,形状将为零。

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 2018-07-25
      • 2020-09-06
      • 1970-01-01
      • 2021-12-03
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多