python - 类型错误 : '<' not supported between instances of 'torch.device' and 'int'

标签 python machine-learning huggingface-transformers torch

2023-01-25 08:21:21,659 - ERROR - Traceback (most recent call last):
  File "/home/xyzUser/project/queue_handler/document_queue_listner.py", line 148, in __process_and_acknowledge
    pipeline_result = self.__process_document_type(message, pipeline_input)
  File "/home/xyzUser/project/queue_handler/document_queue_listner.py", line 194, in __process_document_type
    pipeline_result = bill_parser_pipeline.process(pipeline_input)
  File "/home/xyzUser/project/main/billparser/__init__.py", line 18, in process
    bill_extractor_model = MachineGeneratedBillExtractorModel()
  File "/home/xyzUser/project/main/billparser/models/qa_model.py", line 25, in __new__
    cls.__model = TransformersReader(model_name_or_path=cls.__model_path, use_gpu=False)
  File "/home/xyzUser/project/.env/lib/python3.8/site-packages/haystack/nodes/base.py", line 48, in wrapper_exportable_to_yaml
    init_func(self, *args, **kwargs)
  File "/home/xyzUser/project/.env/lib/python3.8/site-packages/haystack/nodes/reader/transformers.py", line 93, in __init__
    self.model = pipeline(
  File "/home/xyzUser/project/.env/lib/python3.8/site-packages/transformers/pipelines/__init__.py", line 542, in pipeline
    return task_class(model=model, framework=framework, task=task, **kwargs)
  File "/home/xyzUser/project/.env/lib/python3.8/site-packages/transformers/pipelines/question_answering.py", line 125, in __init__
    super().__init__(
  File "/home/xyzUser/project/.env/lib/python3.8/site-packages/transformers/pipelines/base.py", line 691, in __init__
    self.device = device if framework == "tf" else torch.device("cpu" if device < 0 else f"cuda:{device}")
TypeError: '<' not supported between instances of 'torch.device' and 'int'

这是我从项目安装requirement.txt 文件后收到的错误消息。我认为这与 torch 有关,但也不知道如何修复它。我是抱脸变形金刚新手,不知道是否是版本问题。

最佳答案

这是 transformers 的错误v4.22.0 之前的许多版本的软件包,因为特定的代码行无法区分 device 参数的类型可能是 torch。 device,然后将其与 int 进行比较。通过gitblame追踪,我们可以发现this specific change在变更集中制作 9d4a45509ab在结果文件中包含第 764 行提供的急需的 if isinstance(device, torch.device): ,这将确保不会发生此错误。检查上面的标签将显示 v4.22.0 及更高版本的版本应包含此特定修复。作为回顾,要更新特定包,激活环境,并发出以下命令:

pip install -U transformers

或者使用特定版本,例如:

pip install -U transformers==4.22.0

关于python - 类型错误 : '<' not supported between instances of 'torch.device' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75229395/

相关文章:

Python 导入迭代,sys.path

python - 使用 Hugging Face Transformers 库你怎么能 POS_TAG 法语文本

python - 如何检查字典中的动态键?

python - 来自 Kafka 的 pySpark 结构化流不会输出到控制台进行调试

python - append 两个 pandas 数据帧时, bool 值将转换为 float

python - Keras 中的 CNN : unexpected drop of accuracy for small batch sizes

python - KneighborsClassifier 给出与 linalg.norm 和 scipy.spatial.distance.euclidean 不同的欧几里德值

numpy - 为什么我实现深度神经网络的成本在几次迭代后会增加?

python - 拥抱脸 - GPT2 中未知 token 的高效标记化

python - 如何修复 google colab 上的 cuda 运行时错误?