TensorBoardCallback exception

I am getting the following exception when trying to use TensorBoardCallback in fastai v2:

AttributeError: ‘Learner’ object has no attribute ‘smooth_loss’

Here is my code and the detailed exception:

learn = Learner(dls, model, cbs=[ParamScheduler(scheds), AnnealedLossCallback(), TensorBoardCallback('logs', trace_model=False)], loss_func=loss_fn, metrics=[BCEMetric(), KLDMetric(), MUMetric(), StdMetric()])
learn = learn.to_fp16()
learn.fit_one_cycle(200)

AttributeError                            Traceback (most recent call last)
c:\work\ml\fastai2\fastai2\learner.py in _with_events(self, f, event_type, ex, final)
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')

c:\work\ml\fastai2\fastai2\learner.py in _do_epoch(self)
    187     def _do_epoch(self):
--> 188         self._do_epoch_train()
    189         self._do_epoch_validate()

c:\work\ml\fastai2\fastai2\learner.py in _do_epoch_train(self)
    179         self.dl = self.dls.train
--> 180         self._with_events(self.all_batches, 'train', CancelTrainException)
    181 

c:\work\ml\fastai2\fastai2\learner.py in _with_events(self, f, event_type, ex, final)
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')

c:\work\ml\fastai2\fastai2\learner.py in all_batches(self)
    160         self.n_iter = len(self.dl)
--> 161         for o in enumerate(self.dl): self.one_batch(*o)
    162 

c:\work\ml\fastai2\fastai2\learner.py in one_batch(self, i, b)
    175         self._split(b)
--> 176         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    177 

c:\work\ml\fastai2\fastai2\learner.py in _with_events(self, f, event_type, ex, final)
    156         except ex: self(f'after_cancel_{event_type}')
--> 157         finally:   self(f'after_{event_type}')        ;final()
    158 

c:\work\ml\fastai2\fastai2\learner.py in __call__(self, event_name)
    132 
--> 133     def __call__(self, event_name): L(event_name).map(self._call_one)
    134 

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in map(self, f, *args, **kwargs)
    382              else f.__getitem__)
--> 383         return self._new(map(g, self))
    384 

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in _new(self, items, *args, **kwargs)
    332     def _xtra(self): return None
