Fastai文本分类器:对未见数据进行批处理预测

3

我一直在使用fastai的文本分类器 (https://docs.fast.ai/text.html)。目前,我根据以下方式预测未知语句的情感(正面或负面):

def _unpack_prediction(self, text) -> Tuple[bool, float]:
    out = self._model.predict(text)
    return str(out[0]) == "positive", max(out[2][0].item(), out[2][1].item())

def example(self, messages: Sequence[str]):
    results = map(self._unpack_prediction, messages)
    for phrase, out in zip(messages, results):
        print(f"{phrase[:100]}...[{'pos' if out[0] else 'neg'}] - [{out[1]:.2f}]")

给定一组短语:

("I love this movie",
  "The actors are good, but this movie is definitely stupid",
  "There is no plot at all!!! Just special effects ")

结果如下:

I love this movie...[pos] - [1.00]
The actors are good, but this movie is definitely stupid...[neg] - [0.96]
There is no plot at all!!! Just special effects ...[neg] - [0.95]

然而,将预测逐个应用于短语是相当缓慢的。

有没有一种方法可以在不创建测试数据集的情况下使用fastai库进行批量预测?

1个回答

8
当然可以。这里是样例代码来实现它。
test_df = pd.read_csv(path_to_test_csv_file)
learn.data.add_test(test_df[target_col_name])
prob_preds = learn.get_preds(ds_type=DatasetType.Test, ordered=True)


谢谢!顺便提一下,我之前使用的是fastai 1.0.42版本,但为了让这个工作正常运行,我不得不升级到最新版本。 - Paolo Irrera
ordered参数没有文档说明,你知道它的作用吗? - pmbaumgartner
learn.pred_batch(ds_type=DatasetType.Test)会在保持可迭代序列的顺序的同时给出结果。请注意,我正在使用FastAI版本1.0.61。 - Sagar Dawda
你能更新一下适用于fastai 2.0的答案吗?现在没有learn.data了。谢谢。 - Prashant Saraswat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接