Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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 TypeError:不支持*:';发电机&x27;和';浮动';_Python_Error Handling_Typeerror - Fatal编程技术网

Python DataGenerator TypeError:不支持*:';发电机&x27;和';浮动';

Python DataGenerator TypeError:不支持*:';发电机&x27;和';浮动';,python,error-handling,typeerror,Python,Error Handling,Typeerror,我创建了一个数据生成器(序列)类: 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_s

我创建了一个
数据生成器(序列)
类:

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_x = np.array(imread(file_name) for file_name in batch_x)
        batch_x = batch_x * 1./255
        batch_y = self.y[idx*self.batch_size : (idx + 1)*self.batch_size]
        batch_y = np.array(batch_y)

        return batch_x, batch_y
这个
DataGenerator
应该从两个列表中获取批量数据
x_set
是指向图像的文件路径列表
y_set
是此图像数据的相应标签列表<代码>批次x是读取并除以255的
x\u集的批次<代码>批次y
y\u集合
的相应批次

之后,我使用此生成器拟合模型:

model.fit_generator(generator=training_generator,
                    validation_data=validation_generator,
                    steps_per_epoch = num_train_samples // 128,
                    validation_steps = num_val_samples // 128,
                    epochs = 5)
得到了这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-77-e56bae3d1c4b> in <module>()
      3                     steps_per_epoch = num_train_samples // 128,
      4                     validation_steps = num_val_samples // 128,
----> 5                     epochs = 5)

8 frames
<ipython-input-75-6e4037882cc3> in __getitem__(self, idx)
     11         batch_x = self.x[idx*self.batch_size : (idx + 1)*self.batch_size]
     12         batch_x = np.array(imread(file_name) for file_name in batch_x)
---> 13         batch_x = batch_x * 1./255
     14         batch_y = self.y[idx*self.batch_size : (idx + 1)*self.batch_size]
     15         batch_y = np.array(batch_y)

TypeError: unsupported operand type(s) for *: 'generator' and 'float'
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
每个历元3个步骤=num列样本//128,
4个验证步骤=num\u val\u样本//128,
---->5个时代=5个)
8帧
in _uGetItem_uuu(self,idx)
11批次x=self.x[idx*自身批次大小:(idx+1)*自身批次大小]
12 batch_x=np.array(batch_x中文件名的imread(文件名))
--->13批次x=批次x*1./255
14批次y=self.y[idx*自身批次大小:(idx+1)*自身批次大小]
15批次y=np.数组(批次y)
TypeError:不支持*:“生成器”和“浮点”的操作数类型
如何修改行
batch\u x=batch\u x*1./255

谢谢

试试这个

def __getitem__(self, idx):
        batch_x = self.x[idx*self.batch_size : (idx + 1)*self.batch_size]
        batch_x = [imread(file_name) for file_name in batch_x]
        batch_x = np.array(batch_x)
        batch_x = batch_x * 1./255
        batch_y = self.y[idx*self.batch_size : (idx + 1)*self.batch_size]
        batch_y = np.array(batch_y)

        return batch_x, batch_y

是的,很有效!谢谢我不明白的是它非常非常慢…可能是因为批量太大,但是我读到,数据量越大(我有100k个训练文件),批量就应该越大。。。所以,我拿了128。您建议的批处理大小是多少?128批处理大小很好。这种缓慢可能是由于硬件、可用RAM等其他因素造成的。你可以重新启动你的电脑,或者速度很慢。谢谢!我还注意到,我的模型在CPU上的训练速度比在GPU上快。对我来说,这也真的很奇怪。。。