Using CUDA & cuDNN with VS Code on your NVIDIA RTX Laptop: Installing GeForce Drivers and Importing TensorFlow
If you're trying to get started with deep learning using Visual Studio Code (VS Code) on your NVIDIA RTX laptop, this article will guide you through the process of installing GeForce drivers, setting up CUDA and cuDNN, and importing TensorFlow. By the end of this article, you'll have a solid understanding of the key concepts required to harness the power of your GPU for machine learning tasks.
Installing NVIDIA GeForce Drivers
-
Download the correct driver
Visit the official NVIDIA GeForce drivers download page (https://www.nvidia.com/Download/index.aspx) and input your laptop's graphics card model. Ensure you download the 'Game Ready Driver' for the best performance.
-
Install the downloaded driver
Once the download is complete, run the installer and follow the on-screen instructions. Restart your system if prompted.
Setting up CUDA and cuDNN
-
Find compatible CUDA and cuDNN versions
Visit TensorFlow's official website (https://www.tensorflow.org/install/source#gpu) to find the compatible CUDA and cuDNN versions for your TensorFlow installation.
-
Download and install CUDA
Navigate to the NVIDIA CUDA website (https://developer.nvidia.com/cuda-toolkit) and download the appropriate version. Run the installer and select 'Custom' install, enabling 'CUDA Toolkit' and 'Samples'.
-
Set up the CUDA environment variables
Add the CUDA toolkit bin and library directories to your system's PATH:
<p>echo 'export PATH=/usr/local/cuda-11.2/bin${PATH:+:${PATH}}' >> ~/.bashrc</p> <p>echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64 /usr/local/cuda-11.2/lib64/stubs' >> ~/.bashrc</p>Replace '/usr/local/cuda-11.2/' with the path of your installed CUDA toolkit.
-
Download and install cuDNN
Get the compatible cuDNN version from TensorFlow's website. Download cuDNN from the NVIDIA developer website (https://developer.nvidia.com/cudnn). Select 'Download cuDNN', 'cuDNN Library for Linux', and the appropriate version. Extract the downloaded archive.
-
Copy cuDNN libraries
Copy the cuDNN library files to the CUDA library folder:
<p>sudo cp -P cuda/lib64/\* /usr/local/cuda-11.2/lib64</p>Replace '/usr/local/cuda-11.2/' with the path to your CUDA toolkit folder.
Configuring VS Code and TensorFlow
-
Install the Python extension in VS Code
From the VS Code Extensions tab, search for 'Python' and install the Microsoft Python extension.
-
Create a virtual environment
Create a new virtual environment for your deep learning project; this can be done by running the following command:
<p>python3 -m venv .venv</p> -
Activate the virtual environment
Activate the virtual environment using the following command:
<p>source .venv/bin/activate</p> -
Install TensorFlow
Install TensorFlow with GPU support within the virtual environment:
<p>pip install tensorflow-gpu</p> -
Verify the installation
Verify that TensorFlow correctly utilizes the GPU by running the following script:
<p>import tensorflow as tf</p> <p>print("TF Version:", tf.__version__)</p> <p>print("GPU is available:", tf.test.is_gpu_available())</p>This code should output the TensorFlow version and indicate that a GPU is available. With this setup, you can now efficiently train deep learning models in VS Code, utilizing your NVIDIA RTX laptop's GPU.
Summary
- Installed NVIDIA GeForce drivers
- Set up CUDA and cuDNN for TensorFlow
- Integrated TensorFlow in VS Code with GPU support
References
- TensorFlow's GPU setup guidelines: https://www.tensorflow.org/install/source#gpu
- NVIDIA CUDA Toolkit download: https://developer.nvidia.com/cuda-toolkit
- NVIDIA cuDNN download: https://developer.nvidia.com/cudnn
- Microsoft's Python extension for VS Code: https://marketplace.visualstudio.com/items?itemName=ms-python.python