我正在尝试使用自定义生成器来适应我的深度学习模型。
When i fit the model, it shows me this error:
我试图找到类似的问题,但所有答案都是关于将列表转换为 numpy 数组。我认为这不是这个错误的问题。我的列表都是 numpy 数组格式。该自定义生成器基于以下自定义生成器here https://medium.com/@mrgarg.rajat/training-on-large-datasets-that-dont-fit-in-memory-in-keras-60a974785d71
这是我适合模型的代码部分:
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
其中变量是:
-
表示路径- 是一个字符串,其中包含我存储训练文件的路径的目录,该文件是模型的输入
-
目标路径- 是一个字符串,其中包含我存储目标文件的路径的目录,即哪个文件是模型的目标(输出)
-
训练文件名- 是包含训练文件和目标文件名称的列表(两者具有相同的名称,但位于不同的文件夹中)
-
批量大小- 批次大小的整数。它的值为 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)
These are the values, when the method fit is called:
我该如何修复这个错误?
谢谢各位朋友!
When I call the method fit_generator, it calls the method fit.
fit 方法调用 func.fit 方法并传递设置为 None 的变量 Y
The error occurs in this line: