文章目录
- 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\PyTorchEnvadded / updated specs:- python=3.12.4The following packages will be downloaded:package | build---------------------------|-----------------bzip2-1.0.8 | h2bbff1b_6 90 KBexpat-2.6.2 | hd77b12b_0 260 KBlibffi-3.4.4 | hd77b12b_1 122 KBpip-24.0 | py312haa95532_0 3.5 MBpython-3.12.3 | h1d929f7_1 16.3 MBsetuptools-69.5.1 | py312haa95532_0 1.3 MBsqlite-3.45.3 | h2bbff1b_0 973 KBtk-8.6.14 | h0416ee5_0 3.5 MBtzdata-2024a | h04d1e81_0 116 KBvc-14.2 | h2eaa2aa_1 10 KBwheel-0.43.0 | py312haa95532_0 167 KBxz-5.4.6 | h8cc25b3_1 609 KBzlib-1.2.13 | h8cc25b3_1 131 KB------------------------------------------------------------Total: 27.0 MBThe following NEW packages will be INSTALLED:bzip2 pkgs/main/win-64::bzip2-1.0.8-h2bbff1b_6ca-certificates pkgs/main/win-64::ca-certificates-2024.3.11-haa95532_0expat pkgs/main/win-64::expat-2.6.2-hd77b12b_0libffi pkgs/main/win-64::libffi-3.4.4-hd77b12b_1openssl pkgs/main/win-64::openssl-3.0.14-h827c3e9_0pip pkgs/main/win-64::pip-24.0-py312haa95532_0python pkgs/main/win-64::python-3.12.3-h1d929f7_1setuptools pkgs/main/win-64::setuptools-69.5.1-py312haa95532_0sqlite pkgs/main/win-64::sqlite-3.45.3-h2bbff1b_0tk pkgs/main/win-64::tk-8.6.14-h0416ee5_0tzdata pkgs/main/noarch::tzdata-2024a-h04d1e81_0vc pkgs/main/win-64::vc-14.2-h2eaa2aa_1vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.29.30133-h43f2093_3wheel pkgs/main/win-64::wheel-0.43.0-py312haa95532_0xz pkgs/main/win-64::xz-5.4.6-h8cc25b3_1zlib pkgs/main/win-64::zlib-1.2.13-h8cc25b3_1Proceed ([y]/n)? yDownloading 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\PyTorchEnvadded / updated specs:- numpyThe following packages will be downloaded:package | build---------------------------|-----------------blas-1.0 | mkl 6 KBintel-openmp-2023.1.0 | h59b6b97_46320 2.7 MBmkl-2023.1.0 | h6b88ed4_46358 155.9 MBmkl-service-2.4.0 | py312h2bbff1b_1 55 KBmkl_fft-1.3.8 | py312h2bbff1b_0 160 KBmkl_random-1.2.4 | py312h59b6b97_0 196 KBnumpy-1.26.4 | py312hfd52020_0 11 KBnumpy-base-1.26.4 | py312h4dde369_0 6.6 MBtbb-2021.8.0 | h59b6b97_0 149 KB------------------------------------------------------------Total: 165.7 MBThe following NEW packages will be INSTALLED:blas pkgs/main/win-64::blas-1.0-mklintel-openmp pkgs/main/win-64::intel-openmp-2023.1.0-h59b6b97_46320mkl pkgs/main/win-64::mkl-2023.1.0-h6b88ed4_46358mkl-service pkgs/main/win-64::mkl-service-2.4.0-py312h2bbff1b_1mkl_fft pkgs/main/win-64::mkl_fft-1.3.8-py312h2bbff1b_0mkl_random pkgs/main/win-64::mkl_random-1.2.4-py312h59b6b97_0numpy pkgs/main/win-64::numpy-1.26.4-py312hfd52020_0numpy-base pkgs/main/win-64::numpy-base-1.26.4-py312h4dde369_0tbb pkgs/main/win-64::tbb-2021.8.0-h59b6b97_0Proceed ([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\PyTorchEnvadded / updated specs:- pytorch- torchvisionThe following packages will be downloaded:package | build---------------------------|-----------------brotli-python-1.0.9 | py312hd77b12b_8 346 KBcertifi-2024.6.2 | py312haa95532_0 162 KBcharset-normalizer-2.0.4 | pyhd3eb1b0_0 35 KBfilelock-3.13.1 | py312haa95532_0 23 KBfreetype-2.12.1 | ha860e81_0 490 KBidna-3.7 | py312haa95532_0 133 KBjinja2-3.1.4 | py312haa95532_0 349 KBjpeg-9e | h2bbff1b_1 320 KBlcms2-2.12 | h83e58a3_0 454 KBlerc-3.0 | hd77b12b_0 120 KBlibdeflate-1.17 | h2bbff1b_1 153 KBlibjpeg-turbo-2.0.0 | h196d8e1_0 618 KBlibpng-1.6.39 | h8cc25b3_0 369 KBlibtiff-4.5.1 | hd77b12b_0 1.1 MBlibuv-1.44.2 | h2bbff1b_0 288 KBlibwebp-base-1.3.2 | h2bbff1b_0 306 KBlz4-c-1.9.4 | h2bbff1b_1 152 KBmarkupsafe-2.1.3 | py312h2bbff1b_0 27 KBmpmath-1.3.0 | py312haa95532_0 989 KBnetworkx-3.2.1 | py312haa95532_0 2.9 MBopenjpeg-2.4.0 | h4fc8c34_0 219 KBpillow-10.3.0 | py312h2bbff1b_0 980 KBpysocks-1.7.1 | py312haa95532_0 34 KBpytorch-2.3.1 | py3.12_cpu_0 141.2 MB pytorchpytorch-mutex-1.0 | cpu 3 KB pytorchpyyaml-6.0.1 | py312h2bbff1b_0 162 KBrequests-2.32.2 | py312haa95532_0 126 KBsympy-1.12 | py312haa95532_0 14.0 MBtorchvision-0.18.1 | py312_cpu 6.7 MB pytorchtyping_extensions-4.11.0 | py312haa95532_0 75 KBurllib3-2.2.2 | py312haa95532_0 225 KBwin_inet_pton-1.1.0 | py312haa95532_0 10 KByaml-0.2.5 | he774522_0 62 KBzstd-1.5.5 | hd43e919_2 720 KB------------------------------------------------------------Total: 173.6 MBThe following NEW packages will be INSTALLED:brotli-python pkgs/main/win-64::brotli-python-1.0.9-py312hd77b12b_8certifi pkgs/main/win-64::certifi-2024.6.2-py312haa95532_0charset-normalizer pkgs/main/noarch::charset-normalizer-2.0.4-pyhd3eb1b0_0filelock pkgs/main/win-64::filelock-3.13.1-py312haa95532_0freetype pkgs/main/win-64::freetype-2.12.1-ha860e81_0idna pkgs/main/win-64::idna-3.7-py312haa95532_0jinja2 pkgs/main/win-64::jinja2-3.1.4-py312haa95532_0jpeg pkgs/main/win-64::jpeg-9e-h2bbff1b_1lcms2 pkgs/main/win-64::lcms2-2.12-h83e58a3_0lerc pkgs/main/win-64::lerc-3.0-hd77b12b_0libdeflate pkgs/main/win-64::libdeflate-1.17-h2bbff1b_1libjpeg-turbo pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0libpng pkgs/main/win-64::libpng-1.6.39-h8cc25b3_0libtiff pkgs/main/win-64::libtiff-4.5.1-hd77b12b_0libuv pkgs/main/win-64::libuv-1.44.2-h2bbff1b_0libwebp-base pkgs/main/win-64::libwebp-base-1.3.2-h2bbff1b_0lz4-c pkgs/main/win-64::lz4-c-1.9.4-h2bbff1b_1markupsafe pkgs/main/win-64::markupsafe-2.1.3-py312h2bbff1b_0mpmath pkgs/main/win-64::mpmath-1.3.0-py312haa95532_0networkx pkgs/main/win-64::networkx-3.2.1-py312haa95532_0openjpeg pkgs/main/win-64::openjpeg-2.4.0-h4fc8c34_0pillow pkgs/main/win-64::pillow-10.3.0-py312h2bbff1b_0pysocks pkgs/main/win-64::pysocks-1.7.1-py312haa95532_0pytorch pytorch/win-64::pytorch-2.3.1-py3.12_cpu_0pytorch-mutex pytorch/noarch::pytorch-mutex-1.0-cpupyyaml pkgs/main/win-64::pyyaml-6.0.1-py312h2bbff1b_0requests pkgs/main/win-64::requests-2.32.2-py312haa95532_0sympy pkgs/main/win-64::sympy-1.12-py312haa95532_0torchvision pytorch/win-64::torchvision-0.18.1-py312_cputyping_extensions pkgs/main/win-64::typing_extensions-4.11.0-py312haa95532_0urllib3 pkgs/main/win-64::urllib3-2.2.2-py312haa95532_0win_inet_pton pkgs/main/win-64::win_inet_pton-1.1.0-py312haa95532_0yaml pkgs/main/win-64::yaml-0.2.5-he774522_0zstd pkgs/main/win-64::zstd-1.5.5-hd43e919_2Proceed ([y]/n)? ydone
(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