Installing TensorFlow, CUDA, cuDNN for NVIDIA GeForce GTX 1650 Ti on Window 10

GPU vs CPU, training MNIST dataset.

Yan Ding
Analytics Vidhya

--

1 Anaconda

Before we begin, I want to share anaconda with you. If you are familiar with it, please skip this part. If you need to run different demos on your computer, it is necessary to set up different virtual environments. Because it is not realistic to change the version of TensorFlow or other libraries each time when you run different demos.

One of the easiest way is using anaconda. You can first download the version of your computer system(Window, MacOS or Linux) and install it. Next, using conda to create a virtual environment. In this step, you can also specify the python version. In this case, I use python 3.7. You can open your terminal and type:

conda create -n demoEnv python=3.7

2 TensorFlow

After you have created virtual environment, you should first activate it.

activate demoEnv

Now you are in the virtual environment named demoEnv. You can install packages in this environment. We can install TensorFlow now.

pip3 install --upgrade tensorflow==1.15

3 CUDA

You can find the corresponding version of CUDA and cuDNN to support the version of TensorFlow in the TensorFlow website.

In this demo, my TensorFlow is 1.15. So I should download CUDA 10.0 and cuDNN 7.4.

CUDA 10.0 can be download in this website and click the Legacy Release.

And find the CUDA Toolkit 10.0 and choose your system and the version 10.0.

After download the CUDA 10.0, install it step by step by running the exe.

4 cuDNN

The final step is to install cuDNN. We need cuDNN 7.4. You can download in this website. NVIDIA needs you to register first.

cuDNN is actually a folder which include bin, include, lib files. Find the path of your CUDA Computing Toolkit. It may in the following path:

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0

Paste the bin, include and lib files of cuDNN into the corresponding place of CUDA 10.0. You can also refer this step in this website.

5 Environment Path

Now you have completed to install TensorFlow, CUDA and cuDNN. To ensure that TensorFlow can find CUDA, you should go to the system environment and add the CUDA path. In my system, I add these two directories to Path.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\binC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\libnvvp

6 Train a CNN model with your GPU

Now we can use the GPU to train models. If you use PyCharm, you can go to settings and add project interpreter with the python.exe under demoEnv.

We can compare the speed of GPU and CPU by training a MNIST tutorial with Convolutional Neural Network. As we all know, GPU is much faster in doing convolution than CPU. So it should be much faster to train CNN. Run the script as follows in the virtual environment which we just created. It will train with GPU.

import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
num_classes = 10
input_shape = (28, 28, 1)

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Scale images to the [0, 1] range
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")


# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = keras.Sequential(
[
keras.Input(shape=input_shape),
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(num_classes, activation="softmax"),
]
)

model.summary()
batch_size = 128
epochs = 5

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)

It took about 13 seconds to finish training the model in MNIST dataset in 5 epochs and the validation accuracy is about 98.97% as follows.

I also run the same script with CPU i7. It took about 1 minute and 30 seconds to finish training in MNIST dataset in 5 epochs as follows. And the validation accuracy is 99.05%, very close to the result of GPU.

7 Conclusion

As you can see, GPU is much faster than CPU when training CNN, 13s compared to 90s. To sum up, I succeeded to set up TensorFlow 1.15 with Python 3.7, CUDA 10.0 and cuDNN 7.4.

Moreover, I created another virtual environment and succeeded to install TensorFlow 2.3 with Python 3.6/3.8, CUDA 10.1 and cuDNN 7.6 in this GPU GeForce GTX 1650 Ti. It looks like as follows,

The main problem is when I was trying to install Tensorflow 2.3, I cannot find cuDNN 7.4 according to Build from source. So I try to use cuDNN 7.6, it works. If you have any problem, leave comments below. Thanks.

More tips:

  1. My another GPU GeForce GTX 1060 6GB, I used Tensorflow 1.15, Python 3.7, CUDA 10.0, but the version of cuDNN is 7.6 (for CUDA 10.0), not the same with Tensorflow website. And I succeeded to train the model with GPU.
  2. I also create a new virtual environment in the above PC, with GeForce GTX 1060 6GB. I used Tensorflow 2.2.0, Python 3.6, CUDA 10.1, cuDNN 7.6 (for CUDA 10.1), and I can train the model with GPU as well.

--

--