首页 > 解决方案 > 使用模型生成器,具有可变的第一维

问题描述

我有大量数据,我已经对 1000 个文件中的每一个文件进行了预处理并将相应的 x 和 y 数据存储在 numpy 数组中。

我在预处理过程中使用了滑动窗口,因此每个文件的 n 个窗口是不同的。

我正在尝试使用 model.fit_generator,但我遇到的问题是我不确定在这种情况下如何使用生成器,其中 n_windows 是可变数量。

任何帮助将非常感激。

class Generator(keras.utils.Sequence):

    def __init__(self, filenames, batch_size):
        self.filenames = filenames
        self.batch_size = batch_size

    def __len__(self):
        return (np.ceil(len(self.filenames) / float(self.batch_size))).astype(np.int)

    def __getitem__(self, index):

        files = self.filenames[index * self.batch_size: (index + 1) * self.batch_size]
        x, y = self.load_data(files)
        return x, y

    def load_data(self, files):

        x = np.array([]).reshape(0,128,64,3)
        y = np.array([]).reshape(0,128,4)

        for file in files:

            file_x = np.load('C:/project/data/x/' + file + '.npy')
            file_y = np.load('C:/project/data/y/' + file + '.npy')

            # concatenate x
            x = np.concatenate([x, file_x])
            # concatenate y
            y = np.concatenate([y, file_y])

        return x, y


batch_size = 32
generator = Generator(filenames, batch_size)

model.fit_generator(generator=generator,
                    epochs=5,
                    verbose=1,
                    )

这是我上面的代码。

标签: pythontensorflowkerasmodel-fitting

解决方案


推荐阅读