TabularDataBunch.from_df doesn't add / acknowledge test set

Using the Titanic Kaggle datasets (train.csv and test.csv), I am trying to do very simple tabular data NN work.
I am following the example from the docs pretty much exactly, but when creating the TabularDataBunch I am using this line:

data = TabularDataBunch.from_df(path, df_out, dep_var, test_ds=test_df, valid_idx=range(100), procs=procs, cat_names=cat_vars)

However, I note that data.test_ds is None. This makes sense, because in the class definition of TabularDataBunch, we have

def from_df(cls, path, df:DataFrame, dep_var:str, valid_idx:Collection[int], procs:OptTabTfms=None,
                cat_names:OptStrList=None, cont_names:OptStrList=None, classes:Collection=None, **kwargs)->DataBunch:
    "Create a `DataBunch` from train/valid/test dataframes."
    cat_names = ifnone(cat_names, [])
    cont_names = ifnone(cont_names, list(set(df)-set(cat_names)-{dep_var}))
    procs = listify(procs)
    return (TabularList.from_df(df, path=path, cat_names=cat_names, cont_names=cont_names, procs=procs)                     
        .label_from_df(cols=dep_var, classes=None)

And, well, test_ds doesn’t show up in any of the arguments, and so it would get passed in kwags, but I note that in the body, kwargs doesn’t get used anywhere.

The result of this is that, further down the road, I can’t do

learn.get_preds(DatasetTypes.Test) because I’ll get an error about NoneType not supporting the action.

1 Like

test_ds isn’t supported in this function, you should use the data block API to add it by adding:

add_test(TabularList.from_df(test_df, path=path, cat_names=cat_names, cont_names=cont_names))

between the label and the databunch line.


That sounds like the library itself needs that modification, right? As in, for someone just using the library without changing its source code, there is no way to get a test set added to the TabularDataBunch?

It mostly sounds like you need to learn the data block API :wink:
The basic factory methods are only there to get started quickly. To make any modification to them, you should use the data block API: just copy-paste the source code and modify the line you need.

Fair enough; I just would have expected, from a design point of view, that loading your train and test data in one go, would be a basic use case necessary to get started quickly, much like the earlier version of used to do it.

The data block api seems very nice indeed, much like that grammar-of-graphics plotting approach.

1 Like

Hi :slight_smile:

I just tried what ‘sgugger’ said about ‘add_test’ but it throws me an error.

Added in the fastai library:

My Code:

Error Message:

Can someone tell me what I did wrong!?

I get it to work like so:

il = (TabularList.from_csv(path,'train.csv',header='infer',cols=cols, cont_names=cont_names, cat_names=cat_names, procs=procs)
      .add_test(TabularList.from_csv(path, 'test.csv')))

Note how add_test does not have to specify any of the procs, cat names, cont vars etc again, because they are inferred from the train set. So try your code again but with add_test stripped to the bare minimum of providing the path or data frame.

1 Like


thanks for your reply and help!
Well, I tried it to pass the bare minimum (a data frame) but
it still does not work :frowning: Same error.

Here it breaks --> self.dl seems to be null/empty

I am working on Titanic and got it to work like so:

        train_df = shuffle(train_df)
        dep_var = 'Survived'
        cat_names = ['Pclass', 'Sex', 'Alone', 'SibCh', 'Embarked'] # 'AgeGroup', 'FareGroup',
        cont_names = ['Age', 'Fare', 'Relatives' ] 
        procs = [FillMissing, Categorify, Normalize]
        test = TabularList.from_df(test_df, cat_names=cat_names, cont_names=cont_names, procs=procs)
        data = (TabularList.from_df(train_df, path='.', cat_names=cat_names, cont_names=cont_names, procs=procs)
                                   .add_test(test, label=0)
        learn = tabular_learner(data, layers=[60,40], emb_szs= emb_szs,  metrics=accuracy) 
        lr = 1e-1
        learn.fit_one_cycle(4, lr)

Code copied from colab (Kaggle only works with cpu and unstable at the moment - for me at least). Some features are made up.

PS: learn.show_results() doesn’t seem to work #
I am trying to find out how to configure embedding (best practices) for categories. I saw examples ignoring emb_szs and others using it. What approximate criteria to use?
Update: found emb_szs hint at and posted Kaggle kernel: (using cpu, torch install takes too much time).

1 Like

Can you share how to called prediction on test set? Does unseen token in test set handle in the embedding.


See full Kaggle kernel above. Not sure about 2nd part (if you mean categories in test set not in train set). In the Titanic case, this is not an issue but I would guess .add_test takes care of that.

1 Like

To clarify, by adding the test set using add_test() as per the code below, is the test set normalized according to the same parameters as the train/valid set? Or should it be added after data has been initialised?

Also, on a possibly related note, following code throws up a warning when the highlighted part is added and I’m not sure how to locate the problematic part.

When you use add_test, the transforms (FillMissing, Normalize and Categorify) always use the state determined on the training set.
You should pass cat_names and cont_names too when you create your TabularList for the test set, this may be why you have the warning.

1 Like

How is the output called and used? From the above approach I believe I have trained columnar data, but am unsure how to verify or apply the results. I have:

test_df = dftest
data = (TabularList.from_df(df, path=path, cat_names=cat_names, cont_names=cont_vars, procs=procs)
.label_from_df(cols=dep_var, label_cls=FloatList, log=True)
.add_test(TabularList.from_df(test_df, path=path, cat_names=cat_names, cont_names=cont_names))

And then I train the learner and try to get preds with:

preds = learn.get_preds(ds_type=‘Test’)

Which gives me:

I am confused about the output. Is this the log of the dependent variable? Have I made a mistake elsewhere?

1 Like

Yes, it’s the log of the dependent variable (since you put log=True), as predicted by your model.

1 Like

Of course! Thank you!

I suppose that the correct way is something as:

data = (TabularList.from_df(train_df, path=path, cat_names=cat_vars,
cont_names=cont_vars+bools_vars, procs=procs)
.label_from_df(cols=dep_var, label_cls=FloatList, log=True)
.add_test(TabularList.from_df(test_df, path=path, cat_names=cat_vars, cont_names=cont_vars, procs=procs))

But in doing so a get the following exception:

Exception: There are nan values in field 'field_name' but there were none in the training set. 
                Please fix those manually.

How to fix?

1 Like

As indicated by the error message, you have nans in your validation or test set in a field where the training set had none (need to fix the error message so that it show the name). You need to fix them manually since the processor state is fixed by the training set, so either introduce some NaNs in your training set in this column, or remove the ones in the validation/test set.

How add labels to test dataset?
If I specify:

data = (TabularList.from_df(train_df, path=path, cat_names=cat_vars,
cont_names=cont_vars, procs=procs)
.label_from_df(cols=dep_var, label_cls=FloatList, log=True)
.add_test(TabularList.from_df(test_df, path=path, cat_names=cat_vars,
cont_names=cont_vars, procs=procs)
.label_from_df(cols=dep_var, label_cls=FloatList, log=True))

I get an error using

data.show_batch(4, ds_type=DatasetType.Test)

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

What’s wrong?
Thanks again.

In fastai the test set is not labelled. It’s to quickly get predictions on a lot of inputs. If you want to validate on a second test, you should create a second data object with this validation test as documented here.

1 Like