Create anaconda virtual environment in linux environment and install tensorflow-gpu version

1. Find the corresponding version

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

insert image description here

  • Solved, reinstalled protobuf, after installation, the guide package does not report an error
    pip install 'protobuf~=3.19.0'
    
    insert image description here

2.8 Check if the GPU is available

  • Related commands
    tf.test.is_gpu_available()
    
    insert image description here
  • Hmm, not available , possible reason对应的cuda,cudnn,tensorflow版本不匹配
    • Install the correspondingcuda,cudnn
    • The version corresponding to tensorflow2.1.0the version is
      • cuda版本是10.1
      • cudnn版本是7.6.5
  • Installcuda,cudnn
    conda install cudatoolkit=10.1 cudnn=7.6.5
    
  • Enter the command again
    tf.test.is_gpu_available()
    
    insert image description here
    • gpuStart 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))

insert image description here
insert image description here

3. Success, finally success! ! !

Guess you like

Origin blog.csdn.net/m0_46926492/article/details/129315810