DEV Community

Philipp Gysel
Philipp Gysel

Posted on • Updated on

Start Using TensorFlow 2

What this guide is about:

  • Start using TensorFlow 2 in a matter of 2-3 minutes.
  • 3 alternatives to install TensorFlow:
    • Jupyter Notebook hosted in the Google cloud
    • run as Docker image
    • install as Python package

Jervis Bay

Introduction to TensorFlow

TensorFlow is one of the most widely used frameworks for deep learning. The Buzz Word Deep Learning is likely something you’ve heard a lot lately😊 So say you’re interested in exploring the topic of Deep Learning, but you don’t know where to start. Well this guide is a great starting point for you! I cover the three possible steps for you to install and start using TensorFlow 2. With this guide you can actually start using TensorFlow in a matter of 2-3 minutes.

Deep Learning is the term used for really deep neural networks which are capable of solving very challenging problems. A fun project I found is from Kevin Hughes: He used deep learning to drive Mario Kart. Amazing, isn’t it? Well you can check out the following video on Youtube:

An additional fun fact about this project is that it was realized using TensorFlow. So, here’s some basic information about TensorFlow: It’s open source, written by Google, and used by many companies. Google itself uses the framework intensively, for example for images on your Android smartphone: You can go to your picture gallery and search pictures by keyword such as “Mountains” or by person. The great thing about TensorFlow is that it can be used for both experimenting / training as well as to create predictions in a robust production environment.

3 Ways to install TensorFlow

So now let’s make sure you can use TensowFlow on your own machine. There 3 ways to install TensorFlow.

First though, let me tell you about my own experience starting with deep learning, which was all the way back in 2015. Back then, the most widely used framework was Caffe, and installing it was a pain, I can tell you. There were around a dozen dependencies required, all of which were hard to install themselves. Add to that some technical challenges with our GPU… As you can imagine, this made the installation even harder, so I spent around one week for analyzing error messages until everything finally worked.

Thankfully, technologies and deep learning frameworks have progressed a lot since then. The good news: using deep learning frameworks nowadays is much, much simpler😊. In fact, you can even use TensorFlow without any installation whatsoever! So, let’s cover the 3 options you have:

Option #1: No installation required (Google Colab)

This option allows you to run TensorFlow without any installation, all you need is a web browser! Google Colab allows you to run TensorFlow in a Jupiter Notebook and your code runs on the powerful Google Cloud. Optionally you can train networks on the GPU, which makes Colab an especially attractive option if you don’t have a GPU on your own machine.

First, head over to the TensorFlow tutorials site. Next click on any tutorial you are interested in:

TensorFlow Tutorials

Second open the tutorial in Colab:

Run in Colab

This will open a Jupiter Notebook with documentation and code snippets. The tutorials contain all the code required to train a network. You can run individual code snippets one by one:

Run Code Snippet

You can also add your own code or create new Notebooks through the menu File -> New Notebook. And that’s all, it’s that simple to start training neural networks! 😊

Option #2: Run TensorFlow using Docker

Colab is a great way to start using TensorFlow. However, in Colab it’s a bit more complicated to get your own data in and out of the Google Cluster. There’s good news though: There’s a way for you to run TensorFlow on your own machine, without the need to do the full installation of TensorFlow. That’s where the official TensorFlow Docker Image comes into play. Docker allows you to run an application inside a virtual environment where all necessary dependencies are preinstalled. This way you only need to install Docker, download the TensorFlow Docker image, and you’re already good to go!

First make sure you have Docker installed. You can find the installation instructions for Docker Desktop Tools here. Now you should make sure your Docker installation is working properly by querying your Docker version:

$ docker --version
Docker version 19.03.8, build afacb8b
Enter fullscreen mode Exit fullscreen mode

Second, you will download the latest TensorFlow Docker image as follows:

$ docker pull tensorflow/tensorflow:latest-py3
Enter fullscreen mode Exit fullscreen mode

Third, run TensorFlow inside the Docker image:

$ docker run -it tensorflow/tensorflow:latest-py3 /bin/bash
Enter fullscreen mode Exit fullscreen mode

Now you are inside the Docker container. This is a Linux operating system; you can see the following folder structure typical for Linux distributions:

root@33201978194e:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
Enter fullscreen mode Exit fullscreen mode

For this tutorial we will use TensorFlow via interactive Python session, so start a Python session:

$ root@33201978194e:/# python
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Enter fullscreen mode Exit fullscreen mode

Now you can issue the same commands as in the Colab (see previous section). First you import TensorFlow:

>>> import tensorflow as tf
Enter fullscreen mode Exit fullscreen mode

Next you would build your neural network and train it. Note that you might get some warnings as you complete one of the tutorials, but don't worry about them for the moment.

Alternatively you can also create Python scripts and run them in the Docker Container. One last point you might wonder about: How do you transfer Python scripts or training data between your native operating System and the Docker Container? The answer is the Docker --mount option. You initialize mount points when starting the Docker container:

$ docker run -it --mount type=bind,source=path1,target=path2 tensorflow/tensorflow:latest-py3 /bin/bash
Enter fullscreen mode Exit fullscreen mode

In the above example, path1 is the absolute path to the folder on your own OS, and path2 is the absolute path inside the Docker image.

Option #3: Install TensorFlow as Python Package

You can also run TensorFlow directly on your machine's operating system without a Docker image. This requires you to install TensorFlow on your machine, so this is the most complicated way to use TensorFlow. Once you have the installation completed, you will be able to use TensorFlow directly without web browser or Docker.

As prerequisite, you need Python and pip installed on your machine. Use pip to install TensorFlow:

# Requires the latest pip
$ pip install --upgrade pip

# Current stable release for CPU and GPU
$ pip install tensorflow
Enter fullscreen mode Exit fullscreen mode

After the installation has completed (hopefully without errors), you can use TensorFlow from your terminal.

Now let’s run a TensorFlow Hello World Example

Cool, so now you can use TensorFlow, yeyy. So where to you go from here? Luckily there’s an abundance of great hello-world tutorials on the web. You can start with the official tutorials and then use more in-depth guides on areas you are interested in.

Thanks so much for your interest in my tutorial! If it was useful for you, please remember to “heart” ❤️ it so other people can also benefit from it. If you have any suggestions or questions, please leave a comment!

Top comments (0)