目录

  1. 前言
  2. 安装
    1. 1. 创建并激活 conda 环境
    2. 2. PyTorch
    3. 3. TensorFlow
    4. 4. 其他杂项
  3. Reference
  4. END

前言

作为机器学习从业者,我们需要一些 package 来辅助我们的工作。很多人也说现在干机器学习都是调包侠,我不是很赞同这种说法,技术越来越进步,进步的意义就在于越来越便捷,越来越 user-friendly,或者说高层,就像汽车一样。

越来越便捷就可以把宝贵时间留给更有意义的工作,比如数据处理和模型设计。而且可以降低入行门槛,一个行业如果从业者人数太少也不利于行业发展,参考之前传统武术独门绝技啥的传男不传女的规定。另一方面,调包是必要的,但如果你知其然且知其所以然,那更有利于你的工作,特别是 debug 的时候。

包括在 package 的安装方面,现在也是越来越方便,比刚出来的时候方便多了,像 TensorFlow 的安装都还需要专门写一篇文章来讲,我现在 CSDN 上访问量最高的文章就是在 Windows 上安装 TensorFlow 的文章,实在是有点意外。

而本文叫深度学习环境创建指南而不是机器学习环境创建指南,主要是为了强调深度学习相关工具的安装。

安装

为了避免重复劳动,后续可以快速创建环境,以及给有需要的人作参考,本文基于我的工作经历,记录一下 Windows 10 下一个基础深度学习环境的安装,主要包括 PyTorch 和 TensorFlow,其他 package 想起来再加。

1. 创建并激活 conda 环境

1
2
conda create -n dl python=3.9
conda activate dl

2. PyTorch

1
conda install pytorch torchvision torchaudio pytorch-cuda=11.6 -c pytorch -c nvidia

测试:

1
2
3
4
5
6
7
8
9
10
11
>>> import torch
>>> torch.__version__
'1.13.0'
>>> torch.version.cuda
'11.6'
>>> torch.cuda.is_available()
True
>>> torch.cuda.device_count()
1
>>> torch.cuda.get_device_name()
'GeForce GTX 1060'

3. TensorFlow

根据 TensorFlow 官网说明,TensorFlow 2.10 是最后一个在原生 Windows 上支持 GPU 的版本。从 2.11 开始,如果你需要在 Windows 上使用 GPU 版 TensorFlow,就必须得在 WSL 中安装了。

1
2
3
4
# 我系统上之前似乎已经安装了cuda 11.2,所以这里我就直接安装了。
# 如果你系统上没有cuda,那么可以先用conda安装cudatoolkit,然后再用pip安装TensorFlow;
# 详细参考官方教程(注意切成英文版):https://www.tensorflow.org/install/gpu#windows_setup
pip install tensorflow-gpu

测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> import tensorflow as tf
>>> tf.__version__
'2.10.1'
>>> tf.test.is_built_with_cuda()
True
>>> tf.test.gpu_device_name()
2022-12-04 11:49:37.252105: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-12-04 11:49:38.082555: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /device:GPU:0 with 4632 MB memory: -> device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1
'/device:GPU:0'
>>> tf.sysconfig.get_build_info()
OrderedDict([('cpu_compiler',
'C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64/cl.exe'),
('cuda_compute_capabilities',
['sm_35', 'sm_50', 'sm_60', 'sm_70', 'sm_75', 'compute_80']),
('cuda_version', '64_112'),
('cudart_dll_name', 'cudart64_112.dll'),
('cudnn_dll_name', 'cudnn64_8.dll'),
('cudnn_version', '64_8'),
('is_cuda_build', True),
('is_rocm_build', False),
('is_tensorrt_build', False),
('msvcp_dll_names', 'msvcp140.dll,msvcp140_1.dll'),
('nvcuda_dll_name', 'nvcuda.dll')])

4. 其他杂项

1
pip install notebook transformers datasets pandas jieba loguru

最后这个环境已经有 8.67 GB 了……

dl-env-size

Reference

END