【问题标题】:Anybody got huggingface transformers to work in a Conda env?有人在 Conda 环境中安装了拥抱脸转换器吗?
【发布时间】:2021-05-07 02:06:08
【问题描述】:

标题。在这一点上,我基本上尝试了 TF 2.0、2.1、tensorflow-gpu(2.1 和 2.0)和最新的 huggingface 构建的所有组合。此外,每次尝试都从头开始创建新环境。

我可以:

  • 安装 tensorflow-gpu(2.0 或 2.1)-> TF 确实找到了我的 GPU,但随后 huggingface 找不到单个 TF 模型。例如,我可以import AutoModel 很好,但我不能import TFAutoModel(错误:ImportError: cannot import name 'TFAutoModel' from 'transformers' (unknown location))。我尝试的所有 TF* 都会发生这种情况。

  • 安装 tensorflow(2.0 或 2.1)-> TF 找不到我的 GPU,但我可以 import TFAutoModel 没有问题。

我还安装了所需的 CUDA 和 cuDNN 版本,并拥有一个具有足够计算能力 yadda yadda 的 GPU。

TL;DR:如何让 huggingface TF 模型与 TF GPU 配合使用?

【问题讨论】:

  • 你的操作系统是什么?
  • 该死我忘了提...我在 Win10 上,完全是最新的。
  • 你如何检查 GPU 是否被使用,我尝试安装在一个新的环境中,至少你的导入工作
  • 我在 MACOS 11.2 上遇到了同样的错误。 conda create -n tf tensorflow 创建一个 python 3.7.2 环境。然后点安装拥抱脸。仍然得到“无法从'transformers'(未知位置)导入名称'TFAutoModelForSequenceClassification'”

标签: python tensorflow anaconda conda huggingface-transformers


【解决方案1】:

在我的 Windows 机器上,我可以简单地这样做:

conda create -n tf tensorflow-gpu
conda activate tf
conda install -c huggingface

这安装了 tensorflow-gpu 版本 2.3.0 和转换器 4.2.2

然后我可以运行来自their website 的示例代码之一:

(tf) C:\> python
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from transformers import pipeline
>>> nlp = pipeline("sentiment-analysis")
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 629/629 [00:00<00:00, 180kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 268M/268M [00:21<00:00, 12.5MB/s]
2021-02-03 08:59:08.884730: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-02-03 08:59:09.109504: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
Some layers from the model checkpoint at distilbert-base-uncased-finetuned-sst-2-english were not used when initializing TFDistilBertModel: ['pre_classifier', 'dropout_19', 'classifier']
- This IS expected if you are initializing TFDistilBertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing TFDistilBertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
All the layers of TFDistilBertModel were initialized from the model checkpoint at distilbert-base-uncased-finetuned-sst-2-english.
If your task is similar to the task the model of the checkpoint was trained on, you can already use TFDistilBertModel for predictions without further training.
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 232k/232k [00:00<00:00, 528kB/s]
Some layers from the model checkpoint at distilbert-base-uncased-finetuned-sst-2-english were not used when initializing TFDistilBertForSequenceClassification: ['dropout_19']
- This IS expected if you are initializing TFDistilBertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing TFDistilBertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some layers of TFDistilBertForSequenceClassification were not initialized from the model checkpoint at distilbert-base-uncased-finetuned-sst-2-english and are newly initialized: ['dropout_38']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
>>> result = nlp("I hate you")[0]
>>> print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
label: NEGATIVE, with score: 0.9991
>>> result = nlp("I love you")[0]
>>> print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
label: POSITIVE, with score: 0.9999

以及您提到的导入:

>>> from transformers import TFAutoModel
>>>

【讨论】:

  • 你检查过是否使用GPU吗?
猜你喜欢
  • 2021-03-30
  • 2021-04-01
  • 1970-01-01
  • 2021-09-15
  • 2018-07-04
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多