【youcans的深度学习 02】PyTorch CPU版本安装与环境配置

欢迎关注『youcans的深度学习』系列,持续更新中…
【youcans的深度学习 01】安装环境之 miniconda
【youcans的深度学习 02】PyTorch CPU版本安装与环境配置
【youcans的深度学习 03】PyTorch CPU版本安装与环境配置
【youcans的深度学习 04】PyTorch入门教程:基础知识
【youcans的深度学习 05】PyTorch入门教程:快速入门



1. 安装环境要求

  • PyTorch支持Windows、Linux、Macos等操作系统。
  • Windows平台:支持Windows7及以上版本,推荐使用Windows10或更高版本。
  • 需要Python环境,推荐使用conda包管理工具和PycharmIDE工具。
  • 推荐使用MiniConda搭建Python环境,提供了conda 包管理工具和Python解释器。
  • 推荐创建python虚拟环境,在虚拟环境中下载安装PyTorch。

安装CPU版本前建议测试(但非必须)本机是否为独立显卡,是否支持CUDA的安装:点击【cuda-gpus列表】可以查询显卡是否在列表中。


2. 创建Python虚拟环境

2.1 安装 Anaconda 或 miniconda

详见上节 【01 安装环境 miniconda】。


2.2 创建torch虚拟环境

强烈推荐创建python虚拟环境,在虚拟环境中下载安装PyTorch。

(1)cmd进入命令行编辑器,或由Anaconda Prompt,或miniconda Prompt 进入命令行编辑器。默认为<base>环境, base 是安装 Anaconda 自带的一个基础环境。

(2)输入"conda env list",检查本机中的的Python环境:

在这里插入图片描述

如果用户尚未创建虚拟环境,则只有 base环境。如果用户已经创建虚拟环境,也会显示在列表中。

