【发布时间】:2020-05-30 12:39:15
【问题描述】:
我正在尝试使用自定义生成器来拟合我的深度学习模型。
我试图找到类似的问题,但所有的答案都是关于将列表转换为 numpy 数组。我认为这不是这个错误的问题。我的列表都是 numpy 数组格式。这个自定义生成器基于来自here的自定义生成器
这是我适合模型的代码部分:
train_generator = RepresentationGenerator(representation_path=representations_path, target_path=target_path,
filenames=training_filenames, batch_size=batch_size)
val_generator = RepresentationGenerator(representation_path=representations_path, target_path=target_path,
filenames=validation_filenames, batch_size=batch_size)
self.model_semantic.fit_generator(train_generator,
epochs=10,
verbose=1,
validation_data=val_generator,
)
return 0
变量在哪里:
- representations_path - 是一个字符串,其中包含我存储训练文件的路径的目录,该文件是模型的输入
- target_path - 是一个字符串,其中包含我存储目标文件的路径的目录,哪个文件是模型的目标(输出)
- training_filenames - 是一个包含训练和目标文件名称的列表(两者具有相同的名称,但它们位于不同的文件夹中)
- batch_size - 批处理大小的整数。它的值为 7。
我的生成器类如下:
import np
from tensorflow_core.python.keras.utils.data_utils import Sequence
class RepresentationGenerator(Sequence):
def __init__(self, representation_path, target_path, filenames, batch_size):
self.filenames = np.array(filenames)
self.batch_size = batch_size
self.representation_path = representation_path
self.target_path = target_path
def __len__(self):
return (np.ceil(len(self.filenames) / float(self.batch_size))).astype(np.int)
def __getitem__(self, idx):
files_to_batch = self.filenames[idx * self.batch_size: (idx + 1) * self.batch_size]
batch_x, batch_y = [], []
for file in files_to_batch:
batch_x.append(np.load(self.representation_path + file + ".npy", allow_pickle=True))
batch_y.append(np.load(self.target_path + file + ".npy", allow_pickle=True))
return np.array(batch_x), np.array(batch_y)
我该如何解决这个错误?
谢谢小伙伴们!
当我调用 fit_generator 方法时,它会调用 fit 方法。
方法fit,调用方法func.fit,传递设置为None的变量Y
【问题讨论】:
标签: python tensorflow keras error-handling generator