Python 如何在fast ai中获得给定测试集的预测和计算精度?

Python 如何在fast ai中获得给定测试集的预测和计算精度?,python,neural-network,fast-ai,Python,Neural Network,Fast Ai,我正在尝试加载一个由learn.export()导出的学习器,我想在测试集上运行它。我希望我的测试集有标签,这样我就可以测量它的准确性 这是我的代码: 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

我正在尝试加载一个由
learn.export()
导出的学习器,我想在测试集上运行它。我希望我的测试集有标签,这样我就可以测量它的准确性

这是我的代码:

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)


pred_fwd,lbl_fwd = learn_fwd.get_preds(ds_type=DatasetType.Test,ordered=True) 
accuracy(pred_fwd, lbl_fwd)
我得到了以下错误,它显然不接受带标签的数据集

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-7f52f2136d8e> in <module>
      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()
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
6.
7 learn_fwd=加载学习者(路径+'/fwd_learn_c',
---->8测试=测试(src)#,tfm_y=错误)
9 learn_bwd=加载学习者(路径+'/bwd_learn_c',
10测试=测试(src),tfm(y=测试(src)
~/miniconda3/lib/python3.7/site-packages/fastai/basic\u train.py in load\u learner(路径、文件、测试、tfm\u y、**db\u kwargs)
622 model=state.pop('model')
623 src=LabelLists.load_state(路径,state.pop('data'))
-->624如果测试不是无:src.add_测试(测试,tfm_y=tfm_y)
625数据=src.databunch(**db_kwargs)
626 cb_state=state.pop('cb_state'))
添加测试中的~/miniconda3/lib/python3.7/site-packages/fastai/data\u block.py(self、items、label、tfms、tfm\u y)
562“添加包含带有任意“标签”的“项”的测试集。”
563#如果没有通过标签,则使用第一个培训项目的标签
-->564如果标签为无:标签=EmptyLabelist([0]*len(项目))
565其他:labels=self.valid.y.new([label]*len(items)).process()
566如果isinstance(items,MixedItemList):items=self.valid.x.new(items.item\u list,inner\u df=items.inner\u df.process())
TypeError:“LabelLists”类型的对象没有len()

对于测试集,它似乎只接受一个ItemList(没有标签)。在上面的示例中,我向它传递了一个标签列表,这是错误的来源。无论如何,为了获得测试集的精度,我找到了以下解决方案:

# Create your test set:
data_test = (TextList.from_df(df, path, cols='texts')
            .split_by_rand_pct(0.1, seed=42)
            .label_from_df(cols='recommend'))

data_test.valid = data_test.train
data_test=data_test.databunch()

# Set the validation set of the learner by the test data you created
learn.data.valid_dl = data_test.valid_dl

# Now y refers to the actual labels in the data set
preds, y = learn.get_preds(ds_type=DatasetType.Valid)
acc = accuracy(preds, y)

# Alternatively you can call validate if you don't want the predictions

acc = learn.validate()[1]