--> 333     def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
    334     def __getitem__(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __call__(cls, x, *args, **kwargs)
     46 
---> 47         res = super().__call__(*((x,) + args), **kwargs)
     48         res._newchk = 0

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __init__(self, items, use_list, match, *rest)
    323         if (use_list is not None) or not _is_array(items):
--> 324             items = list(items) if use_list else _listify(items)
    325         if match is not None:

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in _listify(o)
    259     if isinstance(o, str) or _is_array(o): return [o]
--> 260     if is_iter(o): return list(o)
    261     return [o]

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __call__(self, *args, **kwargs)
    225         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 226         return self.fn(*fargs, **kwargs)
    227 

c:\work\ml\fastai2\fastai2\learner.py in _call_one(self, event_name)
    136         assert hasattr(event, event_name), event_name
--> 137         [cb(event_name) for cb in sort_by_run(self.cbs)]
    138 

c:\work\ml\fastai2\fastai2\learner.py in <listcomp>(.0)
    136         assert hasattr(event, event_name), event_name
--> 137         [cb(event_name) for cb in sort_by_run(self.cbs)]
    138 

c:\work\ml\fastai2\fastai2\callback\core.py in __call__(self, event_name)
     43         res = None
---> 44         if self.run and _run: res = getattr(self, event_name, noop)()
     45         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit

c:\work\ml\fastai2\fastai2\callback\tensorboard.py in after_batch(self)
     29     def after_batch(self):
---> 30         self.writer.add_scalar('train_loss', self.smooth_loss, self.train_iter)
     31         for i,h in enumerate(self.opt.hypers):

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __getattr__(self, k)
    239             attr = getattr(self,self._default,None)
--> 240             if attr is not None: return getattr(attr,k)
    241         raise AttributeError(k)

AttributeError: 'Learner' object has no attribute 'smooth_loss'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-14-625883747759> in <module>
----> 1 learn.fit_one_cycle(200)

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\utils.py in _f(*args, **kwargs)
    450         init_args.update(log)
    451         setattr(inst, 'init_args', init_args)
--> 452         return inst if to_return else f(*args, **kwargs)
    453     return _f
    454 

c:\work\ml\fastai2\fastai2\callback\schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    111     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    112               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 113     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    114 
    115 # Cell

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\utils.py in _f(*args, **kwargs)
    450         init_args.update(log)
    451         setattr(inst, 'init_args', init_args)
--> 452         return inst if to_return else f(*args, **kwargs)
    453     return _f
    454 

c:\work\ml\fastai2\fastai2\learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    202             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    203             self.n_epoch,self.loss = n_epoch,tensor(0.)
--> 204             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    205 
    206     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

c:\work\ml\fastai2\fastai2\learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

c:\work\ml\fastai2\fastai2\learner.py in _do_fit(self)
    192         for epoch in range(self.n_epoch):
    193             self.epoch=epoch
--> 194             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    195 
    196     @log_args(but='cbs')

c:\work\ml\fastai2\fastai2\learner.py in _with_events(self, f, event_type, ex, final)
    155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
--> 157         finally:   self(f'after_{event_type}')        ;final()
    158 
    159     def all_batches(self):

c:\work\ml\fastai2\fastai2\learner.py in __call__(self, event_name)
    131     def ordered_cbs(self, event): return [cb for cb in sort_by_run(self.cbs) if hasattr(cb, event)]
    132 
--> 133     def __call__(self, event_name): L(event_name).map(self._call_one)
    134 
    135     def _call_one(self, event_name):

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in map(self, f, *args, **kwargs)
    381              else f.format if isinstance(f,str)
    382              else f.__getitem__)
--> 383         return self._new(map(g, self))
    384 
    385     def filter(self, f, negate=False, **kwargs):

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in _new(self, items, *args, **kwargs)
    331     @property
    332     def _xtra(self): return None
--> 333     def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
    334     def __getitem__(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)
    335     def copy(self): return self._new(self.items.copy())

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __call__(cls, x, *args, **kwargs)
     45             return x
     46 
---> 47         res = super().__call__(*((x,) + args), **kwargs)
     48         res._newchk = 0
     49         return res

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __init__(self, items, use_list, match, *rest)
    322         if items is None: items = []
    323         if (use_list is not None) or not _is_array(items):
--> 324             items = list(items) if use_list else _listify(items)
    325         if match is not None:
    326             if is_coll(match): match = len(match)

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in _listify(o)
    258     if isinstance(o, list): return o
    259     if isinstance(o, str) or _is_array(o): return [o]
--> 260     if is_iter(o): return list(o)
    261     return [o]
    262 

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __call__(self, *args, **kwargs)
    224             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    225         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 226         return self.fn(*fargs, **kwargs)
    227 
    228 # Cell

c:\work\ml\fastai2\fastai2\learner.py in _call_one(self, event_name)
    135     def _call_one(self, event_name):
    136         assert hasattr(event, event_name), event_name
--> 137         [cb(event_name) for cb in sort_by_run(self.cbs)]
    138 
    139     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

c:\work\ml\fastai2\fastai2\learner.py in <listcomp>(.0)
    135     def _call_one(self, event_name):
    136         assert hasattr(event, event_name), event_name
--> 137         [cb(event_name) for cb in sort_by_run(self.cbs)]
    138 
    139     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

c:\work\ml\fastai2\fastai2\callback\core.py in __call__(self, event_name)
     42                (self.run_valid and not getattr(self, 'training', False)))
     43         res = None
---> 44         if self.run and _run: res = getattr(self, event_name, noop)()
     45         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     46         return res

c:\work\ml\fastai2\fastai2\callback\tensorboard.py in after_epoch(self)
     37         if self.log_preds:
     38             b = self.dls.valid.one_batch()
