Hi All. I am using tabular module to do some classification task. Please see my code so far
# Get the weights of classes
class_count_df = df_HR_train.groupby('Attrition_Voluntary').count()
class_count_df
n_0, n_1 = class_count_df.iloc[0, 0], class_count_df.iloc[1, 0]
w_0 = (n_0 + n_1) / (2.0 * n_0)
w_1 = (n_0 + n_1) / (2.0 * n_1)
w_0, w_1
# Convert to category column
df_HR_train['Attrition_Voluntary'] = df_HR_train['Attrition_Voluntary'].astype('category')
df_HR_train.info()
dls = TabularDataLoaders.from_df(df_HR_train, y_names='Attrition_Voluntary', y_block=CategoryBlock,
cat_names=CAT_NAMES,
cont_names=CONT_NAMES,
procs=[Categorify, FillMissing, Normalize],
splits=TrainTestSplitter(test_size = 0.2, stratify=df_HR_train['Attrition_Voluntary'],
random_state = 12))
%%time
weights = [w_0, w_1]
class_weights=torch.FloatTensor(weights).cuda()
loss_func = FocalLossFlat(weight=class_weights)
prec = Precision()
learn = tabular_learner(dls,
layers=[500, 250],
loss_func=loss_func,
metrics=prec)
learn.fit_one_cycle(3)
For some reason this part throws an error.
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
RuntimeError Traceback (most recent call last)
in
----> 1 interp = ClassificationInterpretation.from_learner(learn)
2 interp.plot_confusion_matrix()/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/interpret.py in from_learner(cls, learn, ds_idx, dl, act)
27 “Construct interpretation object from a learner”
28 if dl is None: dl = learn.dls[ds_idx].new(shuffled=False, drop_last=False)
—> 29 return cls(dl, *learn.get_preds(dl=dl, with_input=True, with_loss=True, with_decoded=True, act=None))
30
31 def top_losses(self, k=None, largest=True):/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in get_preds(self, ds_idx, dl, with_input, with_decoded, with_loss, act, inner, reorder, cbs, **kwargs)
251 if with_loss: ctx_mgrs.append(self.loss_not_reduced())
252 with ContextManagers(ctx_mgrs):
→ 253 self._do_epoch_validate(dl=dl)
254 if act is None: act = getattr(self.loss_func, ‘activation’, noop)
255 res = cb.all_tensors()/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in _do_epoch_validate(self, ds_idx, dl)
201 if dl is None: dl = self.dls[ds_idx]
202 self.dl = dl
→ 203 with torch.no_grad(): self._with_events(self.all_batches, ‘validate’, CancelValidException)
204
205 def _do_epoch(self):/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before{event_type}’); f()
164 except ex: self(f’after_cancel{event_type}’)
165 self(f’after_{event_type}’); final()/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in all_batches(self)
167 def all_batches(self):
168 self.n_iter = len(self.dl)
→ 169 for o in enumerate(self.dl): self.one_batch(*o)
170
171 def _do_one_batch(self):/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in one_batch(self, i, b)
192 b = self._set_device(b)
193 self._split(b)
→ 194 self._with_events(self._do_one_batch, ‘batch’, CancelBatchException)
195
196 def _do_epoch_train(self):/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
163 try: self(f’before{event_type}’); f()
164 except ex: self(f’after_cancel_{event_type}’)
→ 165 self(f’after_{event_type}’); final()
166
167 def all_batches(self):/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in call(self, event_name)
139
140 def ordered_cbs(self, event): return [cb for cb in self.cbs.sorted(‘order’) if hasattr(cb, event)]
→ 141 def call(self, event_name): L(event_name).map(self._call_one)
142
143 def _call_one(self, event_name):/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastcore/foundation.py in map(self, f, gen, *args, **kwargs)
152 def range(cls, a, b=None, step=None): return cls(range_of(a, b=b, step=step))
153
→ 154 def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))
155 def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))
156 def filter(self, f=noop, negate=False, gen=False, **kwargs):/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastcore/basics.py in map_ex(iterable, f, gen, *args, **kwargs)
664 res = map(g, iterable)
665 if gen: return res
→ 666 return list(res)
667
668 # Cell/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastcore/basics.py in call(self, *args, **kwargs)
649 if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
650 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
→ 651 return self.func(*fargs, **kwargs)
652
653 # Cell/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/learner.py in _call_one(self, event_name)
143 def _call_one(self, event_name):
144 if not hasattr(event, event_name): raise Exception(f’missing {event_name}’)
→ 145 for cb in self.cbs.sorted(‘order’): cb(event_name)
146
147 def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/callback/core.py in call(self, event_name)
43 (self.run_valid and not getattr(self, ‘training’, False)))
44 res = None
—> 45 if self.run and _run: res = getattr(self, event_name, noop)()
46 if event_name==‘after_fit’: self.run=True #Reset self.run to True at each end of fit
47 return res/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/callback/core.py in after_batch(self)
129 if self.with_loss:
130 bs = find_bs(self.yb)
→ 131 loss = self.loss if self.loss.numel() == bs else self.loss.view(bs,-1).mean(1)
132 self.losses.append(self.learn.to_detach(loss))
133/anaconda/envs/azureml_py38/lib/python3.8/site-packages/fastai/torch_core.py in torch_function(self, func, types, args, kwargs)
338 convert=False
339 if _torch_handled(args, self._opt, func): convert,types = type(self),(torch.Tensor,)
→ 340 res = super().torch_function(func, types, args=args, kwargs=kwargs)
341 if convert: res = convert(res)
342 if isinstance(res, TensorBase): res.set_meta(self, as_copy=True)/anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/_tensor.py in torch_function(cls, func, types, args, kwargs)
1021
1022 with _C.DisableTorchFunction():
→ 1023 ret = func(*args, **kwargs)
1024 return _convert(ret, cls)
1025RuntimeError: shape ‘[64, -1]’ is invalid for input of size 1
What is the reason that I cannot print confusion matrix?