【问题标题】:Hugging Face: NameError: name 'sentences' is not defined拥抱脸:NameError:未定义名称“句子”
【发布时间】:2021-08-30 12:02:59
【问题描述】:

我在这里关注本教程: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

【问题讨论】:

    标签: python bert-language-model huggingface-transformers huggingface-tokenizers huggingface-datasets


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 哦,有道理。谢谢你 - 我会看看它。另外,有没有办法使用我自己的数据集做这样的事情?这里:huggingface.co/nlptown/…
      • 您可以传递您已解析为我所描述的类型之一的任何内容,当然可以。您只需要编写一点代码即可将其转换为其中一种类型
      • 好的,谢谢。因此,是否可以使用我的数据集将句子的情绪分类为“快乐”、“悲伤”或“愤怒”?
      【解决方案3】:

      创建一个变量

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

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

      【讨论】:

      • 您好 - 感谢您的回复。这些句子到底是干什么用的?
      • 您好,这些句子用于与您使用的网站不同的教程。在上面的引文中,我从您当前正在关注的教程中得到它。所以只是向你展示你应该添加什么(一个例子)来摆脱错误
      猜你喜欢
      • 2020-06-30
      • 2013-04-23
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2021-04-15
      • 2019-01-26
      • 2021-10-05
      相关资源
      最近更新 更多