Anaconda
Why use anaconda? It can not only install python and many corresponding packages at one time, but also create virtual environments to uniformly manage projects with different requirements. For example, some projects require a python3.7 environment, and some projects require a python3.8 environment.
Go directly to the official (reminder, if there is no response after clicking the download in the edge browser, you can use Google, Firefox or others), just follow the guide to install, there is only one point of attention: the installation process must be checked and added to the environment variable ! After installation, open the anaconda prompt terminal:
input conda -V
, if the current conda version is printed out, the installation is successful!
After downloading conda, it will automatically install python and many packages for you, which is very convenient.
Create a virtual environment and install Tensorflow
In the anaconda prompt terminal, enter conda create -n TF2 pyhton=3.7
Enter, enter y, a python3.7 virtual environment named TF2 is created, and then enter conda activate TF2
to enter the environment. (exit environment input conda deactivate
)
Install cudatoolkit and cudnn
If your host has an NVIDIA GPU, which is an NVIDIA graphics card, you can install cudatoolkit and cudnn, which will be very convenient later.
conda install cudatoolkit=10.1
Select y
conda install cudnn=7.6
and select y.
If an error is reported during installation, it means that your computer hardware does not support NVIDIA GPU, just install tensorflow directly.
Install tensorflow:
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple
Changed to Tsinghua source to install faster, tensorflow can choose to install the version, such as tensorflow==2.5, automatically install the latest version without adding the equal sign.
After the installation is complete, enter in the terminal python
, and then enter import tensorflow as tf
and tf.__version__
to display the current tf version, success!
If the dll file is missing:
Go to https://www.dll-files.com/search/ and download the dll you are missing
Pycharm
Go directly to the official website to download, choose free
The same installation process must be ticked to add environment variables ! Xiaobai ticks all the boxes.
Open pycharm, create a new project, choose a favorite path, the key point is:
select interpreter, click the arrow in the figure below
to select the conda environment
, and select the python.exe that was configured in the TF2 environment just now, because the version 3.7 was set before, so The python in this environment is version 3.7
ok!
run the following code
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)
This appears, congratulations on the successful configuration of the environment!