AttributeError: 'TabularList' object has no attribute 'codes'

(Rushabh Vasani) #1

Hello everyone !
I am trying to make a regressive model and I have trained it also.
It finely predicts on the validation set.But when it comes to test set it gives the error "AttributeError: ‘TabularList’ object has no attribute ‘codes’ ".
And when I once saved my model with learn.save(‘Stage-1’) and tried to load it again with learn.load(‘Stage-1’) it gives the same error.
Updating the version of fastai didn’t work for me.
Any help will be appreciated.
Thank you ! :slight_smile:

0 Likes

#2

We can’t help you without seeing

  1. your code
  2. the full stack error trace
0 Likes

(Rushabh Vasani) #3

Thanks for responding ! @sgugger

This is my entire code !!

====================================================
%load_ext autoreload
%autoreload 2
%matplotlib inline

from zipfile import ZipFile
file_name=“models.zip”
with ZipFile(file_name,‘r’) as zip:
zip.extractall()
print(‘Done’)

import pandas as pd

Data=pd.read_csv(‘train.csv’)

T_Data=pd.read_csv(‘test.csv’)

from fastai.tabular import *

dep_var=‘SalePrice’
cat_var=[‘MSZoning’,‘Street’,‘Alley’,‘LotShape’,‘LandContour’,‘Utilities’,‘LotConfig’,
‘LandSlope’,‘Neighborhood’,‘Condition1’,‘Condition2’,‘BldgType’,
‘HouseStyle’, ‘OverallQual’, ‘OverallCond’,
‘RoofStyle’, ‘RoofMatl’, ‘Exterior1st’, ‘Exterior2nd’, ‘MasVnrType’,
‘ExterQual’, ‘ExterCond’, ‘Foundation’, ‘BsmtQual’,
‘BsmtCond’, ‘BsmtExposure’, ‘BsmtFinType1’, ‘BsmtFinSF1’,
‘BsmtFinType2’, ‘BsmtFinSF2’, ‘BsmtUnfSF’, ‘TotalBsmtSF’, ‘Heating’,
‘HeatingQC’, ‘CentralAir’, ‘Electrical’, ‘1stFlrSF’, ‘2ndFlrSF’,
‘LowQualFinSF’, ‘BsmtFullBath’, ‘BsmtHalfBath’, ‘FullBath’,
‘HalfBath’, ‘BedroomAbvGr’, ‘KitchenAbvGr’, ‘KitchenQual’,
‘TotRmsAbvGrd’, ‘Functional’, ‘Fireplaces’, ‘FireplaceQu’, ‘GarageType’,
‘GarageFinish’, ‘GarageCars’, ‘GarageQual’,
‘GarageCond’, ‘PavedDrive’, ‘WoodDeckSF’, ‘OpenPorchSF’,
‘EnclosedPorch’, ‘3SsnPorch’, ‘ScreenPorch’, ‘PoolArea’, ‘PoolQC’,
‘Fence’, ‘MiscFeature’, ‘MiscVal’, ‘MoSold’, ‘SaleType’,
‘SaleCondition’]

T_Data.GarageArea=T_Data.GarageArea.fillna(value=0)

DataB=(TabularList.from_df(Data, path=’’, cat_names=cat_var, procs=[FillMissing,Categorify,Normalize])
.split_by_idx(range(1400,1460))
.label_from_df(cols=dep_var, label_cls=FloatList, log=True)
.add_test(T_Data)
.databunch())

learn=tabular_learner(DataB,layers=[200,100,1])
learn.loss_func=rmse

learn.load(‘Stage-1’)

learn.get_preds(DatasetType.Test)[1]

=============================================================

This is my code I had already trained it so I just loaded the model and tried to predict.
It gives the error when I try to load it but at the same time it perfectly predicts on the validation set after giving that error also.But while predicting on the Test set it gives the same error.

==========================================================
This is the error for get_preds(DatasetType.Test)

AttributeError Traceback (most recent call last)
in ()
----> 1 learn.get_preds(DatasetType.Test)[1]

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in get_preds(self, ds_type, with_loss, n_batch, pbar)
276 lf = self.loss_func if with_loss else None
277 return get_preds(self.model, self.dl(ds_type), cb_handler=CallbackHandler(self.callbacks),
–> 278 activ=_loss_func2activ(self.loss_func), loss_func=lf, n_batch=n_batch, pbar=pbar)
279
280 def pred_batch(self, ds_type:DatasetType=DatasetType.Valid, batch:Tuple=None, reconstruct:bool=False) -> List[Tensor]:

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in get_preds(model, dl, pbar, cb_handler, activ, loss_func, n_batch)
38 “Tuple of predictions and targets, and optional losses (if loss_func) using dl, max batches n_batch.”
39 res = [torch.cat(o).cpu() for o in
—> 40 zip(*validate(model, dl, cb_handler=cb_handler, pbar=pbar, average=False, n_batch=n_batch))]
41 if loss_func is not None: res.append(calc_loss(res[0], res[1], loss_func))
42 if activ is not None: res[0] = activ(res[0])

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)
50 val_losses,nums = [],[]
51 if cb_handler: cb_handler.set_dl(dl)
—> 52 for xb,yb in progress_bar(dl, parent=pbar, leave=(pbar is not None)):
53 if cb_handler: xb, yb = cb_handler.on_batch_begin(xb, yb, train=False)
54 val_losses.append(loss_batch(model, xb, yb, loss_func, cb_handler=cb_handler))

