Create anaconda virtual environment in linux environment and install tensorflow-gpu version
1. Find the corresponding version
- version connection lookup
2. Download steps
2.1 Select the download version
- TensorFlow 2.1.0
- Python 3.7
- hard 2.3.1
- cuda 10.1 [I reported an error later, only to find that there is a problem with the version, and I need to correspond to the version]
- hidden 7.6[I reported an error later, only to find that there is a problem with the version, and I need to correspond to the version]
2.2 Create a virtual environment
conda create --name env_tensorflow python=3.7
2.3 Enter the virtual environment
conda activate env_tensorflow
2.5 Update three packages
pip install --upgrade numpy
pip install --upgrade pandas
pip install --upgrade scipy
2.6 Install tensorflow and keras
- Add a mirror, download faster
pip install tensorflow-gpu==2.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install keras==2.3.1
2.7 Verify that the installation is successful
import tensorflow as tf
- An error was reported, and the error message was
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).More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
- Solved, reinstalled
protobuf
, after installation, the guide package does not report an errorpip install 'protobuf~=3.19.0'
2.8 Check if the GPU is available
- Related commands
tf.test.is_gpu_available()
- Hmm, not available , possible reason
对应的cuda,cudnn,tensorflow版本不匹配
- Install the corresponding
cuda,cudnn
- The version corresponding to
tensorflow2.1.0
the version iscuda版本是10.1
cudnn版本是7.6.5
- Install the corresponding
- Install
cuda,cudnn
conda install cudatoolkit=10.1 cudnn=7.6.5
- Enter the command again
tf.test.is_gpu_available()
gpu
Start successfully
2.9 Test code
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
x=tf.constant(1)
y=tf.constant(2)
z=x+y
sess=tf.compat.v1.Session()
print(sess.run(z))