Following Chapter 9 though with classification problem gives RuntimeError

I’m following chapter 9, though using my own data as a starting point to continue working from. I’m getting RuntimeError running either learn.lr_find or learn.fit_one_cycle. As a test I changed my target variable to one of the continuous input variables and then it works without issue.

Is there something easy I’m missing to get this to work?

dep_var = 'Categorical Column'

splits = RandomSplitter()(range_of(df))
cont_nn, cat_nn = cont_cat_split(df, 1, dep_var=dep_var)

procs_nn = [Categorify, FillMissing, Normalize]
to_nn = TabularPandas(df, procs_nn, cat_nn, cont_nn,
                      splits=splits, y_names=dep_var)


dls = to_nn.dataloaders(1024 , num_workers=0)

learn = tabular_learner(dls, layers=[500,250],
                        n_out=1, loss_func=F.mse_loss)
                    
learn.lr_find()

learn.fit_one_cycle(5, 1e-2)

Checking what to_nn.y is gives

29612     2
        ... 
43724     8
Name: Target Column, Length: 67123, dtype: int16

Tried to force the type of to_nn.y to be float but that wasn’t accepted either, and feels really hacky.

to_nn.y = to_nn.y.astype(np.float32)
---------------------------------------------------------------------------
AttributeError           Traceback (most recent call last)
<ipython-input-25-379c687d614f> in <module>
----> 1 to_nn.y = to_nn.y.astype(np.float32)

AttributeError: can't set attribute

Error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-24-0a571837f781> in <module>
----> 9 learn.lr_find()

~\Anaconda3\envs\fastai\lib\site-packages\fastai\callback\schedule.py in lr_find(self, start_lr, end_lr, num_it, stop_div, show_plot, suggestions)
    222     n_epoch = num_it//len(self.dls.train) + 1
    223     cb=LRFinder(start_lr=start_lr, end_lr=end_lr, num_it=num_it, stop_div=stop_div)
--> 224     with self.no_logging(): self.fit(n_epoch, cbs=cb)
    225     if show_plot: self.recorder.plot_lr_find()
    226     if suggestions:

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    204             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    205             self.n_epoch = n_epoch
--> 206             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    207 
    208     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

~\Anaconda3\envs\fastai\lib\site-packages\fastai\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()

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in _do_fit(self)
    195         for epoch in range(self.n_epoch):
    196             self.epoch=epoch
--> 197             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    198 
    199     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

~\Anaconda3\envs\fastai\lib\site-packages\fastai\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()

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in _do_epoch(self)
    189 
    190     def _do_epoch(self):
--> 191         self._do_epoch_train()
    192         self._do_epoch_validate()
    193 

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in _do_epoch_train(self)
    181     def _do_epoch_train(self):
    182         self.dl = self.dls.train
--> 183         self._with_events(self.all_batches, 'train', CancelTrainException)
    184 
    185     def _do_epoch_validate(self, ds_idx=1, dl=None):

~\Anaconda3\envs\fastai\lib\site-packages\fastai\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()

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in all_batches(self)
    159     def all_batches(self):
    160         self.n_iter = len(self.dl)
--> 161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
    163     def _do_one_batch(self):

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in one_batch(self, i, b)
    177         self.iter = i
    178         self._split(b)
--> 179         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    180 
    181     def _do_epoch_train(self):

~\Anaconda3\envs\fastai\lib\site-packages\fastai\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()

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in _do_one_batch(self)
    168         if not self.training or not len(self.yb): return
    169         self('before_backward')
--> 170         self._backward()
    171         self('after_backward')
    172         self._step()

~\Anaconda3\envs\fastai\lib\site-packages\fastai\learner.py in _backward(self)
    150 
    151     def _step(self): self.opt.step()
--> 152     def _backward(self): self.loss.backward()
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):

~\Anaconda3\envs\fastai\lib\site-packages\torch\tensor.py in backward(self, gradient, retain_graph, create_graph)
    219                 retain_graph=retain_graph,
    220                 create_graph=create_graph)
--> 221         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    222 
    223     def register_hook(self, hook):

~\Anaconda3\envs\fastai\lib\site-packages\torch\autograd\__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
    128         retain_graph = create_graph
    129 
--> 130     Variable._execution_engine.run_backward(
    131         tensors, grad_tensors_, retain_graph, create_graph,
    132         allow_unreachable=True)  # allow_unreachable flag

RuntimeError: Found dtype Short but expected Float