Skip to content

text2vec==1.2.9 和 torch==2.0.0 兼容性问题:发生异常: ImportError (note: full exception trace is shown but execution is paused at: <module>) cannot import name 'LRScheduler' from 'torch.optim.lr_scheduler' #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
joy1990 opened this issue Mar 13, 2025 · 2 comments
Labels
question Further information is requested

Comments

@joy1990
Copy link

joy1990 commented Mar 13, 2025

GitHub Issue 模板

标题
text2vec==1.2.9torch==2.0.0 兼容性问题:发生异常: ImportError (note: full exception trace is shown but execution is paused at: )
cannot import name 'LRScheduler' from 'torch.optim.lr_scheduler'

描述
在使用 text2vec==1.2.9torch==2.0.0 时,遇到导入错误 ImportError: cannot import name 'LRScheduler' from 'torch.optim.lr_scheduler'

详细信息

  1. 环境信息

    • Python 版本:3.10.12
    • torch 版本:2.0.0
    • text2vec 版本:1.2.9
  2. 错误信息

    ImportError: cannot import name 'LRScheduler' from 'torch.optim.lr_scheduler'
    
  3. 复现步骤

    • 安装依赖:
      pip install torch==2.0.0
      pip install text2vec==1.2.9
    • 运行代码:
      from text2vec import cos_sim, SentenceModel
      
      # 加载预训练的 Sentence-BERT 模型
      model = SentenceModel('shibing624/text2vec-base-chinese')
      
      # 输入文本
      text1 = "酒店非常干净"
      text2 = "这家酒店的房间很整洁"
      
      # 将文本转换为向量
      vec1 = model.encode([text1])[0]
      vec2 = model.encode([text2])[0]
      
      # 使用 torch 进行余弦相似度计算
      similarity = cos_sim(vec1, vec2)
      print(f"余弦相似度: {similarity.item()}")
  4. 预期行为

    • 成功加载 SentenceModel 并计算文本相似度。
  5. 实际行为

    • 报错 ImportError: cannot import name 'LRScheduler' from 'torch.optim.lr_scheduler'

附加信息

  • 是否尝试过其他版本的 torchtext2vec?如果尝试过,请描述结果。
  • 是否有其他库依赖冲突?如果有,请提供相关信息。
@joy1990 joy1990 added the question Further information is requested label Mar 13, 2025
@joy1990
Copy link
Author

joy1990 commented Mar 14, 2025

Image

原来被这哥们改了

@shibing624
Copy link
Owner

建议升级torch版本到2.4.0及以上。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants