Hugging Face: NameError: name 'sentences' is not defined

Hugging Face: NameError: name 'sentences' is not defined

我在这里学习本教程:https://huggingface.co/transformers/training.html - 虽然我遇到了一个错误,我认为本教程缺少导入,但我不知道是哪个。

这些是我目前的进口:

# Transformers installation
! pip install transformers
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git

! pip install datasets transformers

from transformers import pipeline

当前代码:

from datasets import load_dataset

raw_datasets = load_dataset("imdb")
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
inputs = tokenizer(sentences, padding="max_length", truncation=True)

错误:

NameError                                 Traceback (most recent call last)

<ipython-input-9-5a234f114e2e> in <module>()
----> 1 inputs = tokenizer(sentences, padding="max_length", truncation=True)

NameError: name 'sentences' is not defined

错误表明您在作用域中没有名为 sentences 的变量。我相信本教程假定您已经有了一个句子列表并且正在对其进行标记。

看看 documentation 第一个参数可以是字符串或字符串列表或字符串列表。

__call__(text: Union[str, List[str], List[List[str]]],...)

创建一个变量

sentences = ["Hello I'm a single sentence",
             "And another sentence",
             "And the very very last one"]

“正如我们在 Preprocessing data 中看到的,我们可以使用以下命令为模型准备文本输入(这是一个示例,不是您可以执行的命令)”

这个错误是因为你没有声明句子。现在你需要 使用以下方式访问原始数据:

k = raw_datasets['train']
sentences = k['text']