Install Tensorflow 2.0

Reading Time: 4 minutes

Hello everyone. I have some hot news for you: Tensorflow 2.0 has just been announced! This upgrade may come with the biggest change you have ever seen, not just the library itself but also the Tensorflow’s home page. You heard it right, that’s a complete renewal, and more importantly, for good!

Without further talking, let’s catch the trend while it’s still fresh and new. In this post, I will guide you through how to install Tensorflow 2.0 as quickly as possible using Docker. Then we will test it out by creating an RNN model which can generate some text. Sounds cool, right? Let’s do it!

Prerequisites

In order to get the most out of today’s post, I assume (highly recommend) that you:

  • Have a PC/laptop which has GPU(s)
  • Have Ubuntu 18.04 or 16.04 installed
  • Installed Docker (you can read my previous post here)
  • Understand how to generate text with RNNs (you can read my previous post here)

If you don’t have any problem with anything above, then we’re ready to go.

Install Tensorflow 2.0

Now is what we need to do to get Tensorflow 2.0 up and running. Because we will use Docker to run Tensorflow image, the only modification we have to make to our machines is to update the NVIDIA driver.

Tensorflow 2.0 requires CUDA 10, which requires NVIDIA driver of at least version 410. If you already have the newest NVIDIA driver, feel free to skip the next section.

Install NVIDIA driver

Below is the instruction for Ubuntu 18.04 which I myself tested on my machine. You can find the instruction for Ubuntu 16.04 in the reference section. Although I haven’t tested yet, I think it should work without problems.

Before installing the new NVIDIA driver, we need to make sure that we removed the old one to prevent unwanted conflicts:

Next, we will add necessary NVIDIA package repositories:

If we didn’t see any error, we can now go ahead and install the new NVIDIA driver:

On my machine, I came across an error telling me that I didn’t have some packages on which the new NVIDIA driver relies. To solve that, I just installed the first package in the list and luckily, the rest of them were automatically collected too:

Now let’s install the new NVIDIA driver again. This time there should not be any problem:

It will take a while for the new driver to be installed. After it finishes, reboot your computer. Then, let’s check if the new driver is up and ready:

Congratulations! You now have the latest NVIDIA driver.

Install NVIDIA Docker

Many of you might already have installed NVIDIA Docker before. But, surprisingly enough, it has gone when we uninstalled the old NVIDIA driver. So it’s time to bring it back again. Don’t panic, we just need a few simple steps (assuming that you already installed Docker).

There are two versions of NVIDIA Docker. We’re gonna install the newer version since it’s … newer and more importantly, CUDA 10 can only run on NVIDIA Docker version 2.

First, let’s remove NVIDIA docker version 1 (if exists) and all the GPU containers:

Then we will add the necessary package repositories:

Finally, we will install the NVIDIA Docker version 2:

And we’re done. NVIDIA Docker is now ready to serve.

Run the Tensorflow 2.0 container

So far we have upgraded the NVIDIA driver and re-installed NVIDIA Docker, it’s time to pull the Tensorflow 2.0 image and run the container. In fact, we just need to do the latter, since Docker is smart enough to pull the missing image for us:

For those who are not familiar with Docker, the -it flag means interactive, which lets you interact with the container’s environment, just like what you normally do in the terminal. Without that flag, Docker will just execute the command and exit the container immediately.

Next, the –rm flag means remove, which will delete the container after you exit. Since we are just testing the image, we want it to be removed after we are done.

For my daily work, I would create a container as follow:

It will take a while to download the image. When the download is done, we will be inside the container’s environment. The first thing to do is to have a taste of Tensorflow 2.0 – always be eager:

So far so good. It looks like that Tensorflow 2.0 is working. But wait, it’s just it? Just print out some randomly initialized tensors and we’re done? Of course, no! In fact, I have some giveaway today!

Generate text with Tensorflow 2.0

Remember my previous post on how to generate text with Tensorflow? For those who haven’t seen it yet, here is the link: Text Generation With Tensorflow. I have created a new script for Tensorflow 2.0 in the same repository (which you can find here). Now, let’s have some fun with RNNs!

If you haven’t cloned my repository yet, here is what you need to do:

The training should be started immediately and after a short while, we will start to see some cool text which the RNN model generated. It will look like below:

The result above should be enough to guarantee that we now have a perfectly working Tensorflow 2.0 environment.

Final words

Congratulations again on successful Tensorflow 2.0 installation. By utilizing the power of Docker, setting up CUDA environment for Tensorflow is no longer a painful task. Now it’s time to get used to the new library. It won’t be hard if you are familiar with Keras and Eager Execution. Also, bear in mind that Tensorflow 2.0 is just in alpha, and bugs are likely to appear πŸ˜‰

I think you noticed already, that I didn’t explain a line of the text generation model above. Don’t worry, a more detailed explanation will come in the next blog post.

That’s all for today. Thank you for reading and I will see you soon.

Reference

  • Tensorflow’s instructions for installing CUDA: link.
  • Tensorflow 2.0 API: link.

Trung Tran is a Deep Learning Engineer working in the car industry. His main daily job is to build deep learning models for autonomous driving projects, which varies from 2D/3D object detection to road scene segmentation. After office hours, he works on his personal projects which focus on Natural Language Processing and Reinforcement Learning. He loves to write technical blog posts, which helps spread his knowledge/experience to those who are struggling. Less pain, more gain.

Leave a reply:

Your email address will not be published.