---> 39             self.learn.one_batch(0, b)
     40             preds = getattr(self.loss_func, 'activation', noop)(self.pred)
     41             out = getattr(self.loss_func, 'decodes', noop)(preds)

c:\work\ml\fastai2\fastai2\learner.py in one_batch(self, i, b)
    174         self.iter = i
    175         self._split(b)
--> 176         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    177 
    178     def _do_epoch_train(self):

c:\work\ml\fastai2\fastai2\learner.py in _with_events(self, f, event_type, ex, final)
    155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
--> 157         finally:   self(f'after_{event_type}')        ;final()
    158 
    159     def all_batches(self):

c:\work\ml\fastai2\fastai2\learner.py in __call__(self, event_name)
    131     def ordered_cbs(self, event): return [cb for cb in sort_by_run(self.cbs) if hasattr(cb, event)]
    132 
--> 133     def __call__(self, event_name): L(event_name).map(self._call_one)
    134 
    135     def _call_one(self, event_name):

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in map(self, f, *args, **kwargs)
    381              else f.format if isinstance(f,str)
    382              else f.__getitem__)
--> 383         return self._new(map(g, self))
    384 
    385     def filter(self, f, negate=False, **kwargs):

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in _new(self, items, *args, **kwargs)
    331     @property
    332     def _xtra(self): return None
--> 333     def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
    334     def __getitem__(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)
    335     def copy(self): return self._new(self.items.copy())

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __call__(cls, x, *args, **kwargs)
     45             return x
     46 
---> 47         res = super().__call__(*((x,) + args), **kwargs)
     48         res._newchk = 0
     49         return res

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __init__(self, items, use_list, match, *rest)
    322         if items is None: items = []
    323         if (use_list is not None) or not _is_array(items):
--> 324             items = list(items) if use_list else _listify(items)
    325         if match is not None:
    326             if is_coll(match): match = len(match)

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in _listify(o)
    258     if isinstance(o, list): return o
    259     if isinstance(o, str) or _is_array(o): return [o]
--> 260     if is_iter(o): return list(o)
    261     return [o]
    262 

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __call__(self, *args, **kwargs)
    224             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    225         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 226         return self.fn(*fargs, **kwargs)
    227 
    228 # Cell

c:\work\ml\fastai2\fastai2\learner.py in _call_one(self, event_name)
    135     def _call_one(self, event_name):
    136         assert hasattr(event, event_name), event_name
--> 137         [cb(event_name) for cb in sort_by_run(self.cbs)]
    138 
    139     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

c:\work\ml\fastai2\fastai2\learner.py in <listcomp>(.0)
    135     def _call_one(self, event_name):
    136         assert hasattr(event, event_name), event_name
--> 137         [cb(event_name) for cb in sort_by_run(self.cbs)]
    138 
    139     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

c:\work\ml\fastai2\fastai2\callback\core.py in __call__(self, event_name)
     42                (self.run_valid and not getattr(self, 'training', False)))
     43         res = None
---> 44         if self.run and _run: res = getattr(self, event_name, noop)()
     45         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     46         return res

c:\work\ml\fastai2\fastai2\callback\tensorboard.py in after_batch(self)
     28 
     29     def after_batch(self):
---> 30         self.writer.add_scalar('train_loss', self.smooth_loss, self.train_iter)
     31         for i,h in enumerate(self.opt.hypers):
     32             for k,v in h.items(): self.writer.add_scalar(f'{k}_{i}', v, self.train_iter)

~\Anaconda3\envs\fastai2\lib\site-packages\fastcore\foundation.py in __getattr__(self, k)
    238         if self._component_attr_filter(k):
    239             attr = getattr(self,self._default,None)
--> 240             if attr is not None: return getattr(attr,k)
    241         raise AttributeError(k)
    242     def __dir__(self): return custom_dir(self,self._dir())

AttributeError: 'Learner' object has no attribute 'smooth_loss'

If I change

self.writer.add_scalar('train_loss', self.smooth_loss, self.train_iter)

to

self.writer.add_scalar('train_loss', self.loss, self.train_iter)

The problem is gone. Tensorboard does smoothing automatically.

2 Likes