Текстовый классификатор Fastai: пакетный прогноз на невидимые данные
Я работаю с текстовым классификатором 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, не создавая тестовый набор данных?
0 ответов
Вы, безусловно, можете. Вот пример кода для этого
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)