Hi,
I have a private multilabel dataset for text classification. I ran into AttributeError at running learn.loss_func.
Here my dataset looks like:
dls_class_block = DataBlock(
blocks=(TextBlock.from_df('proc_name', res_col_name='proc_name', vocab=dls_lm.vocab),
MultiCategorize),
get_x=ColReader('proc_name'),
get_y=ColReader('cur_job_func', label_delim='|'),
splitter=ColSplitter(col='is_valid')
)
dls_class = dls_class_block.dataloaders(data, bs=5)
dls_class.one_batch()
------------------------------------------------------------------------
(TensorText([[ 2, 26, 11, 32, 48, 15, 337, 29, 32, 88, 85, 45, 140, 155,
85, 45, 140, 337],
[ 2, 11, 13, 9, 61, 209, 95, 16, 95, 16, 310, 280, 95, 16,
201, 1, 1, 1],
[ 2, 11, 32, 26, 298, 30, 100, 0, 0, 30, 55, 297, 298, 0,
0, 1, 1, 1],
[ 2, 26, 11, 32, 11, 96, 46, 20, 97, 52, 300, 345, 64, 1,
1, 1, 1, 1],
[ 2, 15, 31, 9, 84, 80, 10, 12, 31, 159, 254, 108, 103, 1,
1, 1, 1, 1]]),
[('NA', 'NA', 'NA', 'NA', 'InformationTechnology')])
Here is the error detail:
learn = text_classifier_learner(
dls_class,
AWD_LSTM,
drop_mult=0.5,
loss_func=BCEWithLogitsLossFlat(),
metrics=[accuracy_multi, metrics.precision_score, F1ScoreMulti()]
)
x, y = dls_class.one_batch()
pred = learn.model(x)
learn.loss_func(pred, y)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-78-72eba93017a5> in <module>
----> 1 learn.loss_func(pred, y)
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastai/layers.py in __call__(self, inp, targ, **kwargs)
290
291 def __call__(self, inp, targ, **kwargs):
--> 292 inp = inp .transpose(self.axis,-1).contiguous()
293 targ = targ.transpose(self.axis,-1).contiguous()
294 if self.floatify and targ.dtype!=torch.float16: targ = targ.float()
AttributeError: 'tuple' object has no attribute 'transpose'
I have tried other loss function CrossEntropyLossFlat
and the same error. My guess is the underlying data issue. This could also relevant - I got RecursionError when running dls_class.show_batch()
. Here is the message.
dls_class.show_batch()
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-89-6e53cf6f0709> in <module>
----> 1 dls_class.show_batch()
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastai/data/core.py in show_batch(self, b, max_n, ctxs, show, unique, **kwargs)
100 if b is None: b = self.one_batch()
101 if not show: return self._pre_show_batch(b, max_n=max_n)
--> 102 show_batch(*self._pre_show_batch(b, max_n=max_n), ctxs=ctxs, max_n=max_n, **kwargs)
103 if unique: self.get_idxs = old_get_idxs
104
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastai/data/core.py in _pre_show_batch(self, b, max_n)
90 b = self.decode(b)
91 if hasattr(b, 'show'): return b,None,None
---> 92 its = self._decode_batch(b, max_n, full=False)
93 if not is_listy(b): b,its = [b],L((o,) for o in its)
94 return detuplify(b[:self.n_inp]),detuplify(b[self.n_inp:]),its
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastai/data/core.py in _decode_batch(self, b, max_n, full)
84 f = self.after_item.decode
85 f = compose(f, partial(getattr(self.dataset,'decode',noop), full = full))
---> 86 return L(batch_to_samples(b, max_n=max_n)).map(f)
87
88 def _pre_show_batch(self, b, max_n=9):
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastai/torch_core.py in batch_to_samples(b, max_n)
569 if isinstance(b, Tensor): return retain_types(list(b[:max_n]), [b])
570 else:
--> 571 res = L(b).map(partial(batch_to_samples,max_n=max_n))
572 return retain_types(res.zip(), [b])
573
~/.pyenv/versions/3.8.2/lib/python3.8/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):
~/.pyenv/versions/3.8.2/lib/python3.8/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())
~/.pyenv/versions/3.8.2/lib/python3.8/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
~/.pyenv/versions/3.8.2/lib/python3.8/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)
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastcore/foundation.py in _listify(o)
235 if isinstance(o, list): return o
236 if isinstance(o, str) or _is_array(o): return [o]
--> 237 if is_iter(o): return list(o)
238 return [o]
239
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastcore/foundation.py in __call__(self, *args, **kwargs)
298 if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
299 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 300 return self.fn(*fargs, **kwargs)
301
302 # Cell
... last 7 frames repeated, from the frame below ...
~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/fastai/torch_core.py in batch_to_samples(b, max_n)
569 if isinstance(b, Tensor): return retain_types(list(b[:max_n]), [b])
570 else:
--> 571 res = L(b).map(partial(batch_to_samples,max_n=max_n))
572 return retain_types(res.zip(), [b])
573
RecursionError: maximum recursion depth exceeded while calling a Python object
Any suggestion would be appreciate. Thanks!