Setting up a Google Cloud instance for Deep Learning

Piyush Goel
7 min readJun 1, 2018

--

In this article I’ll walk you through setting up a google cloud computing instance and then show you how to install tensorflow with gpu support. Let’s start with a cloud instance first. I am assuming that you already have a google account or know how to make one.

Google Cloud Computing Instance

Go to Cloud Platform Console and follow the instructions to setup your cloud. [You may skip this step for now] When prompted to create a new project, enter a name and a project ID that will be used to identify your project, or accept the defaults. More can be found here.

Now, select the desired project or create new one by clicking on the select project (may have some project name if you’re already inside one) option in the top blue bar of the Cloud Console page, as shown in the image below.

Now, there are two ways to create an instance one is through the use of GUI on the Cloud Platform Console and the other way is using the Google Cloud Shell (the CLI way). I will be discussing both here.

The GUI Way

For creating a new instance click on the menu button on the top left then go to Compute Engine, then VM instances. Now, click on the Create Instances button on the top. The page will appear something like this.

Enter a desired name for that instance and select the region. Note that each region doesn’t offer all the hardware (Go to this link to see what each zone offers).The region and zone you select should have GPUs available (Go to this link to see the zones which offer GPUs). Click on the customize button in the Machine type part to choose the cpu cores and the memory. Then click GPUs to add Nvidia GPUs to your instance. Now click on the change button in Boot Disk and choose the desired OS and storage disk type and capacity.

I chose the following configuration for my instance -

  • us-west1 region and zone-b (recommended for GPU support)
  • 8 CPU cores with 30GB memory (you should be fine with less memory and cores as well)
  • Ubuntu 16.04 LTS (recommended)
  • SSD persistent disk (recommended for faster transfers) with 50GB capacity

Select the Allow HTTP and HTTPS Traffic check boxes in the firewall part and then click create.

Your Instance is now ready to be used. Click on the SSH button to the right of the name of your instance to access the CLI of your instance.

The CLI Way

Note: The CLI I’m talking about in this section is no way related to your instance. Just keep this in mind to avoid any confusions.

Start Google Cloud Shell (can be activated by clicking the <Activate Cloud Shell> Button in the Cloud Platform Console on the top right corner in the blue bar). Or use the google cloud shell directly from the CLI of your computer using Google SDK — you can find the steps to set it up here.

Now, to create an instance with the same configuration as in the GUI method you have to execute the following command.

gcloud beta compute --project=your-project-name-here \
instances create instance-name-here \
--zone=us-west1-b \
--machine-type=n1-standard-8 \
--subnet=default \
--maintenance-policy=TERMINATE \
--accelerator=type=nvidia-tesla-k80,count=1 \
--tags=http-server,https-server \
--image=ubuntu-1604-xenial-v20180522 \
--image-project=ubuntu-os-cloud \
--boot-disk-size=50GB \
--boot-disk-type=pd-ssd

Each of the flags are self-explanatory and may be changed as per the requirements. But, don’t forget to change the project and instance name.

Changing some settings

Note: Making these changes will help you to use Jupyter notebook with ease. You can ignore this section if you don’t wish to use Jupyter notebook.

Now stop your instance by clicking on the three dots to the right of your instance name in the VM Instance page (the same page where you created the instance).

Click on the menu button on the top left corner, scroll down to the networking section, click on VPC network then on External IP addresses. Change the type of your instance from ephemeral to static.

Now go to Firewall rules in VPC network as well, and click on Create Firewall Rule. Fill up the information as shown in the image below. You can change the port address (5100 in this case) to whatever you wish and remember it because it will be required later.

Note: The firewall rule needs to be added only once for all the instances in a project, unless you select different target.

Installing CUDA with cuDNN

If you’re going to install Tensorflow (or any other Deep Learning Library) with GPU support then you are will have to install these. CUDA is a parallel computing platform and programming model developed by NVIDIA for general computing on graphical processing units (GPUs). With CUDA, developers are able to dramatically speed up computing applications by harnessing the power of GPUs.

The NVIDIA CUDA Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers. cuDNN is part of the NVIDIA Deep Learning SDK.

Tensorflow is currently compatible with CUDA v9.0 and less, cuDNN v7 and less. Nvidia already has pretty good guide on how to setup both CUDA and cuDNN. You can either follow those guides and skip this section. If you’re lazy to read and are on Ubuntu 16.04 with a supported Nvidia GPU just follow along with me. Execute the following commands on the command line of your instance by clicking the SSH button to the right of the instance name.

wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda

Tip: Press ‘q’ if you don’t want to read the whole terms and conditions while installing CUDA. On being prompted to select weather to install a component press ‘y’ if you don’t know what to do. DO NOT change the install location.

For cuDNN go to the cuDNN page, create your Nvdia Developer Account and then go the download section, select the appropriate OS version and correct version of cuDNN for the correct version of CUDA (in my case it was cudnn v7 library for cuda v9.0 and linux). Copy the download link and the execute the following commands.

wget <paste-the-link-you-copied-here>
tar -xzvf <name-of-the-downloaded-file>
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h

CUDA and cuDNN should be installed and ready to be used now.

Installing Tensorflow

Google has a pretty good guide already on how to install tensorflow. But here are the steps.

You don’t need to install python on Ubuntu since it’s already installed. To check the versions installed.

python --version # for python 2
python3 --version # for python 3

Install pip and python tools.

# For Python 2.7
sudo apt-get install python-pip python-dev
# For Python3.x
sudo apt-get install python3-pip python3-dev

Install tensorflow with gpu support. ( I’m also going to avoid setting up a different virtual environment for tensorflow installation. You can look up the official guide if you want to set it up.)

# For Python 2.7
pip install --upgrade tensorflow-gpu
# For Python3.x
pip3 install --upgrade tensorflow-gpu

Note: Remove the ‘-gpu’ if you don’t want gpu support.

Installing Jupyter Notebook

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Using it on the cloud requires changing some network. Just run the the command below to install jupyter notebook.

pip install jupyter # for python 2
pip3 install jupyter # for python 3

Now generate the config file of jupyter with the following command.

jupyter notebook --generate-config

The config file will be generated if it wasn’t existing already and it’s path will be displayed. Use that path to edit the config file and add the following lines to the file. Replace 5100 with the port address you used earlier.

c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 5100

Setting up password for Jupyter Notebook

This optional but highly recommended for more security, since anyone with the Instance External IP and the port address will have the access to the notebook. To set up a password for the notebook just run the following command and enter the password when prompted to do so.

jupyter notebook password

Accessing the Notebook

Execute the following command to start the server for jupyter notebook.

jupyter notebook

Now, just go to the following address in your browser to access the notebook.

<external-static-ip>:5100

Write the external IP address of your instance in the place of <external-static-ip> and again, don’t forget to replace 5100 with the port address you used.

You might be prompted to set up a password for jupyter, just following along the steps.

Now, you should be able to use tensorflow with gpu support on Jupyter Notebook on your Google Cloud Instance, to make some deep learning models and train them.

Tip: Don’t forget to stop your instance if it’s not in use, since you’ll be charged if the instance is running.

--

--

Piyush Goel

Computer Scientist with a passion for math and problem-solving.