CUDA与PyTorch实践

📅 发布时间:2026/7/5 19:23:30 👁️ 浏览次数:
CUDA与PyTorch实践
【1】检测电脑知否支持CUDA如何查看电脑是否支持CUDA方法1使用nvidia-smi命令# 在命令行/终端输入nvidia-smi如果有输出显示GPU信息说明有NVIDIA显卡且驱动已安装。右上角会显示CUDA Version比如CUDA Version: 12.1方法2使用Python检查importtorch# 检查CUDA是否可用print(fCUDA是否可用:{torch.cuda.is_available()})iftorch.cuda.is_available():print(fCUDA版本:{torch.version.cuda})print(fGPU数量:{torch.cuda.device_count()})print(fGPU名称:{torch.cuda.get_device_name(0)})else:print(您的电脑不支持CUDA或未安装CUDA)方法3查看CUDA版本importtorch# 查看PyTorch编译时的CUDA版本print(fPyTorch CUDA版本:{torch.version.cuda})【2】PyTorch安装GPU版本根据您的 nvidia-smi 显示您的CUDA版本是 12.3需要安装对应的PyTorch#1.卸载当前版本 pip uninstall torch torchvision torchaudio-y #2.清除pip缓存可选 pip cache purge #3.安装CUDA版本的PyTorchpip install torch torchvision torchaudio--index-url https://download.pytorch.org/whl/cu121 #4.安装其他依赖 pip install transformers pandas scikit-learn tqdm【3】需要升级PyTorch版本ValueError: Due to a serious vulnerability issue intorch.load, even withweights_onlyTrue, we now require users to upgrade torch to at least v2.6 in order to use the function. This version restriction does not apply when loading files with safetensors.See the vulnerability report here https://nvd.nist.gov/vuln/detail/CVE-2025-32434# 配置阿里云镜像 pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/pip config set install.trusted-host mirrors.aliyun.com # 从阿里云的PyTorch镜像源安装 pip install torch2.6.0torchvision torchaudio-f https://mirrors.aliyun.com/pytorch-wheels/cu126/