首页 > 解决方案 > 使用 tokenizer.encode_plus 的麻烦

问题描述

#jupyter 笔记本

我正在尝试使用https://colab.research.google.com/drive/1pTuQhug6Dhl9XalKB0zUGf4FIdYFlpcX#scrollTo=2bBdb3pt8LuQ研究 BERT 分类器

在那个 colab 中,从“Tokenize all the sentence.....”开始

在那部分,我遇到了麻烦“TypeError:_tokenize()得到了一个意外的关键字参数'pad_to_max_length'”

**
input_ids = []
attention_masks = []

for sent in sentences:
    encoded_dict = tokenizer.encode_plus(
                    sent,                      # Sentence to encode.
                    add_special_tokens = True, # Add '[CLS]' and '[SEP]'
                    max_length = 64,           # Pad & truncate all sentences.
                    pad_to_max_length = True,
                    return_attention_mask = True,   # Construct attn. masks.
                    return_tensors = 'pt',     # Return pytorch tensors.
               )

标签: pythonjupytertransformer

解决方案


参考:本帖

“问题在于 conda 仅在 2.1.1 版(存储库信息)中提供了转换器库,并且此版本没有 pad_to_max_length 参数。”

所以也许最好的选择是卸载然后重新安装转换器(这次使用 pip install 而不是 conda forge)或创建一个新的 conda 环境并安装所有东西(通过 pip 而不是通过 conda)。


推荐阅读