无法导入BertTokenizer

时间:2020-10-17 18:52:54

标签: python huggingface-transformers

我正在尝试使用变形金刚包的BertTokenizer部分。首先,我按如下所示进行安装。

pip install transformers

说成功了。

当我尝试按以下方式导入软件包的某些部分时,我得到以下信息。

 from transformers import BertTokenizer
Traceback (most recent call last):

  File "<ipython-input-2-89505a24ece6>", line 1, in <module>
    from transformers import BertTokenizer

  File "C:\Users\User\anaconda3\lib\site-packages\transformers\__init__.py", line 22, in <module>
    from .integrations import (  # isort:skip

  File "C:\Users\User\anaconda3\lib\site-packages\transformers\integrations.py", line 42, in <module>
    from .trainer_utils import PREFIX_CHECKPOINT_DIR, BestRun  # isort:skip

  File "C:\Users\User\anaconda3\lib\site-packages\transformers\trainer_utils.py", line 10, in <module>
    from .tokenization_utils_base import ExplicitEnum

  File "C:\Users\User\anaconda3\lib\site-packages\transformers\tokenization_utils_base.py", line 31, in <module>
    from tokenizers import AddedToken

  File "C:\Users\User\anaconda3\lib\site-packages\tokenizers\__init__.py", line 17, in <module>
    from .tokenizers import Tokenizer, Encoding, AddedToken

ModuleNotFoundError: No module named 'tokenizers.tokenizers'

此处详细介绍了该软件包,因此我认为它应该可用 https://huggingface.co/transformers/model_doc/bert.html

2 个答案:

答案 0 :(得分:2)

您可以这样做:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css'],
})
export class UserComponent {
  user: {
    firstname: string;
    lastname: string;
    age: number;
    email: string;
  };

  constructor() {
    this.user.firstname = 'Muhammad';
    this.user.lastname = 'Shaeel';
    this.user.age = 23;
    this.user.email = 's123@yahoo.com';
  }
}

它应该正常工作。

无论如何,我都做了测试并做了您所做的事情,但这对我有用。我无法重现您的错误。可能您没有正确安装该库。尝试创建新环境并从头开始安装。

答案 1 :(得分:0)

我遇到了类似的问题。问题似乎是我导入张量流库的方式。我将其从 import tensorflow as tf 更改为 import tensorflow