安装环境Anaconda+pytorch

Anaconda安装

Anaconda: 安装教程

创建虚拟环境

# 创建python虚拟环境
conda create -n wkkk python=3.9
# 激活虚拟环境
activate wkkk
# 查看当前存在哪些虚拟环境
conda env list
conda info -e
# 查看安装了哪些包
conda list
# 检查更新当前conda
conda update conda

安装pytorch

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
# 安装各种库
conda install matplotlib

查看显存使用

nvidia-smi
# 动态刷新
watch -n 0.5 nvidia-smi

常见镜像源

安装pytorch时可能会失败,可以选择增加镜像源

# 清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
# 中科大源
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
# 删除源
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
# 设置搜索时显示通道地址
conda config --set show_channel_urls yes
# 查看已安装的源
conda config --show-sources

linux修改anaconda源时出现报错

CondaError: Cannot write to condarc file at /home/yihong/.condarc
Caused by PermissionError(13, ‘Permission denied’)
参考链接

# 解决办法
sudo chown -R pc /home/pc/.condarc
sudo chown -R pc /home/pc/anaconda3
# pc为用户名
# /home/pc/anaconda3为anaconda路径

linux下pip换源

# 查看pip版本,pip版本>10可以使用以下命令
pip -V
# 换源命令
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
# 常见的源列表
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 
阿里云 http://mirrors.aliyun.com/pypi/simple/ 
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 
豆瓣(douban) http://pypi.douban.com/simple/ 
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

若出现writing代表写入成功
参考链接
在这里插入图片描述

pycharm+anaconda虚拟环境

选择existing environment
在这里插入图片描述
选择想要插入的虚拟环境
在这里插入图片描述
插入完成
在这里插入图片描述

测试是否安装完成

import torch

print(torch.__version__)
print(torch.version.cuda)
print(torch.backends.cudnn.version())
print('gpu',torch.cuda.is_available())

测试结果:安装完成
在这里插入图片描述

一些软件的安装教程

Latex:

latex:安装教程

### Anaconda 配合 PyTorch 的详细安装教程 #### 1. 下载并安装 Anaconda 为了简化 Python 和常用库的管理,建议通过 Anaconda 来构建开发环境。可以从清华大学开源软件镜像站下载适合操作系统的 Anaconda 版本[^2]。 - 打开链接:`https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/` - 根据操作系统选择对应的安装包(推荐最新稳定版本)。 - 安装过程中可以选择图形化界面安装程序,这通常更易于新手上手。 完成安装后,在终端或命令提示符中输入 `python` 测试是否可以进入交互模式,并使用 `exit()` 命令退出测试环境[^1]。 --- #### 2. 创建 Conda 虚拟环境 CondaAnaconda 提供的强大工具之一,用于隔离不同项目的依赖关系。以下是创建虚拟环境的具体方法: ```bash # 创建名为 gpupytorch 的虚拟环境,指定 Python 版本为 3.9 conda create -n gpupytorch python=3.9 ``` 激活该虚拟环境以便后续安装其他依赖项: ```bash # 激活虚拟环境 conda activate gpupytorch ``` 如果需要删除此环境,则执行以下命令: ```bash conda remove -n gpupytorch --all ``` 上述过程确保了项目所需的特定 Python 版本和其他依赖不会干扰全局环境或其他项目[^3]。 --- #### 3. 安装 PyTorch PyTorch 支持多种硬件加速方式,包括 CPU 和 GPU。具体安装步骤如下: ##### (a) 访问官方安装页面获取指令 打开官网地址 https://pytorch.org/get-started/locally/ ,根据系统架构、CUDA 版本以及所需功能选择合适的配置选项。例如: - **OS**: Linux/Mac/Windows - **Package Manager**: Conda - **Language**: Python - **Compute Platform**: CUDA 或 CPU 假设当前环境中已启用支持 NVIDIA 显卡的 CUDA 11.8,则可复制以下命令到终端运行: ```bash # 使用 Conda 安装带有 CUDA 加速的 PyTorch conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch -c nvidia ``` 对于仅需 CPU 运算的情况,可以直接忽略 `cudatoolkit` 参数: ```bash # 不带 GPU 加速的纯 CPU 安装 conda install pytorch torchvision torchaudio cpuonly -c pytorch ``` 以上命令会自动解析并解决所有必要的依赖关系。 --- #### 4. 验证安装结果 确认 PyTorch 是否正确加载及其可用设备类型: ```python import torch print(torch.__version__) # 输出 PyTorch 当前版本号 print(torch.cuda.is_available()) # 判断是否有可用的 GPU 设备 if torch.cuda.is_available(): print(f"CUDA Version: {torch.version.cuda}") print(f"Device Name: {torch.cuda.get_device_name(0)}") else: print("No CUDA device detected.") ``` 当返回 True 并显示具体的显卡名称时,说明 GPU 已经成功绑定至 PyTorch 中;反之则表示只启用了 CPU 功能。 --- #### 5. 整合 PyCharm 开发环境 为了让 IDE 更好地适配新建立好的 conda 环境,请按照下列指南设置路径映射: - 启动 PyCharm 应用; - 导航菜单栏中的 File -> Settings -> Project Interpreter; - 添加新的解释器位置指向刚才定义过的 gpupytorch 文件夹下的 bin/python.exe (Windows 用户可能位于 Scripts/python.exe); - 最终保存更改即可让编辑器识别最新的库集合。 至此整个流程结束,现在应该能够流畅开展基于深度学习框架的应用研究工作! --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值