如何解决打开生成器以在 model.fit 内部使用
我遇到了与此 post 中解决的完全相同的问题:我无法在 model.fit
中 keras
的训练输入中使用生成器,因此我应该解开它。建议的解决方案:
from platform import python_version_tuple
if python_version_tuple()[0] == '3':
xrange = range
izip = zip
imap = map
else:
from itertools import izip,imap
import numpy as np
# ..
# other code as in question
# ..
x,y = izip(*(validation_seq[i] for i in xrange(len(validation_seq))))
x_val,y_val = np.vstack(x),np.vstack(y)
正是我正在寻找的。问题是它适用于初始 question 的 ImageDataGenerator()
,但不适用于我的生成器,如下所示:
def generator(data,L,D,i_min,i_max,shuffle=False,batch_size=16,step=1):
if i_max is None:
i_max = len(data) - D - 1
i = i_min + L
while 1:
if shuffle:
rows = np.random.randint(i_min + L,size=batch_size)
else:
if i + batch_size >= i_max:
i = i_min + L
rows = np.arange(i,min(i + batch_size,i_max))
i += len(rows)
samples = np.zeros((len(rows),L // step,data.shape[-1]))
targets = np.zeros((len(rows),))
for j,row in enumerate(rows):
indices = range(rows[j] - L,rows[j],step)
samples[j] = data[indices]
targets[j] = data[rows[j] + D][3] # where is Q in your data
yield samples,targets
data = np.random.standard_normal([256,4])
generator = generator(data=data,L=8,D=1,i_min=0,i_max=255,step=1)
当我执行 izip(*(generator[i] for i in xrange(len(generator))))
时,出现此错误:object of type 'generator' has no len()
。
我已经尝试将 xrange(len(generator))
替换为 len(list(generator))
、enumerate(generator)
,但均无效。我该如何解决这个问题?谢谢。
PS:我在 osx 10.13.6 上使用 python 3.8
。
更新: 根据@couka 的回答,我尝试制作类生成器,但仍然无法正常工作。
class batch_gen:
def __init__(self,data,min_index,max_index,shuffle,batch_size,step):
self.data = data
self.L = L
self.D = D
self.min_index = min_index
self.max_index = max_index
self.shuffle = shuffle
self.batch_size = batch_size
self.step = step
def __iter__(self):
if self.max_index is None:
self.max_index = len(self.data) - self.D - 1
i = self.min_index + self.L
while 1:
if self.shuffle:
rows = np.random.randint(self.min_index + self.L,self.max_index,size=self.batch_size)
else:
if i + self.batch_size >= self.max_index:
i = self.min_index + self.L
rows = np.arange(i,min(i + self.batch_size,self.max_index))
i += len(rows)
samples = np.zeros((len(rows),self.L // self.step,self.data.shape[-1]))
targets = np.zeros((len(rows),))
for j,row in enumerate(rows):
indices = range(rows[j] - self.L,self.step)
samples[j] = self.data[indices]
targets[j] = self.data[rows[j] + self.D][3] # where is Q in your data
yield samples,targets
def __len__(self):
return int(math.floor(len(self.data) / float(self.batch_size)))
当我使用时:
gen_tr = batch_gen(data=data,L=L,D=D,min_index=min(ind_tr),max_index=max(ind_tr),shuffle=True,step=step,batch_size=batch_size)
我收到了这个错误:TypeError: 'batch_gen' object is not subscriptable
。
解决方法
首先,generator
应该是一个类,而不是一个方法。
那么,object of type 'generator' has no len()
表示您的类 generator
没有方法 __len__(self)
。所以你需要添加它。 Afaik 该方法应该返回数据集中的批次数。
它可能看起来像这样:
def __len__(self):
return int(math.floor(len(self.data) / float(self.batch_size)))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。