之前的tensorflow的虚拟环境出现了问题,在jupyter上训练网络经常坏掉重启,试了很多方法也没搞定,有的说需要重新安装jupyter,试了一下还是有问题。于是决定新创建一个tensorflow虚拟环境:
在b站上看到很多简洁版安装tensorflow-gpu版的,但是因为我之前已经安装了CUDA和cudnn,想着不想浪费,就还是按照普通方式进行安装。
1.确认电脑CUDA和cudnn的版本号
通过这篇文章Tensorflow-gpu保姆级安装教程(Win11, Anaconda3,Python3.9)-CSDN博客确定了电脑装的确实有CUDA和cudnn,显卡是RTX4060,CUDA是11.8。
大概就是找到D:\CUDA\CUDA1\extras\demo_suite下输入cmd分别输入下列两行代码出现“pass”即可,可以参考上述大佬的文章。
.\bandwidthTest.exe
.\deviceQuery.exe
2.创建虚拟环境并安装Tensorflow-gpu
conda create -n tf1 python=3.7
创建虚拟环境tf1,python版本我用的3.7(因为看了很多教程用3.7的比较多,2.6.0的tensorflow支持3.6-3.8)
conda activate tf1
进入虚拟环境,然后安装tensorflow-gpu--2.6.0,用镜像源的速度还是很快的,大概两分钟安装完成了
pip install tensorflow-gpu==2.6.0 -i https://pypi.mirrors.ustc.edu.cn/simple
顺利安装完成。
输入一下代码测试安装完成了
import tensorflow as tf
print(tf.__version__)
print(tf.test.gpu_device_name())
print(tf.config.experimental.set_visible_devices)
print('GPU:', tf.config.list_physical_devices('GPU'))
print('CPU:', tf.config.list_physical_devices(device_type='CPU'))
print(tf.config.list_physical_devices('GPU'))
print(tf.test.is_gpu_available())
# 输出可用的GPU数量
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
# 查询GPU设备
出现结果True
2.6.0
/device:GPU:0
<function set_visible_devices at 0x0000018B7D3AB840>
GPU: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
CPU: [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
WARNING:tensorflow:From C:\Users\Administrator\AppData\Local\Temp\ipykernel_4604\2322085259.py:9: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
True
Num GPUs Available: 1
3.安装后的问题
本来以为顺利结束,安装了相关的包pandas、matplotlib、scikit-learn,不知道为啥numpy好像直接安装上了,如果没有安装可以再手动安装一下。
pip install pandas matplotlib scikit-learn
开始拿之前写的代码重新运行一下,前边还很顺利,但是到这行代码时候断了
from tensorflow.keras.models import Sequential
出现的问题是
ImportError: cannot import name 'deserialize_keras_object' from 'keras.utils.generic_utils' (D:\conda\envs\tf1\lib\site-packages\keras\utils\generic_utils.py)
有的说是keras版本太高,于是我查了下keras版本,确实是2.11.0,比tensorflow高了不少,于是重新安装了keras==2.6.0,结果还是报错,找了很多文章,终于这一篇解决了问题:Python第三方库keras错误:module ‘keras.utils.generic_utils‘ has no attribute ‘populate_dict_with_module_o_in <module> import keras-CSDN博客
根据后边的文件路径找到generic_utils文件,并且打开,在最后补充一段代码
def to_snake_case(s):
return ''.join(['_' + ch.lower() if ch.isupper() else ch for ch in str(s)]).lstrip('_')
上述文章中提到的第一段代码,我查找之后发现原来的py文件中是有的,所以补充后边这段代码就成功了。
之后运行GRU神经网络时候,发现没有使用gpu加速
WARNING:tensorflow:Layer gru_2 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
之前其实也遇到过这个问题,当时大概就是把激活函数从“relu”改为“tanh”就好
查了一下文章说的是cudnn对LSTM和GRU有参数要求,大概是:
有的人说要把所有参数改成这样才会好,但是我反正是把激活函数改完就可以用gpu训练了
至此,可以跑通之前的代码了!!!
参考文献: