Tensorflow is one of the most widely used deep learning frameworks. It’s arguably the most popular machine learning platform on the web, with a wide range of users, from those just starting out, to people looking for an edge in their careers and businesses.
Not all users know that you can install the TensorFlow GPU if your hardware supports it. We will discuss what Tensorflow is, how it is used in today’s world, and how to install the latest version of TensorFlow with CUDA, cudNN and GPU support on Windows, Mac and Linux.
Introduction to
TensorFlow
TensorFlow is an open source software library for machine learning, created by Google. It was initially launched on November 28, 2015, and is now used in many fields, including science and engineering research.
The idea behind TensorFlow is to make it quick and simple to train deep neural networks that use a variety of mathematical models. These networks can learn from data without human intervention or oversight, making them more efficient than conventional methods. The library also offers support for processing on multiple machines simultaneously with different operating systems and GPUs.
TensorFlow Applications
TensorFlow, a library for deep learning created by Google, has been gaining a lot of traction since its introduction early last year. Key features include automatic differentiation, convolutional neural networks (CNNs), and recurrent neural networks (RNNs). It is written in C++ and Python, for high performance it uses a server called “Cloud TensorFlow” running on Google Cloud Platform. It doesn’t require a GPU, which is one of its main features.
The latest version of Tensorflow also supports data visualization via matplotlib. This visualization library is very popular, and is often used in data science courses as well as by artists and engineers to make data visualizations using MATLAB or Python/R/etc.
Installing the latest version of TensorFlow with
CUDA, cudNN and GPU support
Let’s see how to install the latest version of TensorFlow on Windows, macOS and Linux
.
Windows
- Python 3.6-3.8
- Windows 7 or later (with C++ redistributable)
- Check https://www.tensorflow.org/install/so… For information
prerequisite
about the latest version,
steps
1) Download Microsoft Visual Studio from
:
https://visualstudio.microsoft.com/vs
…
2) Install NVIDIA CUDA Toolkit (https://developer.nvidia.com/cuda-too…), check the version of software and hardware requirements, we will use :
compilation tools We will install CUDA version 11.2, but be sure to install the latest or updated version (for example, 11.2.2 if available).
Click on the latest version and a screen will appear, where you can choose from some options, so follow the image below and choose these options for Windows.
Once you choose the above options, wait for the download to complete.
Install it with the Express option (recommended), it will take a while to install on your machine
.
3) Now we will download NVIDIA cuDNN, https://developer.nvidia.com/cudnn
Check the version code from the TensorFlow site.
Now, check the CUDA and cuDNN versions, and click download for your operating system. If you can’t find the desired version, click cuDNN file and download it from there.
After the download is complete, extract the files.
Now, copy these 3 folders (bin, include, lib). Go to C Drive>Program Files and search for NVIDIA GPU Computing Toolkit.
>Open the folder,
select CUDA > Version Name, and replace (paste) the copied files
.
Now click on the bin folder and copy the path. It should look like this: C: Program FilesNVIDIA GPU Computing ToolkitCUDAv11.2bin.
On your PC, look for environment variables, as shown below.
Click Environment Variables at the bottom left. Now click on the link that indicates PATH.
Once you click on the PATH, you will see something like this.
Now click New (top left) and paste the trash path here. Go to the CUDA folder, select the libnvvm folder and copy its path. Follow the same process and paste that path into the system path. Next, simply restart your PC.
4) Installation of Tensorflow
Open conda prompt. If it is not installed, get it here → https://www.anaconda.com/products/individual.
Now copy the following commands and paste them into the prompt (Check the versions).
conda create -name TF2.5 python==3.8 conda activate tf2.5 (version) pip install tensorflow (with GPU support) //install TensorFlow GPU command, pip install -upgrade tensorflow-gpu
You’ll see an installation screen like this. If you see any errors, make sure you are using the correct version and don’t miss any steps.
We’ve installed everything, so let’s test it on Pycharm.
Test
To test the whole process we will use Pycharm. If it is not installed, obtain the Community Edition → https://www.jetbrains.com/pycharm/download/#section=windows.
First, to check if the TensorFlow GPU has been successfully installed on your machine, run the following code:
# import the tensorflow import package Tensorflow as tf tf.test.is_built_with_cuda() tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
It should show TRUE as output. If it’s FALSE or some error, look at the steps.
Now let’s run some code
.
For a simple demonstration, we trained you on the MNIST dataset of handwritten digits. We’ll look at how to create the network, as well as initialize a loss function, check the accuracy, and more.
Configure the env, create a new Python file, and paste the following code:
# Imports import torchvision import torchvision import torch.nn.functional as F import torchvision.datasets as datasets import torchvision.transforms as transformations from torch import optim from torch import nn from torch.utils.data import DataLoader from tqdm import tqdm
Check the rest of the code here -> https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master/ML/Pytorch/Basics/pytorch_simple_CNN.py
.
When you run the code, look for cuda(versioncode) opened successfully.
Once the training started, all the steps were successful!
MacOS
MacOS does not support the Nvidia GPU for the latest versions, so this will be a CPU-only installation. You can get GPU support on a Mac with a little effort and additional requirements.
Prerequisite
- Python 3.6-3.8
- macOS 10.12.6 (Sierra) or later (no GPU support)
- Check https://www.tensorflow.org/install/so… For
the latest version information
You can install the latest version available on the site, but for this tutorial, we will use Python 3.8. Also, check with the TensorFlow site for release support.
2) Prepare environment:
After installing Miniconda, open the command prompt. conda
install -y jupyter
This will take some time to install jupyter. Next, install the Mac tensorflow.yml file. You can also create a .yml file to install TensorFlow and dependencies (mentioned below).
Dependencies: – python=3.8 – pip>=19.0 – Jupyter – scikit-learn – scipy – pandas – pandas-datareader – matplotlib – pillow – tqdm – requests – h5py – pyyaml – flask – boto3 – pip: – tensorflow==2.4 – Bayesian optimization – gym – kaggle
Run the following command from the same directory that contains tensorflow.yml. conda env create -f tensorflow.yml -n tensorflow
This installation may take a few minutes.
Activate the environment using the following command:
python -m ipykernel install -user -name tensorflow -display-name “Python 3.8 (tensorflow)”
Test
To test the whole process, we will use a Jupyter notebook. Use this command to start
Jupyter: Jupyter Notebook
Address the following code and run it in Jupyter Notebook
. import sys import tensorflow.keras import pandas as pd import sklearn as sk import tensorflow as tf print(f”Tensor Flow Version: {tf.__version__}”) print(f”Keras Version: {tensorflow.keras.__version__}”) print() print(f”Python {sys.version}”) print(f”Pandas {pd.__version__}”) print(f”Scikit-Learn {sk.__version__}”) gpu = len(tf.config.list_physical_devices(‘GPU’))>0 print(“GPU is”, “available” if the gpu is another “NOT AVAILABLE”)
This may take some time, but you will see something like this with its installed versions
.
Linux
We can install CPU and GPU versions on Linux
.
Prerequisite
- Python 3.6-3.8
- Ubuntu 16.04 or later
- Check https://www.tensorflow.org/install/so… for the latest version information
Steps
1) First download and install Miniconda from https://docs.conda.io/en/latest/miniconda.html
2) To install CUDA on your machine, you will need:
- CUDA-compatible GPU
- a supported version of Linux
- Toolkit (https://developer.nvidia.com/cuda-downloads).
,
, NVIDIA CUDA
You can install CUDA by running, $ sudo apt install nvidia-cuda-toolkit
After installing CUDA, run to verify the installation
: nvcc -V
You’ll see it generates something like this
: nvcc: NVIDIA(R) Cuda Compiler Driver Copyright (c) 2005-2021 NVIDIA Corporation Built on Sun_Jul_22_21:07:
16_PDT_2019 Cuda build tools, ‘version’ release
3) Now we will download NVIDIA cuDNN, https://developer.nvidia.com/cudnn
Check the version code on the TensorFlow site.
After downloading, extract the
files: tar -xvzf cudnn-10.1-linux-x64-‘version’.tgz
Now, we will copy the extracted files to
the CUDA Installation path: sudo cp cuda/include/cudnn.h /usr/lib/cuda/include/ sudo cp cuda/lib64/libcudnn* /usr/lib/cuda/lib64/
Setting cuDNN file permissions
: $ sudo chmod a+r /usr/lib/cuda/include/cudnn.h /usr/lib/cuda/lib64/libcudnn*
4) Prepare the environment:
Export CUDA environment variables. To set them, run
: $ echo ‘export LD_LIBRARY_PATH=/usr/lib/cuda/lib64:$LD_LIBRARY_PATH’ >> ~/.bashrc $ echo ‘export LD_LIBRARY_PATH=/usr/lib/cuda/include:$LD_LIBRARY_PATH’ >> ~/.bashrc You can also configure the environment with conda and jupyter notebook.
After installing Miniconda, open the command prompt.
conda install -y jupyter
Now, check with the TensorFlow site for the version and run the following command
: conda create -name tensorflow python=3.8
To enter the
environment: conda activate tensorflow
Let’s create Jupyter support for your new environment:
conda install nb_conda
This will take some time to get things done.
To install only the CPU, use the following command: conda install -c anaconda tensorflow To install
both the GPU and CPU,
use the
following command
: conda install -c anaconda tensorflow-gpu To add additional libraries, update
or create the ymp file in its root location, use:
conda env update -file tools.yml
Below are the additional libraries that you need to install (you can install them with pip).
Dependencies: – Jupyter – scikit-learn – scipy – pandas – pandas-datareader – matplotlib – pillow – tqdm – requests – h5py – pyyaml – flask – boto3 – pip – pip: – bayesian optimization –
gym – Kaggle Test
There are two ways to test your
GPU. First, you can
run this command
: import tensorflow as tf tf.config.list_physical_devices(“GPU”)
You will see a similar output, [PhysicalDevice(name=’/physical_device:GPU:0′, device_type=’GPU’)]
Secondly, you can also use a Jupyter notebook. Use this command to start Jupyter.
Jupyter Notebook Now, run the following code
: import sys import tensorflow.keras import pandas as pd import sklearn as sk import tensorflow as tf print(f”Tensor Flow Version: {tf.__version__}”) print(f”Keras Version: {tensorflow.keras.__version__}”) print() print(f”Python {sys.version}”) print(f”Pandas {pd.__version__}”) print(f”Scikit-Learn {sk.__version__}”) gpu = len(tf.config.list_physical_devices(‘GPU’))>0 print(“GPU is”, “available” if the gpu is “NOT AVAILABLE”)
You’ll see results like this
: TensorFlow version: ‘version’ Keras version: ‘version’-tf Python 3.8.0 Pandas ‘version’ Scikit-Learn ‘version’ GPU is available
So, when you see that there is a GPU available, you successfully installed TensorFlow on your machine. Conclusion TensorFlow
is
a free and open-source software library for machine learning created by Google, and is best known for its GPU-accelerated computing speed. We saw how to install TensorFlow on Windows, Mac, and Linux. I hope this guide helps you get started with TensorFlow!
References and recommended reading:
- https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master/ML/Pytorch/Basics/pytorch_simple_CNN.py
- https://www.youtube.com/watch?v=dj-Jntz-74g
- https://github.com/jeffheaton/t81_558_deep_learning/blob/master/install/tensorflow-install-jul-2020.ipynb
- https://www.tensorflow.org/guide/gpu
- https://www.liquidweb.com/kb/how-to-install-tensorflow-on-ubuntu-18-04/
- https://www.pyimagesearch.com/2019/12/09/how-to-install-tensorflow-2-0-on-macos/
- https://towardsdatascience.com/installing-tensorflow-gpu-in-ubuntu-20-04-4ee3ca4cb75d