Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将DataGenerator应用于培训和验证数据?_Python_Data Generation - Fatal编程技术网

Python 如何将DataGenerator应用于培训和验证数据?

Python 如何将DataGenerator应用于培训和验证数据?,python,data-generation,Python,Data Generation,使用这里的代码,我编写了一个定制的DataGenerator # Here, `x_set` is list of path to the images # and `y_set` are the associated classes. class DataGenerator(Sequence): def __init__(self, x_set, y_set, batch_size): self.x, self.y = x_set, y_set

使用这里的代码,我编写了一个定制的
DataGenerator

 # Here, `x_set` is list of path to the images
 # and `y_set` are the associated classes.

class DataGenerator(Sequence):

    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size

    def __len__(self):
        return math.ceil(len(self.x) / self.batch_size)

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) *
        self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) *
        self.batch_size]

        return np.array([
            resize(imread(file_name), (224, 224))
               for file_name in batch_x]), np.array(batch_y)
现在,我想知道如何将数据生成器应用于我的培训数据和验证数据? 我有
X_-train
X_-val
,它们是包含到我的图像文件的图像路径的列表,
y_-train
y_-val
是一个热编码标签

那么我可以使用这个代码吗

training_generator = DataGenerator(X_train, y_train)
validation_generator = DataGenerator(X_val, y_val)
然后再安装模型

model.fit_generator(generator=training_generator,
                    validation_data=validation_generator)

你写的基本上是正确的。不要忘记将
batch\u size
参数传递给您的
DataGenerator

 # Here, `x_set` is list of path to the images
 # and `y_set` are the associated classes.

class DataGenerator(Sequence):

    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size

    def __len__(self):
        return math.ceil(len(self.x) / self.batch_size)

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) *
        self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) *
        self.batch_size]

        return np.array([
            resize(imread(file_name), (224, 224))
               for file_name in batch_x]), np.array(batch_y)
另一方面,
epochs
参数(如您在评论中提到的)应该传递给
model.fit\u generator
(更好的方法是使用
model.fit
,因为
fit\u generator
方法是无效的)。如果不传递它,
epochs
的默认值将为1

另外,请查看如何使用
序列
类(您可以跳到使用
数据生成器
的底部)。在本教程中,除了
batch\u size
之外的两个参数被传递到
DataGenerator
,因为它们被定义为
\uuuuu init\uu
方法的输入。只要不定义它们,就不必传递它们。

您忘记了将“batch\u size”参数传递给
数据生成器
类初始化方法。如果愿意,可以在方法声明中设置默认值。好的,谢谢!:-)另一个问题出现了,因为生成器找不到文件。。。你能看看这个问题吗?这与这里的这个问题有关。。。