您现在的位置是:首页 > 技术教程 正文

关于安装 PyTorch-Lightning 的一些问题(GPU版)

admin 阅读: 2024-03-25
后台-插件-广告管理-内容页头部广告(手机)

官网地址:

PyTorch

PyTorch-Lightning

安装PyTorch-Lightning

1、不能直接使用pip install pytorch-lightning ,否则如下图会直接卸载掉你的torch而安装cpu版本的torch。

  1. Installing collected packages: torch, lightning_fabric
  2. Attempting uninstall: torch
  3. Found existing installation: torch 1.9.1+cu111
  4. Uninstalling torch-1.9.1+cu111:
  5. Successfully uninstalled torch-1.9.1+cu111

2、在安装pytorch-lightning时一定注意自己的torch是pip安装还是conda安装,两者要保持一致,否则也会导致你的torch版本被替换。

正确安装方式:pip install pytorch-lightning==版本名

验证PyTorch和PyTorch-Lightning版本

 在Python中导入PyTorch,然后打印出其版本号来查看,示例代码如下:

  1. import torch
  2. print(torch.__version__)

使用以下代码来查看已安装的 PyTorch-Lightning 版本: 

  1. import pytorch_lightning as pl
  2. print(pl.__version__)

验证GPU是否能用

  1. import torch
  2. if torch.cuda.is_available():
  3. print("CUDA is available")
  4. else:
  5. print("CUDA is not available")
  1. import pytorch_lightning as pl
  2. print(pl._C.has_cuda) # will print True if GPU is supported, False otherwise

 一些报错解决:

import pytorch_lightning出现的报错:

 ImportError: cannot import name ‘get_num_classes’ from ‘torchmetrics.utilities.data’

根据参考  ImportError: cannot import name ‘get_num_classes’ from ‘torchmetrics.utilities.data’在虚拟环境下执行下面命令

  1. pip uninstall torchmetrics
  2. pip install torchmetrics==0.5

 然而,出现了新的报错

AttributeError: module 'distutils' has no attribute 'version'

根据参考python, torch. 遇到AttributeError: module ‘distutils‘ has no attribute ‘version‘ 报错。和AttributeError: module ‘distutils‘ has no attribute ‘version‘ 解决方案

在虚拟环境下执行下面命令

pip install setuptools==59.5.0

再次尝试,报错解决

参考文章:

pytorch-lightning安装

安装 pytorch-lightning 的坑

 ImportError: cannot import name ‘get_num_classes’ from ‘torchmetrics.utilities.data’

 python, torch. 遇到AttributeError: module ‘distutils‘ has no attribute ‘version‘ 报错。

 AttributeError: module ‘distutils‘ has no attribute ‘version‘ 解决方案

AttributeError: module 'distutils' has no attribute 'version'

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索