1、首先安装实验环境,需要python3.7(默认安装了conda)
conda create -n TF2.1 python=3.7
conda activate TF2.1,进入环境
conda install cudatoolkit=10.1
安装不顺畅可以在网上搜conda换源
conda install cudnn=7.6
pip install tensorflow==2.1
2、启动pycharm
可以直接在csdn上搜pycharm安装包,社区版的就行。
点击New Project,选择interpreter为TF2.1
然后进行检测。先建立一个test.py文件,输入如下代码
import tensorflow as tf
tensorflow_version = tf.__version__
gpu_available = tf.test.is_gpu_available()
print("tensorflow version:", tensorflow_version, "\tGPU available:", gpu_available)
a = tf.constant([1.0, 2.0],name = "a")
b = tf.constant([1.0, 2.0],name = "b")
result = tf.add(a, b, name = "add")
print(result)
出错了:
TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are: 1. Downgrade the protobuf package to 3.20.x or lower. 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).这个错误通常意味着项目中使用的TensorFlow版本与protobuf
库版本不兼容。
pip install --upgrade protobuf>=3.19.0
解决
安装成功了,但是不能用GPU,在网上搜到tensorflow需要cuda10.1或者11.0的,而我安装的是11.8的,进行卸载安装。
参考博客windows下CUDA的卸载以及安装_把电脑上的cuda卸载了会有影响么-CSDN博客
CUDA安装及环境配置——最新详细版-CSDN博客
成功,换成了cuda11.0.1