Windows系统下tensorflow-gpu2.10看图急速入门
(针对win10、win11 64位版本--2023年8月14日)
1、安装
1.1Windows下anaconda安装(针对win10、win11 64位版本)
1.1.1打开网站:https://www.anaconda.com/download/
1.1.2点击Download下载windows下的anaconda最新版本。
当前最新版本为Anaconda3-2023.07-2-Windows-x86_64(截至2023年8月14日)
1.1.3下载后双击Anaconda3-2023.07-2-Windows-x86_64.exe 文件
点击Next按钮。
点击I Agree按钮。
点击Next按钮。
设置安装目录,本次演示设置为C:\anaconda3,然后点击Next按钮。
全选后,点击Install按钮。
然后耐心等待几分钟。
点击Next按钮。
点击Next按钮。
点击Finish按钮,弹出anaconda的网站和配置界面。
至此anaconda安装完成,本anaconda默认的python版本是3.11,属于现阶段比较稳定的比较高的版本,建议使用。
1.2按照下表查询本机的nvidia GPU算力,若有适合的nvidia的gpu(建议算力3.5以上),安装gpu版
GPU | 算力 | GPU | 算力 | GPU | 算力 | GPU | 算力 |
---|---|---|---|---|---|---|---|
NVIDIA A100 | 8 | RTX A5000 | 8.6 | GeForce RTX 3090 Ti | 8.6 | GeForce RTX 3080 Ti | 8.6 |
NVIDIA A40 | 8.6 | RTX A4000 | 8.6 | GeForce RTX 3090 | 8.6 | GeForce RTX 3080 | 8.6 |
NVIDIA A30 | 8 | RTX A3000 | 8.6 | GeForce RTX 3080 Ti | 8.6 | GeForce RTX 3070 Ti | 8.6 |
NVIDIA A10 | 8.6 | RTX A2000 | 8.6 | GeForce RTX 3080 | 8.6 | GeForce RTX 3070 | 8.6 |
NVIDIA A16 | 8.6 | RTX 5000 | 7.5 | GeForce RTX 3070 Ti | 8.6 | GeForce RTX 3060 | 8.6 |
NVIDIA A2 | 8.6 | RTX 4000 | 7.5 | GeForce RTX 3070 | 8.6 | GeForce RTX 3050 Ti | 8.6 |
NVIDIA T4 | 7.5 | RTX 3000 | 7.5 | Geforce RTX 3060 Ti | 8.6 | GeForce RTX 3050 | 8.6 |
NVIDIA V100 | 7 | T2000 | 7.5 | Geforce RTX 3060 | 8.6 | Geforce RTX 2080 | 7.5 |
Tesla P100 | 6 | T1200 | 7.5 | GeForce GTX 1650 Ti | 7.5 | Geforce RTX 2070 | 7.5 |
Tesla P40 | 6.1 | T1000 | 7.5 | NVIDIA TITAN RTX | 7.5 | Geforce RTX 2060 | 7.5 |
Tesla P4 | 6.1 | T600 | 7.5 | Geforce RTX 2080 Ti | 7.5 | GeForce GTX 1080 | 6.1 |
Tesla M60 | 5.2 | T500 | 7.5 | Geforce RTX 2080 | 7.5 | GeForce GTX 1070 | 6.1 |
Tesla M40 | 5.2 | P620 | 6.1 | Geforce RTX 2070 | 7.5 | GeForce GTX 1060 | 6.1 |
Tesla K80 | 3.7 | P520 | 6.1 | Geforce RTX 2060 | 7.5 | GeForce GTX 980 | 5.2 |
Tesla K40 | 3.5 | Quadro P5200 | 6.1 | NVIDIA TITAN V | 7 | GeForce GTX 980M | 5.2 |
Tesla K20 | 3.5 | Quadro P4200 | 6.1 | NVIDIA TITAN Xp | 6.1 | GeForce GTX 970M | 5.2 |
Tesla K10 | 3 | Quadro P3200 | 6.1 | NVIDIA TITAN X | 6.1 | GeForce GTX 965M | 5.2 |
RTX A6000 | 8.6 | Quadro P5000 | 6.1 | GeForce GTX 1080 Ti | 6.1 | GeForce GTX 960M | 5 |
RTX A5000 | 8.6 | Quadro P4000 | 6.1 | GeForce GTX 1080 | 6.1 | GeForce GTX 950M | 5 |
RTX A4000 | 8.6 | Quadro P3000 | 6.1 | GeForce GTX 1070 Ti | 6.1 | GeForce 940M | 5 |
T1000 | 7.5 | Quadro P2000 | 6.1 | GeForce GTX 1070 | 6.1 | GeForce 930M | 5 |
T600 | 7.5 | Quadro P1000 | 6.1 | GeForce GTX 1060 | 6.1 | GeForce 920M | 3.5 |
T400 | 7.5 | Quadro P600 | 6.1 | GeForce GTX 1050 | 6.1 | GeForce 910M | 5.2 |
Quadro RTX 8000 | 7.5 | Quadro P500 | 6.1 | GeForce GTX TITAN X | 5.2 | GeForce GTX 880M | 3 |
Quadro RTX 6000 | 7.5 | Quadro M5500M | 5.2 | GeForce GTX TITAN Z | 3.5 | GeForce GTX 870M | 3 |
Quadro RTX 5000 | 7.5 | Quadro M2200 | 5.2 | GeForce GTX TITAN Black | 3.5 | GeForce GTX 860M | 3.0/5.0 |
Quadro RTX 4000 | 7.5 | Quadro M1200 | 5 | GeForce GTX TITAN | 3.5 | GeForce GTX 850M | 5 |
Quadro GV100 | 7 | Quadro M620 | 5.2 | GeForce GTX 980 Ti | 5.2 | GeForce 840M | 5 |
Quadro GP100 | 6 | Quadro M520 | 5 | GeForce GTX 980 | 5.2 | GeForce 830M | 5 |
Quadro P6000 | 6.1 | Quadro K6000M | 3 | GeForce GTX 970 | 5.2 | GeForce 820M | 2.1 |
Quadro P5000 | 6.1 | Quadro K5200M | 3 | GeForce GTX 960 | 5.2 | GeForce 800M | 2.1 |
Quadro P4000 | 6.1 | Quadro K5100M | 3 | GeForce GTX 950 | 5.2 | GeForce GTX 780M | 3 |
Quadro P2200 | 6.1 | Quadro M5000M | 5 | GeForce GTX 780 Ti | 3.5 | GeForce GTX 770M | 3 |
Quadro P2000 | 6.1 | Quadro K500M | 3 | GeForce GTX 780 | 3.5 | GeForce GTX 765M | 3 |
Quadro P1000 | 6.1 | Quadro K4200M | 3 | GeForce GTX 770 | 3 | GeForce GTX 760M | 3 |
Quadro P620 | 6.1 | Quadro K4100M | 3 | GeForce GTX 760 | 3 | GeForce GTX 680MX | 3 |
Quadro P600 | 6.1 | Quadro M4000M | 5 | GeForce GTX 750 Ti | 5 | GeForce GTX 680M | 3 |
Quadro P400 | 6.1 | Quadro K3100M | 3 | GeForce GTX 750 | 5 | GeForce GTX 675MX | 3 |
Quadro M6000 24GB | 5.2 | GeForce GT 730 DDR3,128bit | 2.1 | GeForce GTX 690 | 3 | GeForce GTX 675M | 2.1 |
Quadro M6000 | 5.2 | Quadro M3000M | 5 | GeForce GTX 680 | 3 | GeForce GTX 670MX | 3 |
Quadro 410 | 3 | Quadro K2200M | 3 | GeForce GTX 670 | 3 | GeForce GTX 670M | 2.1 |
Quadro K6000 | 3.5 | Quadro K2100M | 3 | GeForce GTX 660 Ti | 3 | GeForce GTX 660M | 3 |
Quadro M5000 | 5.2 | Quadro M2000M | 5 | GeForce GTX 660 | 3 | GeForce GT 755M | 3 |
Quadro K5200 | 3.5 | Quadro K1100M | 3 | GeForce GTX 650 Ti BOOST | 3 | GeForce GT 750M | 3 |
Quadro K5000 | 3 | Quadro M1000M | 5 | GeForce GTX 650 Ti | 3 | GeForce GT 650M | 3 |
Quadro M4000 | 5.2 | Quadro K620M | 5 | GeForce GTX 650 | 3 | GeForce GT 745M | 3 |
Quadro K4200 | 3 | Quadro K610M | 3.5 | GeForce GTX 560 Ti | 2.1 | GeForce GT 645M | 3 |
Quadro K4000 | 3 | Quadro M600M | 5 | GeForce GTX 550 Ti | 2.1 | GeForce GT 740M | 3 |
Quadro M2000 | 5.2 | Quadro K510M | 3.5 | GeForce GTX 460 | 2.1 | GeForce GT 730M | 3 |
Quadro K2200 | 5 | Quadro M500M | 5 | GeForce GTS 450 | 2.1 | GeForce GT 640M | 3 |
Quadro K2000 | 3 | GeForce 705M | 2.1 | GeForce GTS 450* | 2.1 | GeForce GT 640M LE | 3 |
Quadro K2000D | 3 | NVIDIA NVS 810 | 5 | GeForce GTX 590 | 2 | GeForce GT 735M | 3 |
Quadro K1200 | 5 | NVIDIA NVS 510 | 3 | GeForce GTX 580 | 2 | GeForce GT 635M | 2.1 |
Quadro K620 | 5 | NVIDIA NVS 315 | 2.1 | GeForce GTX 570 | 2 | GeForce GT 730M | 3 |
Quadro K600 | 3 | NVIDIA NVS 310 | 2.1 | GeForce GTX 480 | 2 | GeForce GT 630M | 2.1 |
Quadro K420 | 3 | Quadro Plex 7000 | 2 | GeForce GTX 470 | 2 | GeForce GT 625M | 2.1 |
GeForce GT 730 | 3.5 | GeForce 710M | 2.1 | GeForce GTX 465 | 2 | GeForce GT 720M | 2.1 |
GeForce GT 720 | 3.5 | GeForce 610M | 2.1 | GeForce GT 740 | 3 | GeForce GT 620M | 2.1 |
本次演示以本机的3070显卡为例。
1.2.1安装驱动
打开Anaconda Prompt运行nvidia-smi
查看本机的nvidia驱动版本,本机cuda为11.7,注意下面安装的cuda不得超过此版本。
查看tensorflow-gpu相应的对应版本
https://tensorflow.google.cn/install/source_windows?hl=cn 查看相应的对应版本(中文)
https://tensorflow.google.cn/install/source_windows?hl=en 查看相应的对应版本(英文)
我们发现对于gpu版本的tensorflow中文版只到tensorflow2.6.0版(对应python3.6-3.9),而英文对应到2.10.0版(对应python3.6-3.10),我们选择英文版(对应cuDNN 为8.1版本,CUDA为11.2,注意这个不能搞错,否则易出现版本不兼容)。
1.2.1.1https://developer.nvidia.com/cuda-toolkit-archive 下载相应版本的CUDA
我们下载cuda_11.2.0_460.89_win10.exe
(https://developer.nvidia.com/cuda-11.2.0-download-archive?target_os=Windows&target_arch=x86_64&target_version=10)
点击下载,约等待10分钟左右。
下载完成后,双击安装cuda_11.2.0_460.89_win10.exe
点击OK按钮解压
解压后安装
约等待半分钟后
点击同意并继续(A)按钮
按照默认的精简(E)即可,点击按钮下一步按钮继续
选中复选框,点击NEXT 继续。
点击按钮下一步按钮继续
选中复选框,点击按钮关闭(C)完成CUDA的安装。
默认安装在C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.2文件夹下。
1.2.1.2配置环境变量
右键单击桌面上的此电脑,弹出右键菜单后点击属性
输入path
回车
编辑系统环境变量。
点击按钮环境变量(N)
发现已经自动配置好了。
1.2.1.3 https://developer.nvidia.com/rdp/cudnn-archive 下载cudnn
我们下载cudnn8.1.1,对应的文件为cudnn-11.2-windows-x64-v8.1.1.33.zip(中间会提示登录账号,以自己的邮箱注册一个账号即可)。下载后解压文件,得到如下文件夹
把上面的bin include lib三个文件夹选中复制。
打开C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2
文件夹进行粘贴并覆盖。
1.2.2Tensorflow安装
1.2.2.1 tensorflow-gpu的安装
打开Anaconda Prompt
运行Pip install tensorflow-gpu==2.10
或pip install tensorflow-gpu==2.10 -i https://pypi.tuna.tsinghua.edu.cn/simple
目前windows下gpu版本最新版本是2.10
(补充,本内容为依据网上资料的简易安装方法,感觉应该很有意义,但是可能是防火墙的原因实际测试不能用,仅供参考。打开Anaconda Prompt运行 conda install cudatoolkit=11.2 conda install cuda=8.1)
1.2.2.2若无gpu,安装cpu版
打开Anaconda Prompt
执行以下命令:
Pip install tensorflow
或pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple
目前windows下最新版本是2.13
2.测试tensorflow gpu性能:
2.1
import tensorflow as tf
import timeit
# 使用cpu运算
def cpu_run():
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([100000, 1000])
cpu_b = tf.random.normal([1000, 1000])
c = tf.matmul(cpu_a, cpu_b)
return c
# 使用gpu运算
def gpu_run():
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([100000, 1000])
gpu_b = tf.random.normal([1000, 1000])
c = tf.matmul(gpu_a, gpu_b)
return c
# 默认运算
def pu_run():
gpu_a = tf.random.normal([100000, 1000])
gpu_b = tf.random.normal([1000, 1000])
c = tf.matmul(gpu_a, gpu_b)
return c
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
pu_time=timeit.timeit(pu_run, number=10)
# print("cpu:", cpu_time, " gpu:", gpu_time)
print("cpu:", cpu_time, " gpu:", gpu_time," pu:", pu_time)
# 结果
# cpu: 3.5386725999996997 gpu: 0.003457500002696179 pu: 0.002655599993886426
# 我们发现使用GPU后速度得到极大提升。
2.2
# TensorFlow and tf.keras
import tensorflow as tf
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images.shape
len(train_labels)
train_labels
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(1280, activation='relu'),
tf.keras.layers.Dense(1280, activation='relu'),
tf.keras.layers.Dense(1280, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# import timeit
import time
aa=time.time()
model.fit(train_images, train_labels, epochs=10)
bb=time.time()
cc=bb-aa
cc
aa=time.time()
with tf.device('/CPU:0'):
model.fit(train_images, train_labels, epochs=10)
bb=time.time()
cc=bb-aa
cc
aa=time.time()
with tf.device('/GPU:0'):
model.fit(train_images, train_labels, epochs=10)
bb=time.time()
cc=bb-aa
cc
在I9 13900 3700显卡 32G ddr5内存下
采用CPU,单次约24秒;采用GPU,单次约3秒
在I7 7700 710显卡 16G ddr4内存下
采用CPU,单次约53秒;
I7 7700 为4核8线程 4G,I9 13900 为24核32线程,看来,CPU带来的性能提升不大。GPU带来的性能提升才是主要的。
3. 若无合适的本机GPU,可以登录
https://cloud.tencent.com/act/pro/gpu-study?from=20421&from_column=20421
使用腾讯公司的腾讯云的GPU服务器,短期使用性价比更高。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。