本文旨在介绍Pytorch的环境搭建步骤,所使用的JupyterLab 是基于 Web 的交互式笔记本环境。于 JupyterLab 我们可以边记笔记的同时、边执行 PyTorch 代码,便于学习、调试。
安装Anaconda
- Anaconda官网:Free Download | Anaconda
进入官网并下载相应版本后安装。
完成后使用win+r打开运行窗口,输入cmd打开命令行窗口
输入conda回车
出现如下所示文本表明安装成功:
usage: conda [-h] [-V] command ...
conda is a tool for managing and deploying applications, environments and packages.
Options:
positional arguments:
command
clean Remove unused packages and caches.
config Modify configuration values in .condarc. This is modeled
after the git config command. Writes to the user .condarc
file (C:\Users\Lenovo\.condarc) by default.
create Create a new conda environment from a list of specified
packages.
help Displays a list of available conda commands and their help
strings.
info Display information about current conda install.
install Installs a list of packages into a specified conda
environment.
list List linked packages in a conda environment.
package Low-level conda package utility. (EXPERIMENTAL)
remove Remove a list of packages from a specified conda environment.
uninstall Alias for conda remove. See conda remove --help.
search Search for packages and display associated information. The
input is a MatchSpec, a query language for conda packages.
See examples below.
update Updates conda packages to the latest compatible version. This
command accepts a list of package names and updates them to
the latest versions that are compatible with all other
packages in the environment. Conda attempts to install the
newest versions of the requested packages. To accomplish
this, it may update some packages that are already installed,
or install additional packages. To prevent existing packages
from updating, use the --no-update-deps option. This may
force conda to install older versions of the requested
packages, and it does not prevent additional dependency
packages from being installed. If you wish to skip dependency
checking altogether, use the '--force' option. This may
result in an environment with incompatible packages, so this
option must be used with great caution.
upgrade Alias for conda update. See conda update --help.
optional arguments:
-h, --help Show this help message and exit.
-V, --version Show the conda version number and exit.
conda commands available from other packages:
build
convert
develop
env
index
inspect
metapackage
render
server
skeleton
如果报出未知外部命令等错误,则打开系统“设置”,搜索“编辑系统环境变量”,找到“Path“或”path”变量,点击编辑,新建如下环境变量应可以解决如下问题:
#个人路径不尽相同
Anaconda路径
Anaconda路径+\Scripts\
Anaconda路径+\Linbrary\bin
Anaconda路径+\library\mingw-w64\bin
安装Jupyter Lab
jupyter默认伴随anaconda安装,可以在命令行窗口使用如下代码查看安装版本:
如果没有安装,可以使用如下代码安装:
conda install -c conda-forge jupyterlab
执行jupyter lab启动,会打开一个网页,可以在该页面中进行正常的代码编写,运行,调试。
创建 PyTorch 环境
- PyTorch官网:PyTorch
进入命令行窗口:
输入如下代码创建虚拟环境,在虚拟环境内工作可以减少出现错误后的工作量,预防出现系统级的崩溃。
conda create -n 虚拟环境名
activate 虚拟环境名
虚拟环境创建后,进入官网根据需要选择需求后,将官网生成的代码在命令行中运行即可安装,示例如下:
导入PyTorch环境
# 安装 IPython kernel for Jupyter
conda install ipykernel -y
# 导入 pytorch 虚拟环境到 ipykernel
python -m ipykernel install --user --name pytorch --display-name "Python PyTorch"
完成以上步骤,再运行jupyter lab,进入网页,选择python pytorch即可开始编写代码。