TypeError: object of type 'LabelLists' has no len()

I’m trying to load a learner which was exported and run it against a test set. This is my code:

test_src = (TextList.from_df(df, path, cols='texts')
            .split_by_rand_pct(0.1, seed=42)
            .label_from_df(cols='recommend'))

learn_fwd = load_learner(path + '/fwd_learn_c', 
                         test=test_src) #, tfm_y=False)
learn_bwd = load_learner(path + '/bwd_learn_c',
                         test=test_src) #, tfm_y=test_src)

pred_fwd,lbl_fwd = learn_fwd.get_preds(ds_type=DatasetType.Test,ordered=True) 

And I got the following error, what did I miss?


TypeError Traceback (most recent call last)
in
6
7 learn_fwd = load_learner(path + ‘/fwd_learn_c’,
----> 8 test=test_src) #, tfm_y=False)
9 learn_bwd = load_learner(path + ‘/bwd_learn_c’,
10 test=test_src) #, tfm_y=test_src)

~/miniconda3/lib/python3.7/site-packages/fastai/basic_train.py in load_learner(path, file, test, tfm_y, **db_kwargs)
622 model = state.pop(‘model’)
623 src = LabelLists.load_state(path, state.pop(‘data’))
–> 624 if test is not None: src.add_test(test, tfm_y=tfm_y)
625 data = src.databunch(**db_kwargs)
626 cb_state = state.pop(‘cb_state’)

~/miniconda3/lib/python3.7/site-packages/fastai/data_block.py in add_test(self, items, label, tfms, tfm_y)
562 “Add test set containing items with an arbitrary label.”
563 # if no label passed, use label of first training item
–> 564 if label is None: labels = EmptyLabelList([0] * len(items))
565 else: labels = self.valid.y.new([label] * len(items)).process()
566 if isinstance(items, MixedItemList): items = self.valid.x.new(items.item_lists, inner_df=items.inner_df).process()

TypeError: object of type ‘LabelLists’ has no len()

It seems that for the test set, it just accepts an ItemList (without lables). In the above example, I passed a LabelList to it which is the source of error. Anyway to get the accuracy for a test set I found validate and I had to create a TextDataBunch for my test set as following:

test_src = (TextList.from_df(df, path, cols='texts')
            .split_by_rand_pct(0.1, seed=42)
            .label_from_df(cols='recommend')
            .databunch())

learn_fwd = load_learner(path + '/fwd_learn_c')
learn_fwd.validate(test_src.valid_dl)

That won’t work exactly, as you’re still missing 10% of your data in the validation.

See here for how to do so: https://github.com/muellerzr/fastai-Experiments-and-tips/blob/master/Test%20Set%20Generation/Labeled_Test_Set.ipynb

1 Like