python - 使用模型生成器,具有可变的第一维
问题描述
我有大量数据,我已经对 1000 个文件中的每一个文件进行了预处理并将相应的 x 和 y 数据存储在 numpy 数组中。
- x 数据的形状为:[n windows, 128, 64, 3]
- y 数据的形状为:[n windows, 128, 4]
我在预处理过程中使用了滑动窗口,因此每个文件的 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,
)
这是我上面的代码。
解决方案
推荐阅读
- mysql - 设置 Docker MySql 数据库并将其连接到 BE
- java - Oracle:按插入顺序排列的结果集
- c# - 选择一个值,然后在 c# 的同一查询中更新该值
- r - 如何在不拆分的情况下在 R 中制作俄罗斯地图?由于本初子午线而发生分裂
- c# - C# foreach 循环不拾取字符串
- git - 尽管无法连接到我的 git 服务器,Git 如何跟踪分支状态
- plsql - 找到子记录时跳过删除记录
- python - 在 Pandas 分析报告中显示“其他值”
- azure - 保护公共 Web 应用使用的 Azure 函数终结点
- r - 在 R 中安装 mvtnorm,尝试执行 gfortran-4.2 时出错,可执行文件中的 CPU 类型错误