我一直在使用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库进行批量预测?
ordered
参数没有文档说明,你知道它的作用吗? - pmbaumgartnerlearn.pred_batch(ds_type=DatasetType.Test)
会在保持可迭代序列的顺序的同时给出结果。请注意,我正在使用FastAI版本1.0.61。 - Sagar Dawdalearn.data
了。谢谢。 - Prashant Saraswat