You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Transformers Pipeline做文本分类时遇Numpy不可用RuntimeError求助

解决Transformers Pipeline文本分类时的"Numpy is not available"错误

问题场景

使用Hugging Face Transformers的pipeline()进行文本分类时,无论调用何种模型,均抛出RuntimeError: Numpy is not available错误。已尝试卸载重装Transformers和Numpy(均为最新兼容版本),问题仍未解决。

运行代码示例

pipe = pipeline("text-classification", model="AdamLucek/roberta-llama3.1405B-twitter-sentiment")   
sentiment_pipeline('Today is a great day!')

# 尝试过的其他模型:
sentiment_pipeline = pipeline(model="cardiffnlp/twitter-roberta-base-sentiment-latest", tokenizer="cardiffnlp/twitter-roberta-base-sentiment-latest")
sentiment_pipeline('Today is a great day!')

错误回溯信息

RuntimeError                              Traceback (most recent call last)
Cell In[49], line 1
----> 1 sentiment_pipeline('Today is a great day!')

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\pipelines\text_classification.py:156, in TextClassificationPipeline.__call__(self, inputs, **kwargs)
    155 inputs = (inputs,)
--> 156 result = super().__call__(*inputs, **kwargs)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\pipelines\base.py:1257, in Pipeline.__call__(self, inputs, num_workers, batch_size, *args, **kwargs)
   1256 else:
--> 1257     return self.run_single(inputs, preprocess_params, forward_params, postprocess_params)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\pipelines\base.py:1265, in Pipeline.run_single(self, inputs, preprocess_params, forward_params, postprocess_params)
   1264 model_outputs = self.forward(model_inputs, **forward_params)
--> 1265 outputs = self.postprocess(model_outputs, **postprocess_params)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\pipelines\text_classification.py:208, in TextClassificationPipeline.postprocess(self, model_outputs, function_to_apply, top_k, _legacy)
    207     # To enable using fp16 and bf16
--> 208     outputs = outputs.float().numpy()
    209 else:
    210     outputs = outputs.numpy()

RuntimeError: Numpy is not available

解决方法

1. 验证Numpy在当前环境的可用性

先确认Numpy是否能被当前Python环境正常导入:

import numpy
print(numpy.__version__)

若执行时抛出导入错误,说明Numpy未正确安装到当前运行代码的Python环境中。

2. 确认Python环境一致性

检查当前代码运行的Python解释器路径,与pip工具对应的Python版本是否一致:

  • Windows系统:
    where python
    pip --version
    
  • Linux/macOS系统:
    which python
    pip --version
    

若两者路径不一致,说明你可能在错误的环境中运行代码(比如未激活虚拟环境,或安装到了其他Python版本)。

3. 强制重新安装Numpy

激活目标Python环境后,彻底卸载并重装Numpy:

pip uninstall -y numpy
pip install numpy

若存在版本兼容问题,可指定与Python 3.12兼容的Numpy版本(如1.26.x系列):

pip install numpy==1.26.4

4. 检查Transformers依赖冲突

运行以下命令检查Transformers的依赖是否存在冲突:

pip check transformers

若输出依赖冲突提示,按照提示修复对应包的版本。

5. 自定义Postprocess步骤绕过Numpy(临时方案)

如果上述方法无效,可自定义Pipeline的postprocess方法,用PyTorch原生操作替代Numpy转换:

from transformers import pipeline
import torch

def custom_postprocess(self, model_outputs, function_to_apply=None, top_k=1, _legacy=True):
    outputs = model_outputs["logits"][0].float()
    probabilities = torch.softmax(outputs, dim=-1)
    top_probs, top_indices = torch.topk(probabilities, k=top_k)
    
    results = []
    for prob, idx in zip(top_probs, top_indices):
        results.append({
            "label": self.model.config.id2label[idx.item()],
            "score": prob.item()
        })
    return results

# 创建Pipeline并替换postprocess方法
sentiment_pipeline = pipeline(model="cardiffnlp/twitter-roberta-base-sentiment-latest")
sentiment_pipeline.postprocess = custom_postprocess.__get__(sentiment_pipeline, type(sentiment_pipeline))

# 测试
print(sentiment_pipeline('Today is a great day!'))

内容的提问来源于stack exchange,提问作者Leo_Lighthouse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:05:21