如图所示,用户创建了 py38虚拟环境,当前激活环境为 base(路径之前有个星号*

(3)输入"conda create -n torch",创建名为"torch"的虚拟环境:

在这里插入图片描述

(4)输入"conda activate torch",激活torch虚拟环境。

在这里插入图片描述

激活torch虚拟环境以后,当前激活环境为 torch(路径之前有个星号*)。

(5)在torch虚拟环境安装必要的包。

conda install -y numpy
conda install -y matplotlib & conda install -y pandas & conda install -y xlrd


2.3 PyCharm环境配置

2.3.1 创建新项目的环境设置
  • 打开PyCharm,创建新的项目。

  • Location 选择适当的项目路径

  • Python Interpreter 选择 Previously configured interpreter,选项框中自动找到 Python3.8(torch)

  • 如果没有自动找到,则可以通过路径选择添加Python解释器Add Python Interpreter,找到所经历的 py38 环境的Python解释器。如下图所示:

在这里插入图片描述

如果在该窗口选择 Create a main.py welcom script,则在创建项目的同时新建了一个 main.py 文件。

运行这个 main.py 文件,在 PyCharm 下方就会出现运行结果:

在这里插入图片描述


2.3.2 已有项目的环境设置
  • 打开 PyCharm,打开一个已经建立的项目;
  • 在 PyCharm 上方菜单选择:FileSettings
  • 弹出 Settings 窗口,从左侧菜单选择:ProjectPython Interpreter,从右上方的选项框Python Interpreter选择 Python3.8(torch)环境。当然也可以选择创建的其它 Python 环境。
  • 打开并运行 python 程序,得到运行结果。

在这里插入图片描述

如果提示缺少导入的包,可以按照 3.2 的方法安装相应的包。



3. 安装 PyTorch CPU版本

3.1 官网下载

(1)进入PyTorch官网【https://pytorch.org/】

PyTorch

FROM RESEARCH TO PRODUCTION

An open source machine learning framework that accelerates the path from research prototyping to production deployment.

Installhttps://pytorch.org/

(2)按照 INSTALL PYTORCH 的提示,选择适合自己的PyTorch版本。
说明:Stable代表PyTorch当前最受测试和支持的版本,适合普通用户。

在这里插入图片描述

(3)复制"Run this Command"生成的命令。

Run this Command:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

3.2 conda安装PyTorch

(1)回到conda环境(cmd进入命令行编辑器,或由Anaconda Prompt,或miniconda Prompt 进入命令行编辑器)

(2)运行此前所复制的"Run this Command"代码。

在这里插入图片描述

注意:

  • 一定要切换到 torch 虚拟环境,否则可能安装到base环境或其它虚拟环境。

  • 如果下载速度慢,建议使用国内镜像源。但此时要删除所复制命令中最后面的 “-c pytorch”。

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 --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

conda install pytorch torchvision torchaudio cpuonly

  • 安装中会跳出"Proceed([y]/n)?",输入"y"或直接回车即可。

3.3 PyTorch安装测试

3.3.1 进入python进行测试

在conda环境中,进入python后import torch,就可以进行测试。结果如下图所示,没有报错就说明安装成功。

在这里插入图片描述


3.3.2 进入PyCharm进行测试

(4)进入PyCharm进行测试

上述conda环境的测试已经说明PyTorch版本安装成功。但为了确认PyCharm中的虚拟环境配置正确,可以进入PyCharm,选择 Python3.8(torch)环境运行以下例程:

# BasicTorch01_v1.py
# test01 of PyTroch
import torch
import torchvision

def print_hi(name):
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

if __name__ == '__main__':
    print_hi('PyCharm')
    print("HELLO pytorch {}".format(torch.__version__))
    print("torchvision.version:", torchvision.__version__)
    print("torch.cuda.is_available? ", torch.cuda.is_available())

    x = torch.rand(3, 6)
    print(x)

运行结果如下:

Hi, PyCharm
HELLO pytorch 1.13.1
torchvision.version: 0.14.1
torch.cuda.is_available? False
tensor([[0.0052, 0.2104, 0.8887, 0.8930, 0.4597, 0.7055],
[0.6065, 0.2684, 0.9401, 0.4489, 0.2740, 0.3039],
[0.0663, 0.8546, 0.4360, 0.6067, 0.9844, 0.3755]])


一切顺利,收工。


4. PyTorch(CPU)应用例程

最后给一个基于PyTorch(CPU)的应用例程,绘制几种激活函数的形状。

# BasicTorch02_v1.py
# test02 of PyTroch

import torch
from torch.autograd import Variable
import torch.nn.functional as Func
import matplotlib.pyplot as plt

# 生成数据序列
tensor = torch.linspace(-5, 5, 200)  #  返回一维张量
tensor = Variable(tensor)  # Tensor 转为 Variable
xNp = tensor.numpy()  # Tensor 转为 numpy

# 定义激活函数
y_relu = torch.relu(tensor).data.numpy()
y_sigmoid =torch.sigmoid(tensor).data.numpy()
y_tanh = torch.tanh(tensor).data.numpy()
y_softplus = Func.softplus(tensor).data.numpy()

# 绘图
plt.figure(figsize=(9, 6))
plt.suptitle("Response curve of activation function")
plt.subplot(221)
plt.plot(xNp, y_relu, c='red', label='RelU')
plt.legend(loc='best')
plt.subplot(222)
plt.plot(xNp, y_softplus, c='red', label='hardTanh')
plt.legend(loc='best')
plt.subplot(223)
plt.plot(xNp, y_sigmoid, c='red', label='sigmoid')
plt.legend(loc='best')
plt.subplot(224)
plt.plot(xNp, y_tanh, c='red', label='tanh')
plt.legend(loc='best')
plt.show()

在这里插入图片描述

【本节完】


版权声明:
欢迎关注『youcans的深度学习』系列,转发请注明原文链接:
【youcans的深度学习 02】PyTorch CPU版本安装与环境配置(https://youcans.blog.csdn.net/article/details/129228158)
Copyright 2023 youcans, XUPT
Crated:2023-03-20


  • 3
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youcans_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值