/usr/local/lib/python3.6/dist-packages/fastprogress/fastprogress.py in iter(self)
64 self.update(0)
65 try:
—> 66 for i,o in enumerate(self._gen):
67 yield o
68 if self.auto_update: self.update(i+1)

/usr/local/lib/python3.6/dist-packages/fastai/basic_data.py in iter(self)
73 def iter(self):
74 “Process and returns items from DataLoader.”
—> 75 for b in self.dl: yield self.proc_batch(b)
76
77 @classmethod

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in next(self)
635 self.reorder_dict[idx] = batch
636 continue
–> 637 return self._process_next_batch(batch)
638
639 next = next # Python 2 compatibility

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
656 self._put_indices()
657 if isinstance(batch, ExceptionWrapper):
–> 658 raise batch.exc_type(batch.exc_msg)
659 return batch
660

AttributeError: Traceback (most recent call last):
File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 138, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 138, in
samples = collate_fn([dataset[i] for i in batch_indices])
File “/usr/local/lib/python3.6/dist-packages/fastai/data_block.py”, line 595, in getitem
if self.item is None: x,y = self.x[idxs],self.y[idxs]
File “/usr/local/lib/python3.6/dist-packages/fastai/data_block.py”, line 104, in getitem
if isinstance(idxs, Integral): return self.get(idxs)
File “/usr/local/lib/python3.6/dist-packages/fastai/tabular/data.py”, line 125, in get
codes = [] if self.codes is None else self.codes[o]
AttributeError: ‘TabularList’ object has no attribute ‘codes’

=================================================

=================================================

And this is the error for learn.load(‘Stage-1’)

AttributeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/IPython/core/formatters.py in call(self, obj)
697 type_pprinters=self.type_printers,
698 deferred_pprinters=self.deferred_printers)
–> 699 printer.pretty(obj)
700 printer.flush()
701 return stream.getvalue()

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in pretty(self, obj)
396 if callable(meth):
397 return meth(obj, self, cycle)
–> 398 return _default_pprint(obj, self, cycle)
399 finally:
400 self.end_group()

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
516 if _safe_getattr(klass, ‘repr’, None) not in baseclass_reprs:
517 # A user-provided repr. Find newlines and replace them with p.break
()
–> 518 _repr_pprint(obj, p, cycle)
519 return
520 p.begin_group(1, ‘<’)

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in repr_pprint(obj, p, cycle)
707 “”“A pprint that just redirects to the normal repr function.”""
708 # Find newlines and replace them with p.break
()
–> 709 output = repr(obj)
710 for idx,output_line in enumerate(output.splitlines()):
711 if idx:

/usr/local/lib/python3.6/dist-packages/dataclasses.py in repr(self)

/usr/local/lib/python3.6/dist-packages/fastai/basic_data.py in repr(self)
101
102 def repr(self)->str:
–> 103 return f’{self.class.name};\n\nTrain: {self.train_ds};\n\nValid: {self.valid_ds};\n\nTest: {self.test_ds}’
104
105 @staticmethod

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in repr(self)
558
559 def repr(self)->str:
–> 560 items = [self[i] for i in range(min(5,len(self.items)))]
561 res = f’{self.class.name} ({len(self.items)} items)\n’
562 res += f’x: {self.x.class.name}\n{show_some([i[0] for i in items])}\n’

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in (.0)
558
559 def repr(self)->str:
–> 560 items = [self[i] for i in range(min(5,len(self.items)))]
561 res = f’{self.class.name} ({len(self.items)} items)\n’
562 res += f’x: {self.x.class.name}\n{show_some([i[0] for i in items])}\n’

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in getitem(self, idxs)
593 idxs = try_int(idxs)
594 if isinstance(idxs, Integral):
–> 595 if self.item is None: x,y = self.x[idxs],self.y[idxs]
596 else: x,y = self.item ,0
597 if self.tfms or self.tfmargs:

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in getitem(self, idxs)
102 def getitem(self,idxs:int)->Any:
103 idxs = try_int(idxs)
–> 104 if isinstance(idxs, Integral): return self.get(idxs)
105 else: return self.new(self.items[idxs], xtra=index_row(self.xtra, idxs))
106

/usr/local/lib/python3.6/dist-packages/fastai/tabular/data.py in get(self, o)
123 def get(self, o):
124 if not self.preprocessed: return self.xtra.iloc[o] if hasattr(self, ‘xtra’) else self.items[o]
–> 125 codes = [] if self.codes is None else self.codes[o]
126 conts = [] if self.conts is None else self.conts[o]
127 return self._item_cls(codes, conts, self.classes, self.col_names)

AttributeError: ‘TabularList’ object has no attribute ‘codes’

=======================================================

0 Likes

#4

You can’t use add_test with your dataframe directly, you need to pass another TabularList (that you will create from this dataframe):

add_test(TabularList.from_df(T_Data, cat_names=cat_var))

Also, you should less the processors handle the na in the test dataframe, you may be filling with a different value.

1 Like

(Rushabh Vasani) #5

Thank you so much brother !! :smile:
It was bothering me from last 24 hours.

0 Likes