anaconda 无法识别 Huggingface 变形金刚模块

Hugginface transformers module not recognized by anaconda

我正在使用 Anaconda,python3.7,windows10。

我试图通过 https://huggingface.co/transformers/ 在我的环境中安装转换器。 我知道我必须安装 pytorch 或 TF,我安装了 pytorch - 正如在 anaconda 导航器环境中看到的那样。

我会遇到多种错误,具体取决于我在哪里(anaconda / 提示符)卸载并重新安装了 pytorch 和 transformers。最后一次尝试使用 conda 安装 pytorch torchvision cpuonly -c pytorch 和 conda install -c conda-forge 变形金刚 我得到一个错误:

from transformers import BertTokenizer
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)

def tok(dataset):
    input_ids = []
    attention_masks = []
    sentences = dataset.Answer2EN.values
    labels = dataset.Class.values
    for sent in sentences:
        encoded_sent = bert_tokenizer.encode(sent, 
                                             add_special_tokens=True,
                                             max_length = 64,
                                             pad_to_max_length =True)

TypeError: _tokenize() got an unexpected keyword argument 'pad_to_max_length'

有人知道使用 Anaconda 安全安装变压器吗? 谢谢

问题是 conda 仅在版本 2.1.1 (repository information) 中提供了 transformers 库,而此版本没有 pad_to_max_length 参数。如果有不同的参数,我不想查找它,但您可以简单地填充结果(这只是一个整数列表):

from transformers import BertTokenizer
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)

sentences = ['this is just a test', 'this is another test']

max_length = 64

for sent in sentences:
    encoded_sent = bert_tokenizer.encode(sent, 
                                         add_special_tokens=True,
                                         max_length = max_length)
    encoded_sent.extend([0]* (max_length - len(encoded_sent)))

    ###your other stuff

我认为更好的选择是创建一个新的 conda 环境并通过 pip 而不是通过 conda 安装所有内容。这将允许您使用最新的变形金刚版本 (2.11)。

正如 cronoik 已经提到的,不知为何 conda 只安装了 2.1.1 版的变形金刚,尽管更高版本似乎可用(参见:https://anaconda.org/conda-forge/transformers/files

我在 conda 方面解决的问题是,也可以通过 link.

安装

所以我使用这个命令安装了最新版本:

 conda install https://anaconda.org/conda-forge/transformers/4.16.2/download/noarch/transformers-4.16.2-pyhd8ed1ab_0.tar.bz2

只需浏览他们的存储库并右键单击>复制link目标

编辑:我注意到,cronoik 的回答是在 condas 存储库实际上还没有提供任何其他版本的时候。但是在我回答时它确实提供了其他版本,但是如果没有另外指定,仍然只安装版本 2.1.1。

@Hung 的回答对我有用,但我还需要在收到错误后更新 packaging 版本:“huggingface-hub 0.5.1 需要打包>= 20.9,但您将拥有不兼容的包装 20.4。

This other post 也已经通过 运行 解决了这个问题:

pip install --upgrade huggingface-hub