抱脸,Numpy 无法使用

问题描述 投票:0回答:2

我正在运行的代码:

from transformers import pipeline

classifier = pipeline('sentiment-analysis')

res = classifier("I Love Python.'")

print(res)

我遇到的错误:

No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
    Using a pipeline without specifying a model name and revision in production is not recommended.

C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\serialization.py:871: UserWarning: Failed to initialize NumPy: module compiled against API version 0x10 but this version of numpy is 0xf (Triggered internally at  ..\torch\csrc\utils\tensor_numpy.cpp:68.)
obj = cast(Storage, torch._UntypedStorage(nbytes))
Traceback (most recent call last):
    File "f:\AIAR\yooo\xox.py", line 5, in <module>
    res = classifier("I've been waiting for a HuggingFace course my whole life.'")
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\text_classification.py", line 138, in __call__
    result = super().__call__(*args, **kwargs)
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\base.py", line 1067, in __call__    
    return self.run_single(inputs, preprocess_params, forward_params, 
postprocess_params)
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\base.py", line 1075, in run_single  
    outputs = self.postprocess(model_outputs, **postprocess_params)   
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\text_classification.py", line 183, in postprocess
    outputs = outputs.numpy()
RuntimeError: Numpy is not available

PIP FREEZE:别介意,我已经做了很多尝试和错误。

更新输出:

No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
2022-08-14 18:45:12.106975: I tensorflow/core/platform/cpu_feature_guard.cc:193] 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.
2022-08-14 18:45:12.667076: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1339 MB memory:  -> device: 0, name: NVIDIA GeForce MX230, pci bus id: 0000:01:00.0, compute capability: 6.1
All model checkpoint layers were used when initializing TFDistilBertForSequenceClassification.

All the layers of TFDistilBertForSequenceClassification 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 TFDistilBertForSequenceClassification for predictions without further training.

获得我想要的输出:

[{'label': 'POSITIVE', 'score': 0.9973993301391602}]

python numpy nlp huggingface-transformers
2个回答
4
投票

对您来说最直接的解决办法可能是

pip install --upgrade numpy

在回答这个问题时,这应该会让你

numpy==1.23.1

我没有过多关注您的其他要求,但如果您还有其他需要旧版本

numpy
的东西,您应该真正考虑一下 运行虚拟环境 并隔离您的要求彼此拥有,而不是将所有内容安装到系统范围的 Python 中
site-packages

如果我使用此设置运行您的代码(在虚拟环境中)

#> python -m venv .venv
#> source .venv/bin/activate
#> pip install tensorflow==2.9.1 numpy==1.23.1 transformers==4.21.1
#> python hug-test.py

[{'label': 'POSITIVE', 'score': 0.9996470212936401}]

以下是更长的解释...

您的需求依赖关系已损坏,

numpy
的手动安装是问题所在,因为您很可能执行了与以下步骤类似的操作来达到此状态:

pip install tensorflow
pip install transformers
# at some point
pip uninstall numpy
pip install --ignore-installed numpy==1.22.0

tensorflow
transformers
都有针对
numpy==1.23.x
构建的预构建轮子,因此通过降级
numpy
版本,它们无法通过依赖项检查,因为它们具有检查代码以确保它们所使用的版本构建的基础与您安装的相同。

这是重现您的问题的最低限度

requirements.txt

transformers==4.21.1
tensorflow==2.9.1
numpy==1.22.0

这是我用来重新创建您的环境的

Dockerfile

FROM python:3.10-slim-buster

RUN mkdir -p /hug-app
WORKDIR /hug-app

RUN printf "from transformers import pipeline\nclassifier=pipeline('sentiment-analysis')\nres=classifier('I love python.')\nprint(res)" >> hugme.py
RUN printf "transformers==4.21.1\ntensorflow==2.9.1\nnumpy==1.22.0" >> requirements.txt
RUN pip install -r requirements.txt

CMD ["python", "hugme.py"]

如果你有 Docker/Podman,你可以像我一样运行它......

docker build --tag wkl:tensor-test -f ./Dockerfile
docker run wkl:tensor-test

这会产生与您几乎完全相同的输出:

No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
Downloading config.json: 100%|██████████| 629/629 [00:00<00:00, 425kB/s]
Downloading tensorflow_model.bin: 100%|██████████| 255M/255M [00:09<00:00, 27.6MB/s]
/usr/local/lib/python3.10/site-packages/torch/serialization.py:871: UserWarning: Failed to initialize NumPy: module compiled against API version 0x10 but this version of numpy is 0xf (Triggered internally at  /root/pytorch/torch/csrc/utils/tensor_numpy.cpp:68.)
  obj = cast(Storage, torch._UntypedStorage(nbytes))
Downloading tokenizer_config.json: 100%|██████████| 48.0/48.0 [00:00<00:00, 51.0kB/s]
Downloading vocab.txt: 100%|██████████| 226k/226k [00:00<00:00, 2.85MB/s]
Traceback (most recent call last):
  File "/tensor-app/hugme.py", line 3, in <module>
    res=classifier('I love python.')
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/text_classification.py", line 138, in __call__
    result = super().__call__(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/base.py", line 1067, in __call__
    return self.run_single(inputs, preprocess_params, forward_params, postprocess_params)
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/base.py", line 1075, in run_single
    outputs = self.postprocess(model_outputs, **postprocess_params)
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/text_classification.py", line 183, in postprocess
    outputs = outputs.numpy()
RuntimeError: Numpy is not available

2
投票

我也刚刚开始玩这个,我收到了与您相同的错误/警告消息。 我查看了文档并尝试了我所看到的一些模型。 我尝试的得分明显较低,所以我想:如果我指定默认模型会怎样?

`classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

错误/警告消失了,分数相同。

© www.soinside.com 2019 - 2024. All rights reserved.