【问题标题】:AttributeError: 'NoneType' object has no attribute 'encode_plus'AttributeError:“NoneType”对象没有属性“encode_plus”
【发布时间】:2021-12-26 18:12:35
【问题描述】:

我正在尝试将 XLNet 模型用于我的情绪分类项目,但收到此错误。

from transformers import XLNetTokenizer, XLNetModel
PRE_TRAINED_MODEL_NAME = 'xlnet-base-cased'
tokenizer = XLNetTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)



input_txt = "India is my country. All Indians are my brothers and sisters"
encodings = tokenizer.encode_plus(input_txt, add_special_tokens=True, max_length=16, return_tensors='pt', return_token_type_ids=False, return_attention_mask=True, pad_to_max_length=False)

AttributeError                            Traceback (most recent call last)
<ipython-input-41-4b204650a45a> in <module>()
      1 input_txt = "India is my country. All Indians are my brothers and sisters"
----> 2 encodings = tokenizer.encode_plus(input_txt, add_special_tokens=True, max_length=16, return_tensors='pt', return_token_type_ids=False, return_attention_mask=True, pad_to_max_length=False)

AttributeError: 'NoneType' object has no attribute 'encode_plus'
SEARCH STACK OVERFLOW

为什么我的分词器被识别为 NoneType?

【问题讨论】:

  • 这个错误很清楚地告诉你tokenizerNone。这意味着XLNetTokenizer.from_pretrained 正在返回None。所以,检查一下,然后试着找出它为什么返回None

标签: python nlp attributeerror huggingface-transformers


【解决方案1】:

我的 XLNet 遇到了类似的问题 - 事实证明,您导入/安装 XLNet 和 SentencePiece 的顺序会有所不同。

在这种情况下,您应该在转换器包之前导入 SentencePiece。这将使python更好地理解tokenizer和SentencePiece之间的关系。

所以你的代码应该是这样的:

!pip install sentencepiece transformers

import sentencepiece
from transformers import XLNetTokenizer, XLNetModel


PRE_TRAINED_MODEL_NAME = 'xlnet-base-cased'
tokenizer = XLNetTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)

input_txt = "India is my country. All Indians are my brothers and sisters"
encodings = tokenizer.encode_plus(input_txt, add_special_tokens=True, max_length=16, return_tensors='pt', return_token_type_ids=False, return_attention_mask=True, pad_to_max_length=False)

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2023-03-16
    • 2018-07-14
    • 2013-06-16
    • 2015-06-15
    相关资源
    最近更新 更多