前言
由于个人花费不少时间,不断尝试,在全网几乎没有成功配置老版本Tensorflow-gpu1.8.0的情况下,最终还是让我找到了一种可行的且被验证的tensorflow-gpu配置方法,便记录与大家分享。
但是前人经验给我不少帮助,在此感谢!!!
1.环境配置
我是在Windows11 + Anaconda3(2024.06_1) + pycharm 2023.3.2 + RTX4050显卡上配置老版本tensorflow-gpu1.8.0,具体参数如下:
tensorflow-gpu1.8.0 + cudatoolkit9.0 + cudnn7.6.4
试了很多不同的 cudatoolkit 和 cudnn 的版本组合,都或多或少有些问题。上面的版本组合经过测试没什么问题。
网上有通过从官网下载cudatoolkit 和 cudnn安装的方式,我没试过,因为觉得配置起来有点麻烦。
我是通过在Anaconda的虚拟环境中直接通过命令输入配置安装包,tensorflow-gpu1.8.0具体步骤与tensorflow-gpu2.6.0的配置类似,大家可以参考,此处不再赘述。
Anaconda安装好后,可以先创建一个虚拟环境,这样可以与其他环境隔离开,互不干扰,创建方法如下,环境名不能是中文:
conda create -n 环境名 python=3.6
激活环境:
conda activate 环境名
激活后应该是这样,环境由base切换到你创建的环境,我的如下:
接着,按照下面所示依次输入命令,每条命令输完后,按回车(注意:tensorflow-gpu一定要pip安装,后面需要安装其他包也优先选择pip,这里照做就行):
conda install cudatoolkit==9.0
conda install cudnn==7.6.4
pip install tensorflow-gpu==1.8.0
conda install -c nvidia cuda-nvcc
然后,将创建的环境路径添加到你电脑的系统环境变量中的path中:
到这还没完,也是关键!!!关键!!!关键!!!很多人安装了tensorflow-gpu老版本,最后跑代码基本都会报错“InternalError: Blas GEMM launch failed”,我也无法躲过,当然导致该错误的原因有很多,我是几乎把网上能搜到的方法都试了,发现还是不能解决问题。一度还相信网上博主说的高版本显卡不能兼容低版本的cuda(我的显卡RTX4050)。
最后,还是从英伟达那找到了解决办法:需要从官网下载cudatoolkit9.0 补丁包,选择最新的,记住只下载补丁包。
双击.exe文件,会生成三个文件夹,将文件夹里面的文件对应拷贝到你创建的虚拟环境路径下的对应文件里:
我的虚拟环境对应的路径:D:\ProgramData\Data\Anaconda_envs\envs\tf-gpu-1.8\Library,将上面三个文件里的文件复制到下面图中对应的三个文件夹。
打开系统环境变量,删除CUDA_PATH、 CUDA_PATH_V9_0 这两个路径。之后,重启电脑。
到这里,才算基本配置完tensorflow-gpu1.8.0 + cudatoolkit9.0 + cudnn7.6.4。
2.注意事项
2.1 重要库的版本不能错
1.重要的库包括:numpy、keras、pyglet、scipy、gym、pillow
重要的库版本一定要按照如下安装(优先使用pip安装,看完注意事项第2条再安装):
没问题的Tensorflow-gpu1.8.0环境配置库
2.先安装pillow6.2.1,再安装低版本的scipy1.1.0 。参考1。SciPy版本与Python和NumPy各个版本的兼容性参考2。
2.2 使用tensorflow的gpu
1.使用tensorflow的gpu,详见参考3
3.测试代码
import tensorflow as tf
# 查看日志信息,如果包含gpu信息就是使用了gpu
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# 查看tensorflow-GPU版本
# print("tf.info: ", tf.config.list_physical_devices('GPU'))
tensorflow_version = tf.__version__
print("Tensorflow version: ", tensorflow_version)
tf.test.is_gpu_available()
gpu_available = tf.test.is_gpu_available()
print('tensorflow version:', tensorflow_version, '\tGPU available:', gpu_available)
# 调用gpu计算
with tf.device('/cpu:0'):
a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
with tf.device('/gpu:1'):
c = a + b
# 注意:allow_soft_placement=True表明:计算设备可自行选择,如果没有这个参数,会报错。
# 因为不是所有的操作都可以被放在GPU上,如果强行将无法放在GPU上的操作指定到GPU上,将会报错。
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # 指定 '/gpu:0' 可以运行
sess.run(tf.global_variables_initializer())
print(sess.run(c))
#
# tf.config.list_physical_devices('GPU')
结果如下:
D:\ProgramData\Data\Anaconda_envs\envs\tf-gpu-1.8\python.exe
Tensorflow version: 1.8.0
2024-09-03 12:43:40.571920: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2024-09-03 12:43:40.840991: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1356] Found device 0 with properties:
name: NVIDIA GeForce RTX 4050 Laptop GPU major: 8 minor: 9 memoryClockRate(GHz): 2.055
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 4.97GiB
2024-09-03 12:43:40.841520: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0
2024-09-03 12:43:43.512096: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2024-09-03 12:43:43.512369: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:929] 0
2024-09-03 12:43:43.512544: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:942] 0: N
2024-09-03 12:43:43.513181: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1053] Created TensorFlow device (/device:GPU:0 with 4734 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 4050 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.9)
2024-09-03 12:43:44.166530: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0
2024-09-03 12:43:44.166801: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2024-09-03 12:43:44.167026: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:929] 0
2024-09-03 12:43:44.167173: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:942] 0: N
2024-09-03 12:43:44.167400: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1053] Created TensorFlow device (/device:GPU:0 with 4734 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 4050 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.9)
tensorflow version: 1.8.0 GPU available: True
2024-09-03 12:43:44.204802: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0
2024-09-03 12:43:44.205195: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2024-09-03 12:43:44.205593: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:929] 0
2024-09-03 12:43:44.205841: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:942] 0: N
2024-09-03 12:43:44.206120: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4734 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 4050 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.9)
2024-09-03 12:43:44.212999: I T:\src\github\tensorflow\tensorflow\core\common_runtime\direct_session.cc:284] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: NVIDIA GeForce RTX 4050 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.9
2024-09-03 12:43:44.214903: I T:\src\github\tensorflow\tensorflow\core\common_runtime\placer.cc:886] init: (NoOp)/job:localhost/replica:0/task:0/device:GPU:0
2024-09-03 12:43:44.215070: I T:\src\github\tensorflow\tensorflow\core\common_runtime\placer.cc:886] add: (Add)/job:localhost/replica:0/task:0/device:GPU:0
2024-09-03 12:43:44.215227: I T:\src\github\tensorflow\tensorflow\core\common_runtime\placer.cc:886] b: (Const)/job:localhost/replica:0/task:0/device:CPU:0
2024-09-03 12:43:44.215473: I T:\src\github\tensorflow\tensorflow\core\common_runtime\placer.cc:886] a: (Const)/job:localhost/replica:0/task:0/device:CPU:0
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: NVIDIA GeForce RTX 4050 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.9
init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0
add: (Add): /job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
[2. 4. 6.]
Process finished with exit code 0