UnboundLocalError: local variable 'p' referenced before assignment in learn.fit_one_cycle()

I revisited my notebook after a couple of days and got this UnboundLocalError while calling learn.fit_one_cycle(4).


UnboundLocalError Traceback (most recent call last)
in ()
----> 1 learn.fit_one_cycle(4)

4 frames
/usr/local/lib/python3.6/dist-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
21 callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
22 final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
—> 23 learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
24
25 def fit_fc(learn:Learner, tot_epochs:int=1, lr:float=defaults.lr, moms:Tuple[float,float]=(0.95,0.85), start_pct:float=0.72,

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
198 else: self.opt.lr,self.opt.wd = lr,wd
199 callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
–> 200 fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
201
202 def create_opt(self, lr:Floats, wd:Floats=0.)->None:

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)
99 for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
100 xb, yb = cb_handler.on_batch_begin(xb, yb)
–> 101 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
102 if cb_handler.on_batch_end(loss): break
103

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
33 loss,skip_bwd = cb_handler.on_backward_begin(loss)
34 if not skip_bwd: loss.backward()
—> 35 if not cb_handler.on_backward_end(): opt.step()
36 if not cb_handler.on_step_end(): opt.zero_grad()
37

/usr/local/lib/python3.6/dist-packages/fastai/callback.py in step(self)
51 if self.true_wd:
52 for lr,wd,pg1,pg2 in zip(self._lr,self.wd,self.opt.param_groups[::2],self.opt.param_groups[1::2]):
—> 53 if p.grad is None: continue
54 for p in pg1[‘params’]: p.data.mul
(1 - wd*lr)
55 if self.bn_wd:

UnboundLocalError: local variable ‘p’ referenced before assignment

I have tried downgrading PyTorch to 1.4.0 from 1.5.0 and use the latest version of fastai library and searched for it but I couldn’t find as to what is wrong here.

I have the same error when trying to run learn.lr_find()

Have you tried anything to solve it?

Only tried changing batch size and reinstalling fastai and none of it worked. Haven’t changed much in my notebook other than batch size and it worked just fine last night. Looks like the issue is in the callback.py file but honestly not sure how to go about it

Same here, can’t find anything on the forums either.

I think Sylvain inadvertently introduces the error in the last merge (see image-1) today, 5/11. I reverted to Jiahua commit on April 30th (see image 2), and the function .lr_find() works.

(image 1)

(image 2)

1 Like

I am constantly working with my notebook since last 2 hours and learn.fit_one_cycle() is still giving the same error. And I have no clue what to do to fix it.

Abdullah:

did you see my post above?

Do this:

!pip install --upgrade git+https://github.com/fastai/fastai.git@564896d7b84b59bee40db19ee298a1028235442b

@sylviangugger @jeremy

I did see the above post but was unsure which commit to use. Thank you!

Also, the library needs more support and documentation in my humble opinion. These forums rarely cover issues concerning library errors.

It is working thank you!