文章目录
- 1. GPU 支持检查
- 2. 安装 Anaconda
- 3. 创建 PyTorch 虚拟环境
- 3.2 创建虚拟 PyTorchEnv 环境
- 3.3 检查、激活、推出虚拟环境
- 3.4 虚拟环境中python包管理
- 3.5 虚拟环境中安装 PyTorch 框架
- 4. TensorFlow 安装
1. GPU 支持检查
打开 Task Manager (任务管理器),查看第二项 Performance(性能),查看GPU,如果是 NVIDIA 那就支持GPU 加速,安装NVIDIA驱动。
驱动下载地址:NVIDIA 官方驱动
2. 安装 Anaconda
官网下载
清华镜像地址
本地安装,一路next即可:
习惯用命令行的,可以打开Prompt,执行 conda list查看所有包链接:
“conda”源可以换为清华源用以下载加速:
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/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --set show_channel_urls yes
# pip 源同理
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Anaconda Navigate 里面会关联已经安装的软件,直接 Launch 即可:
3. 创建 PyTorch 虚拟环境
PyTorch的前身是Torch,其底层和Torch框架一样,但是使用Python重新写了很多内容,不仅更加灵活,支持动态图,而且提供了Python接口。它是由Torch7团队开发,是一个以Python优先的深度学习框架,不仅能够实现强大的GPU加速,同时还支持动态神经网络。
2017年1月,由Facebook人工智能研究院(FAIR)基于Torch推出了PyTorch。它是一个基于Python的可续计算包,提供两个高级功能:
- 具有强大的GPU加速的张量计算(如NumPy)
- 包含自动求导系统的深度神经网络。
PyTorch更有利于研究人员、爱好者、小规模项目等快速搞出原型。而TensorFlow更适合大规模部署,特别是需要跨平台和嵌入式部署。
3.2 创建虚拟 PyTorchEnv 环境
#新建虚拟环境不指定 Python 版本
(base) PS C:\Users\xxx> conda create -n PyTorchEnv
#新建虚拟环境并指定 Python 版本:
(base) PS C:\Users\xxx> conda create -n PyTorchEnv python=3.12.4
Channels:
- defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\xxx\AppData\Local\anaconda3\envs\PyTorchEnv
added / updated specs:
- python=3.12.4
The following packages will be downloaded:
package | build
---------------------------|-----------------
bzip2-1.0.8 | h2bbff1b_6 90 KB
expat-2.6.2 | hd77b12b_0 260 KB
libffi-3.4.4 | hd77b12b_1 122 KB
pip-24.0 | py312haa95532_0 3.5 MB
python-3.12.3 | h1d929f7_1 16.3 MB
setuptools-69.5.1 | py312haa95532_0 1.3 MB
sqlite-3.45.3 | h2bbff1b_0 973 KB
tk-8.6.14 | h0416ee5_0 3.5 MB
tzdata-2024a | h04d1e81_0 116 KB
vc-14.2 | h2eaa2aa_1 10 KB
wheel-0.43.0 | py312haa95532_0 167 KB
xz-5.4.6 | h8cc25b3_1 609 KB
zlib-1.2.13 | h8cc25b3_1 131 KB
------------------------------------------------------------
Total: 27.0 MB
The following NEW packages will be INSTALLED:
bzip2 pkgs/main/win-64::bzip2-1.0.8-h2bbff1b_6
ca-certificates pkgs/main/win-64::ca-certificates-2024.3.11-haa95532_0
expat pkgs/main/win-64::expat-2.6.2-hd77b12b_0
libffi pkgs/main/win-64::libffi-3.4.4-hd77b12b_1
openssl pkgs/main/win-64::openssl-3.0.14-h827c3e9_0
pip pkgs/main/win-64::pip-24.0-py312haa95532_0
python pkgs/main/win-64::python-3.12.3-h1d929f7_1
setuptools pkgs/main/win-64::setuptools-69.5.1-py312haa95532_0
sqlite pkgs/main/win-64::sqlite-3.45.3-h2bbff1b_0
tk pkgs/main/win-64::tk-8.6.14-h0416ee5_0
tzdata pkgs/main/noarch::tzdata-2024a-h04d1e81_0
vc pkgs/main/win-64::vc-14.2-h2eaa2aa_1
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.29.30133-h43f2093_3
wheel pkgs/main/win-64::wheel-0.43.0-py312haa95532_0
xz pkgs/main/win-64::xz-5.4.6-h8cc25b3_1
zlib pkgs/main/win-64::zlib-1.2.13-h8cc25b3_1
Proceed ([y]/n)? y
Downloading and Extracting Packages:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate PyTorchEnv
#
# To deactivate an active environment, use
#
# $ conda deactivate
创建成功之后,在 Anacoda Navigator 的 Environments 里就可以看到新建的虚拟环境
3.3 检查、激活、推出虚拟环境
#------------------------------查看所有conda环境------------------------------
#方法一:
(base) PS C:\Users\xxx> conda env list
#conda environments:
base * C:\Users\xxx\AppData\Local\anaconda3
PyTorchEnv C:\Users\xxx\AppData\Local\anaconda3\envs\PyTorchEnv
#方法二:
(base) PS C:\Users\xxx> conda info -e
#conda environments:
base * C:\Users\xxx\AppData\Local\anaconda3
PyTorchEnv C:\Users\xxx\AppData\Local\anaconda3\envs\PyTorchEnv
#------------------------------激活某个虚拟环境------------------------------
(base) PS C:\Users\xxx> conda activate PyTorchEnv
(PyTorchEnv) PS C:\Users\xxx>
#------------------------------退出当前虚拟环境(返回base环境)------------------------------
(PyTorchEnv) PS C:\Users\xxx> conda deactivate
(base) PS C:\Users\xxx>
3.4 虚拟环境中python包管理
#-------------------查看某个环境中已安装python包列表-------------------
(base) PS C:\Users\xxx> conda list -n PyTorchEnv
#packages in environment at C:\Users\xxx\AppData\Local\anaconda3\envs\PyTorchEnv:
#Name Version Build Channel
bzip2 1.0.8 h2bbff1b_6
ca-certificates 2024.3.11 haa95532_0
expat 2.6.2 hd77b12b_0
libffi 3.4.4 hd77b12b_1
openssl 3.0.14 h827c3e9_0
pip 24.0 py312haa95532_0
python 3.12.3 h1d929f7_1
setuptools 69.5.1 py312haa95532_0
sqlite 3.45.3 h2bbff1b_0
tk 8.6.14 h0416ee5_0
tzdata 2024a h04d1e81_0
vc 14.2 h2eaa2aa_1
vs2015_runtime 14.29.30133 h43f2093_3
wheel 0.43.0 py312haa95532_0
xz 5.4.6 h8cc25b3_1
zlib 1.2.13 h8cc25b3_1
#------------------------------安装和卸载python包------------------------------
(PyTorchEnv) PS C:\Users\xxx> conda install numpy
Channels:
- defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done
##Package Plan ##
environment location: C:\Users\xxx\AppData\Local\anaconda3\envs\PyTorchEnv
added / updated specs:
- numpy
The following packages will be downloaded:
package | build
---------------------------|-----------------
blas-1.0 | mkl 6 KB
intel-openmp-2023.1.0 | h59b6b97_46320 2.7 MB
mkl-2023.1.0 | h6b88ed4_46358 155.9 MB
mkl-service-2.4.0 | py312h2bbff1b_1 55 KB
mkl_fft-1.3.8 | py312h2bbff1b_0 160 KB
mkl_random-1.2.4 | py312h59b6b97_0 196 KB
numpy-1.26.4 | py312hfd52020_0 11 KB
numpy-base-1.26.4 | py312h4dde369_0 6.6 MB
tbb-2021.8.0 | h59b6b97_0 149 KB
------------------------------------------------------------
Total: 165.7 MB
The following NEW packages will be INSTALLED:
blas pkgs/main/win-64::blas-1.0-mkl
intel-openmp pkgs/main/win-64::intel-openmp-2023.1.0-h59b6b97_46320
mkl pkgs/main/win-64::mkl-2023.1.0-h6b88ed4_46358
mkl-service pkgs/main/win-64::mkl-service-2.4.0-py312h2bbff1b_1
mkl_fft pkgs/main/win-64::mkl_fft-1.3.8-py312h2bbff1b_0
mkl_random pkgs/main/win-64::mkl_random-1.2.4-py312h59b6b97_0
numpy pkgs/main/win-64::numpy-1.26.4-py312hfd52020_0
numpy-base pkgs/main/win-64::numpy-base-1.26.4-py312h4dde369_0
tbb pkgs/main/win-64::tbb-2021.8.0-h59b6b97_0
Proceed ([y]/n)? y
Downloading and Extracting Packages:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
3.5 虚拟环境中安装 PyTorch 框架
PyTorch 要求 Python 3.10+
(PyTorchEnv) PS C:\Users\xxx> conda install pytorch torchvision -c pytorch
Channels:
- pytorch
- defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\xxx\AppData\Local\anaconda3\envs\PyTorchEnv
added / updated specs:
- pytorch
- torchvision
The following packages will be downloaded:
package | build
---------------------------|-----------------
brotli-python-1.0.9 | py312hd77b12b_8 346 KB
certifi-2024.6.2 | py312haa95532_0 162 KB
charset-normalizer-2.0.4 | pyhd3eb1b0_0 35 KB
filelock-3.13.1 | py312haa95532_0 23 KB
freetype-2.12.1 | ha860e81_0 490 KB
idna-3.7 | py312haa95532_0 133 KB
jinja2-3.1.4 | py312haa95532_0 349 KB
jpeg-9e | h2bbff1b_1 320 KB
lcms2-2.12 | h83e58a3_0 454 KB
lerc-3.0 | hd77b12b_0 120 KB
libdeflate-1.17 | h2bbff1b_1 153 KB
libjpeg-turbo-2.0.0 | h196d8e1_0 618 KB
libpng-1.6.39 | h8cc25b3_0 369 KB
libtiff-4.5.1 | hd77b12b_0 1.1 MB
libuv-1.44.2 | h2bbff1b_0 288 KB
libwebp-base-1.3.2 | h2bbff1b_0 306 KB
lz4-c-1.9.4 | h2bbff1b_1 152 KB
markupsafe-2.1.3 | py312h2bbff1b_0 27 KB
mpmath-1.3.0 | py312haa95532_0 989 KB
networkx-3.2.1 | py312haa95532_0 2.9 MB
openjpeg-2.4.0 | h4fc8c34_0 219 KB
pillow-10.3.0 | py312h2bbff1b_0 980 KB
pysocks-1.7.1 | py312haa95532_0 34 KB
pytorch-2.3.1 | py3.12_cpu_0 141.2 MB pytorch
pytorch-mutex-1.0 | cpu 3 KB pytorch
pyyaml-6.0.1 | py312h2bbff1b_0 162 KB
requests-2.32.2 | py312haa95532_0 126 KB
sympy-1.12 | py312haa95532_0 14.0 MB
torchvision-0.18.1 | py312_cpu 6.7 MB pytorch
typing_extensions-4.11.0 | py312haa95532_0 75 KB
urllib3-2.2.2 | py312haa95532_0 225 KB
win_inet_pton-1.1.0 | py312haa95532_0 10 KB
yaml-0.2.5 | he774522_0 62 KB
zstd-1.5.5 | hd43e919_2 720 KB
------------------------------------------------------------
Total: 173.6 MB
The following NEW packages will be INSTALLED:
brotli-python pkgs/main/win-64::brotli-python-1.0.9-py312hd77b12b_8
certifi pkgs/main/win-64::certifi-2024.6.2-py312haa95532_0
charset-normalizer pkgs/main/noarch::charset-normalizer-2.0.4-pyhd3eb1b0_0
filelock pkgs/main/win-64::filelock-3.13.1-py312haa95532_0
freetype pkgs/main/win-64::freetype-2.12.1-ha860e81_0
idna pkgs/main/win-64::idna-3.7-py312haa95532_0
jinja2 pkgs/main/win-64::jinja2-3.1.4-py312haa95532_0
jpeg pkgs/main/win-64::jpeg-9e-h2bbff1b_1
lcms2 pkgs/main/win-64::lcms2-2.12-h83e58a3_0
lerc pkgs/main/win-64::lerc-3.0-hd77b12b_0
libdeflate pkgs/main/win-64::libdeflate-1.17-h2bbff1b_1
libjpeg-turbo pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
libpng pkgs/main/win-64::libpng-1.6.39-h8cc25b3_0
libtiff pkgs/main/win-64::libtiff-4.5.1-hd77b12b_0
libuv pkgs/main/win-64::libuv-1.44.2-h2bbff1b_0
libwebp-base pkgs/main/win-64::libwebp-base-1.3.2-h2bbff1b_0
lz4-c pkgs/main/win-64::lz4-c-1.9.4-h2bbff1b_1
markupsafe pkgs/main/win-64::markupsafe-2.1.3-py312h2bbff1b_0
mpmath pkgs/main/win-64::mpmath-1.3.0-py312haa95532_0
networkx pkgs/main/win-64::networkx-3.2.1-py312haa95532_0
openjpeg pkgs/main/win-64::openjpeg-2.4.0-h4fc8c34_0
pillow pkgs/main/win-64::pillow-10.3.0-py312h2bbff1b_0
pysocks pkgs/main/win-64::pysocks-1.7.1-py312haa95532_0
pytorch pytorch/win-64::pytorch-2.3.1-py3.12_cpu_0
pytorch-mutex pytorch/noarch::pytorch-mutex-1.0-cpu
pyyaml pkgs/main/win-64::pyyaml-6.0.1-py312h2bbff1b_0
requests pkgs/main/win-64::requests-2.32.2-py312haa95532_0
sympy pkgs/main/win-64::sympy-1.12-py312haa95532_0
torchvision pytorch/win-64::torchvision-0.18.1-py312_cpu
typing_extensions pkgs/main/win-64::typing_extensions-4.11.0-py312haa95532_0
urllib3 pkgs/main/win-64::urllib3-2.2.2-py312haa95532_0
win_inet_pton pkgs/main/win-64::win_inet_pton-1.1.0-py312haa95532_0
yaml pkgs/main/win-64::yaml-0.2.5-he774522_0
zstd pkgs/main/win-64::zstd-1.5.5-hd43e919_2
Proceed ([y]/n)? y
done
(PyTorchEnv) PS C:\Users\xxx>
验证安装成功:
(PyTorchEnv1) PS C:\Users> python
Python 3.9.19 (main, May 6 2024, 20:12:36) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>
4. TensorFlow 安装
Tensorflow 官网安装
## 安装 python 3.9.17
(PyTorchEnv1) PS C:\Users\xxx> pip3 install tensorflow-cpu==2.5
Requirement already satisfied: tensorflow-cpu==2.5 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (2.5.0)
Requirement already satisfied: numpy~=1.19.2 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (1.19.5)
Requirement already satisfied: absl-py~=0.10 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (0.15.0)
Requirement already satisfied: astunparse~=1.6.3 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (1.6.3)
Requirement already satisfied: flatbuffers~=1.12.0 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (1.12)
Requirement already satisfied: google-pasta~=0.2 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (0.2.0)
Requirement already satisfied: h5py~=3.1.0 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (3.1.0)
Requirement already satisfied: keras-preprocessing~=1.1.2 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (1.1.2)
Requirement already satisfied: opt-einsum~=3.3.0 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (3.3.0)
Requirement already satisfied: protobuf>=3.9.2 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (3.20.3)
Requirement already satisfied: six~=1.15.0 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (1.15.0)
Requirement already satisfied: termcolor~=1.1.0 in c:\users\xxx\appdata\local\anaconda3\envs\pytorchenv1\lib\site-packages (from tensorflow-cpu==2.5) (1.1.0)
... ...
验证安装成功:
(PyTorchEnv1) PS C:\Users\xxx> python
Python 3.9.19 (main, May 6 2024, 20:12:36) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
>>> print(tf.__version__)
2.5.0