Wednesday, September 21, 2022

(Python) Enabling the Nvidia GPU - Tensor Flow and Keras Utilization

Though this website does not typically feature articles related to Tensor Flow or Keras (machine learning libraries), due to reader requests, in this entry, I will illustrate how to enable Nvidia GPU utilization as it pertains to the aforementioned packages.

I will be operating under the following assumptions:

1. You are in possession of a Windows PC which contains a Nvidia GPU. 

2. You are relatively familiar with the Tensor Flow and Keras libraries. 

3. You are utilizing the Anaconda Python distribution.

If the above assumptions are correct, then I have designated 4 steps towards the completion of this process:

1. Installation of the most recent Nvidia GPU drivers. 

2. Installation of the “tensorflow-gpu” package library.

3. Installation of the CUDA toolkit.

4. Trouble-shooting.

Installing the most recent Nvidia drivers:

This step is relatively self explanatory. If you are in possession of a computer which contains a Nvidia GPU, you should have the following program located on your hard drive: “GeForce Experience”. Depending on the type of Nvidia GPU which you possess, the name of the program may vary. To locate this program, or a similar program which achieves the same result, search: “Nvidia”, from the desktop start bar.

After you have launched the Nvidia desktop interface, you will be asked to create a Nvidia Account. To achieve this, enter the appropriate information within the coinciding menu prompts. Once this has been completed, follow the link within the conformation e-mail to finalize the creation of your user account.

With your new Nvidia account created, you will possess the ability to access the latest driver updates within the Nvidia console interface. Be sure to update all of the drivers which are listed, prior to proceeding to the next step.


Installing the TensorFlow GPU Package Library

Completing this simple pre-requisite can be achieved by either:

A. Running the following code within the Jupyter Notebook programming environment:

import pip

pip.main(['install', 'tensorflow-gpu'])

B. Running the following code within the “Anaconda Prompt”:

conda install tensorflow-gpu

To reach the “Anaconda Prompt” terminal, type “Anaconda Prompt” into the Windows desktop search bar.

Installing the CUDA toolkit

You are now prepared to complete the final pre-requisite, which is the most complicated of all of the required steps.

First, you must click the link below:

https://developer.nvidia.com/cuda-downloads

The address above will direct you to the Nvidia webpage.

Select the appropriate options which pertain to your operating system from the list of selections. Doing such, will present a download link to the version of the CUDA software which is best suited for your PC.


The rest of the installation process is relatively straight-forward.

The product of the download will produce the file below:


(File name will vary based on operating system and version selection)

Double clicking this file icon will begin the installation process.


After clicking through the associated options, the following screen should appear:


If you do not have Microsoft Visual Studios installed on your PC, you will be presented with an installation error. However, if you do not intend to utilize the CUDA software for development related to such, you can continue the installation process without further hesitation.

Once the process is fully completed, the following shortcut icon should appear on your PC’s desktop:


I would now advise that you re-start your PC prior to implementing GPU utilization within your machine learning projects.

Trouble Shooting

I’ve found that GPU enabled Tensor Flow projects, at least from my experience, tend to be more error prone from session to session. However, I accept this shortcoming, due to the significant speed increase enabled by GPU utilization.

Utilizing the newly installed GPU implementation is relatively simple, as the implementation is automatically assumed within the model structure. Meaning, that the alteration of pre-existing machine learning project code is un-necessary as it pertains to GPU optimization. If you run code which has been previously created for the purpose of utilizing Keras and Tensor Flow library implementation, then the computer will automatically assume that you now desire to have the analysis performed through the GPU hardware architecture.

To ensure that GPU functionality is enabled, you may run the following lines of code within the Anaconda coding platform:

from tensorflow.python.client import device_lib

from keras import backend as K

print(device_lib.list_local_devices())

K.tensorflow_backend._get_available_gpus()

This should produce output which includes the term: ‘GPU’. If this is the case, then GPU utilization has been successfully enabled.

If, for whatever reason, errors occur as it relates to keras or tensorflow implementation following the installation of the prior programs, try completing any of the following steps to remedy this occurrence.

1. Restart the Anaconda platform and Jupyter Notebook.

2. Uninstall and re-install both the tensorflow and tensorflow-gpu libraries from the Anaconda Prompt (command line). This can be achieved by utilizing the code below:

conda uninstall tensorflow

conda uninstall tensorflow-gpu

conda install tensorflow

conda install tensorflow-gpu


Assuming that these remedies addressed and solved the previously present issue, assuming that an issue previously existed, then you should be prepared to experience the blazing speed enabled by Nvidia GPU utilization.

That’s all for this entry.

Stay busy, Data Heads!