本文主要讲解Anaconda环境的搭建以及如何安装pytorch.在这里插入图片描述
1 Anaconda
Anaconda是一个用于科学计算的Python发行版,支持Linux,Mac,winndows系统,能让你在数据科学的工作中轻松地安装经常使用的程序包。下载地址为:https://www.anaconda.com/distribution/#macos
在介绍Anaconda之前,先介绍下Conda,Conda是一个类似于pip/pip3的可执行命令行工具,其核心功能是包的管理与环境管理,支持多种语言,因此用来管理Python包也是绰绰有余的。其与pip的区别是pip可以再任何环境中安装Python包,而 conda则可以再conda环境中安装任何语言包。因为Anaconda中集合了conda,因此可以执行使用conda进行包和环境的管理。
选择相应的操作系统和Python版本,以及是图形还是命令行安装:

开启终端,用于conda安装包:

例如安装numpy包:

anaconda的所有的软件包全部在国外,安装起来很麻烦,关键是下载速度慢,而且经常中断,所以需要配置国内安装的镜像,下载速度就很快了。
1.1 conda换国内源
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QWQmpsAm-1580864796819)(http://note.youdao.com/yws/res/70338/CDB8EF5125814E8997F25D2D0F11EF82)]
1.1.1 查看源
命令
conda config --show-sources
显示结果
(base) bash-3.2$ conda config --show-sources
==> /Users/linhongzheng/.condarc <==
ssl_verify: True
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- defaults
show_channel_urls: True
这里有三个源,两个是清华的源,另一个是默认的源
1.1.2 添加源
命令
conda config --add channels
操作:
#添加清华的源
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 --show-sources)
1.1.3 其他可选的源
中科大的源
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
阿里云的源
conda config --add channels http://mirrors.aliyun.com/pypi/simple/
1.1.4 移除源
命令
conda config --remove channels
操作
conda config --remove channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/'
1.2 编辑脚本
启动jupyter notebook,用作脚本编辑器:

新建一个Python3环境的窗口:
编辑脚本,验证之前安装的numpy是否生效:

2 Pytorch
下载地址:https://pytorch.org/get-started/locally/
选择相应的版本和操作系统,以及安装工具、语言和是否支持GPU(CUDA):

在anaconda的命令行中执行上图生成的命令:

也可以指定安装的版本:
conda install pytorch=1.3.1 torchvision -c pytorch
验证
from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
输出如下信息,表明pytorch已经安装成功。
tensor([[0.5918, 0.2321, 0.0967],
[0.0229, 0.0954, 0.5862],
[0.4148, 0.5069, 0.8548],
[0.5220, 0.7584, 0.8996],
[0.2323, 0.1885, 0.2464]])
562



被折叠的 条评论
为什么被折叠?



