由于出现上述错误,我无法致电learner.fit
。
特别是,
我正在尝试训练一个采用文本文件并将其转换为LanguageModelData
并将其通过get_model
馈送到RNN的语言模型。
md = LanguageModelData.from_text_files(PATH, TEXT, **FILES, bs=bs, bptt=bptt, min_freq=10)
learner = md.get_model(opt_fn, em_sz, nh, nl,
dropouti=0.05, dropout=0.05, wdrop=0.1, dropoute=0.02, dropouth=0.05)
learner.reg_fn = partial(seq2seq_reg, alpha=2, beta=1)
learner.clip=0.3
learner.fit(3e-3, 4)
ValueErrorTraceback (most recent call last)
<ipython-input-7-579772ee6693> in <module>()
----> 1 learner.fit(3e-3, 4)
/nfsroot/data/home/yeshwanth/misc/fastai/fastai/courses/dl1/practice/fastai/learner.py in fit(self, lrs, n_cycle, wds, **kwargs)
285 self.sched = None
286 layer_opt = self.get_layer_opt(lrs, wds)
--> 287 return self.fit_gen(self.model, self.data, layer_opt, n_cycle, **kwargs)
288
289 def warm_up(self, lr, wds=None):
/nfsroot/data/home/yeshwanth/misc/fastai/fastai/courses/dl1/practice/fastai/learner.py in fit_gen(self, model, data, layer_opt, n_cycle, cycle_len, cycle_mult, cycle_save_name, best_save_name, use_clr, use_clr_beta, metrics, callbacks, use_wd_sched, norm_wds, wds_sched_mult, use_swa, swa_start, swa_eval_freq, **kwargs)
232 metrics=metrics, callbacks=callbacks, reg_fn=self.reg_fn, clip=self.clip, fp16=self.fp16,
233 swa_model=self.swa_model if use_swa else None, swa_start=swa_start,
--> 234 swa_eval_freq=swa_eval_freq, **kwargs)
235
236 def get_layer_groups(self): return self.models.get_layer_groups()
/nfsroot/data/home/yeshwanth/misc/fastai/fastai/courses/dl1/practice/fastai/model.py in fit(model, data, n_epochs, opt, crit, metrics, callbacks, stepper, swa_model, swa_start, swa_eval_freq, **kwargs)
159
160 if not all_val:
--> 161 vals = validate(model_stepper, cur_data.val_dl, metrics, seq_first=seq_first)
162 stop=False
163 for cb in callbacks: stop = stop or cb.on_epoch_end(vals)
/nfsroot/data/home/yeshwanth/misc/fastai/fastai/courses/dl1/practice/fastai/model.py in validate(stepper, dl, metrics, seq_first)
220 stepper.reset(False)
221 with no_grad_context():
--> 222 for (*x,y) in iter(dl):
223 y = VV(y)
224 preds, l = stepper.evaluate(VV(x), y)
/nfsroot/data/home/yeshwanth/misc/fastai/fastai/courses/dl1/practice/fastai/nlp.py in __next__(self)
135
136 def __next__(self):
--> 137 if self.i >= self.n-1 or self.iter>=len(self): raise StopIteration
138 bptt = self.bptt if np.random.random() < 0.95 else self.bptt / 2.
139 seq_len = max(5, int(np.random.normal(bptt, 5)))
ValueError: __len__() should return >= 0
答案 0 :(得分:4)
您的数据似乎位于1个.txt文件中,LanguageModelData.from_text_files()
希望处理包含许多文件的文件夹
UPD:解决了!每个文件夹中至少必须有bs
个数字文件!否则,LanguageModelLoader
的{{1}}的数据为空。
在验证过程中我遇到了同样的错误,问题似乎出在LanguageModelData
构造数据集的方式上:
LanguageModelData()
for (*x, y) in md.trn_dl:
set_trace()
应该是pytorch张量或形状x
,(smth, batch_size)
是大小y
的一维张量。与smth*batch_size
相同。在您的情况下,很可能没有md.val_dl
。数据出了点问题。 (*x, y)
和len(md.trn_dl)
不能等于0。
任何解决方案,我都会感激,谢谢您的提问!
此外,语言模型的较新版本删除了len(md.val_dl)
并使其更易于调试:https://github.com/fastai/fastai/blob/master/courses/dl2/imdb.ipynb