How To Use Your GPU In Jupyter Notebook

IDEs and code editors are your best friends when you want to write, modify, and execute your self-programs and codes. Jupyter Notebook is one such IDE that’s amazing for coders … Read more

Taylor Bell

Taylor Bell

Published on Jun 25, 2024

How To Use Your GPU In Jupyter Notebook

IDEs and code editors are your best friends when you want to write, modify, and execute your self-programs and codes. Jupyter Notebook is one such IDE that’s amazing for coders who specialize in data analysis, web scraping, machine learning, and AI-powered projects. For the average coder who’s into data science, Jupyter Notebook serves as the perfect companion as it lets you create interactive documents for everything from jotting down notes to compiling complex codes. Sure, Jupyter Notebook will work well on most modern laptops, but you’re going to have a tough time if you attempt to perform complex AI and machine learning tasks on a mere CPU.

Luckily, you can fine-tune Jupyter Notebook to relegate the demanding deep-learning workloads to your powerful graphics card instead of the processor. However, you’ll have to go through several steps, including setting up Python libraries, creating coding environments, and installing drivers before you can get the IDE to leverage your GPU.

A Windows 11 screenshot with the Anaconda and Jupyter Notebook apps open in the background with the Jupyter logo overlaid on top.

Related

How to use Jupyter Notebook on Windows, Linux, and macOS

Want to get the most out of learning Python? Get familiar with Jupyter Notebooks

Installing Python

install python

This step may sound redundant if you’re already knee-deep into programming, but you’ll need to install Python on your PC to use GPU-accelerated AI in Jupyter Notebook. Simply download the Python.exe file from the official website and click on the install button after granting admin privileges to the installer.

Two laptops with a banner stating Python

Related

How to install Python on Windows, Linux, and macOS

If you want to install Python and get started with development, we have a handy quick-start guide to run you through the basics.

For most users, I recommend choosing the Disable Path Length Limit to avoid future headaches caused by the 260-character limit on the length of file paths set by Windows 11.

Installing Miniconda

Miniconda is a toolkit that contains important Python libraries, environments, and packages necessary to enable your GPU. It also lets you create a Jupyter Notebook.

  1. Download the setup.exe file from the official website and run it with admin privileges.
  2. Choose the I Agree option when the installer asks you to agree to the licensing terms and hit Next.
    A screenshot of the Miniconda installer depicting the license agreement

  3. Choose the directory where you wish to install Miniconda, and click on the Next button.
    A screenshot of the Miniconda installer depicting the destination folder

  4. Hit the Install button and press Finish once the installation is complete.

Setting up a Conda environment

Now that you’ve installed Python and Miniconda, it’s time to configure a coding environment for your machine learning projects. I recommend creating a separate enviroment as we’ll be using older packages in this tutorial.

Since the latest version of TensorFlow doesn’t work on Windows 11 systems that don’t have WSL pre-configured, you’ll have to install a build that’s older than TensorFlow v2.11. The same goes for Python, so you’ll have to downgrade to Python 3.9 in the new Conda environment.

  1. Type miniconda in the Windows Search Bar and pick the Run as Administrator option under the Anaconda Powershell Prompt.
    A screenshot of Windows 11 with the Anaconda Powershell Prompt highlighted

  2. Paste the following code into the terminal and press enter:
    conda create --name my_env python=3.9 -y
    A screenshot of the Anaconda Powershell Prompt showcasing the command to create a new environment

  3. Activate the newly created environment using the following command:
    conda activate my_env
    A screenshot of the Anaconda Powershell Prompt showcasing the command to activate an environment

  4. Run this command to install the cuDNN library and CUDA drivers:
    conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0 -y
    A screenshot of the Anaconda Powershell Prompt showcasing the command to install the cuDNN library and CUDA drivers

  5. Install the TensorFlow library by running the following command:
    pip install "tensorflow<2.11"
    A screenshot of the Anaconda Powershell Prompt showcasing the command to install TensorFlow

Installing Jupyter Notebook

Finally, you can set up a local Jupyter Notebook server containing all your project files.

  1. Run this code inside the Anaconda Powershell Prompt:
    pip install jupyter notebook -y
    A screenshot of the Anaconda Powershell Prompt showcasing the command to install Jupyter Notebook

  2. Open the Jupyter Notebook server by typing:
    jupyter notebook
    A screenshot of the Anaconda Powershell Prompt showcasing the command to open Jupyter Notebook

You can check if the Miniconda coding environment works with the GPU. To do so,

  1. Click on the New button and choose Notebook.
    A screenshot of Jupyter Notebook with the options to create a new notebook highlighted

  2. Select Python 3 (ipykernel) as the kernel.
    A screenshot of Jupyter Notebook with the options to select the kernel highlighted

  3. Copy these lines of code inside the newly created Notebook:
    • import tensorflow as tf
    • gpus=tf.config.list_physical_devices("GPU")
    • if gpus:
    •  for gpu in gpus:
    •  print("Found a GPU with the name:", gpu)
    • else:
    •  print("Failed to detect a GPU.")
      A screenshot of Jupyter Notebook with the code to check for the GPU highlighted

  4. Press the Run button.

If Jupyter Notebook displays a graphics card as the output, it means the process was successful!

Running Jupyter Notebook on a GPU

Windows 11 screenshot that shows Jupyter Notebook within a Google Chrome tab.

Once you’ve verified that the graphics card works with Jupyter Notebook, feel free to use the import-tensorflow command every time you wish to run your codes on the GPU. If Jupyter Notebook is unable to detect your graphics card, you can retry the same procedure in another Miniconda environment. I also recommend installing the same versions of the CUDA drivers and the cuDNN and TensorFlow libraries I’ve used in this tutorial if you run into compatibility errors.

In case your projects take eons to compile, your graphics card could be lacking in horsepower. Upgrading to a better GPU is an easy fix that will give your PC the much-needed boost to run complex AI and deep learning algorithms.

A picture of two Nvidia graphics cards with the legend Best gpus for deep learning

Related

Best GPUs for deep learning in 2024

Whether you want to get started with image generation or tackling huge datasets, we’ve got you covered with the GPU you need for deep learning tasks.

Partager cet article

Inscrivez-vous à notre newsletter