Getting Started with TensorFlow in Google Colaboratory

Learn two exciting technologies in 10 minutes!

George Liu
Towards Data Science

--

Photo by Franck V. on Unsplash

TensorFlow is the dominating Deep Learning framework for Data Scientists and Jupyter Notebook is the go-to tool for Data Scientists. What if you can use TensorFlow from anywhere without the hassle of setting up the environment? Better yet, what if you can use GPU to train your Deep Learning models for free?

Google Colaboratory (Colab)is the answer! It is a very exciting technology that allows Data Scientists to focus on building Machine Learning models instead of the logistics!

In this article, we’ll not only walk through the basics of using Colab, but also help you get started with TensorFlow with easy to understand examples.

Here we go!

Opening up a Colab Notebook

When using Colab for the first time, you can launch a new notebook here:

Once you have a notebook created, it’ll be saved in your Google Drive (Colab Notebooks folder). You can access it by visiting your Google Drive page, then either double-click on the file name, or right-click, and then choose “Open with Colab”.

Connecting with GitHub

The builders of Colab are so thoughtful that they even baked in the functionality of committing to Github.

To connect with GitHub, you first need to create a repo with a master branch on GitHub. Then, from the drop-down menu, select “File —Save a copy in GitHub”. You will be asked to authorize only for the first time. What’s handy is that it even allows you to include an “Open in Colab” button in your notebook like this:

Enabling GPU Support

To turn on GPU for your Deep Learning projects, just go to the drop-down menu and select “Runtime — Change runtime type — Hardware accelerator” and choose GPU:

Working with Cells

For the most part, it’s exactly the same with a local Jupyter Notebook. For example, to run a code cell, you can just press “Shift + Enter”. Check out the below frequently used keyboard shortcuts (on Windows using Chrome):

  • Run cell: “Shift + Enter”
  • Delete cell: “Ctrl + M, then D”
  • Undo: “Ctrl + Shift + Z”
  • Convert to code cell: “Ctrl + M, then Y”
  • Convert to markdown cell: “Ctrl + M, then M”
  • Save notebook: “Ctrl + S”
  • Open up the shortcut screen: “Ctrl + M, then H”

Working with Files

You can also upload data to your Colab folder. See image below:

Tensors

TensorFlow bases its name on the word “tensor”. What is a tensor anyway? In short, a multi-dimensional array. Let’s see what that means!

  • We have one single number, e.g. 6, we call it a “scalar”;
  • We have three numbers, e.g. [ 6, 8, 9], we call that a “vector”;
  • We have a table of numbers, e.g. [[6, 8, 9], [2, 5, 7]], we call that a “matrix” (which has two rows and three columns);
  • We have a table of table of numbers, e.g. [[[6, 8, 9], [2, 5, 7]], [[6, 8, 9], [2, 5, 7]]], and…we are running out of words here :( My friend, that is a tensor! A tensor is just a generalized form of arrays that can have any number of dimensions.

In TensorFlow jargons, a scalar is a rank 0 tensor, a vector is rank 1 and matrix rank 2 etc. There are three frequently used types of tensors: constant, variable, and placeholder which are explained below.

Types of Tensors

Constants are exactly what their names refer to. They are the fixed numbers in your equation. To define a constant, we can do this:

a = tf.constant(1, name='a_var')
b = tf.constant(2, name='b_bar')

Aside from the value 1, we can also provide a name such as “a_var” for the tensor which is separate from the Python variable name “a”. It’s optional but will be helpful in later operations and troubleshooting.

After defining, if we print variable a, we’ll have:

<tf.Tensor 'a_var:0' shape=() dtype=int32>

Variables are the model parameters to be optimized, for example, the weights and biases in your neural networks. Similarly, we can also define a variable and show its contents like this:

c = tf.Variable(a + b)
c

and have this output:

<tf.Variable 'Variable:0' shape=() dtype=int32_ref>

But it’s important to note that all variables need to be initialized before use like this:

init = tf.global_variables_initializer()

You might have noticed that the values of a and b, i.e., integers 1 and 2 are not showing up anywhere, why?

That’s an important characteristic of TensorFlow — “lazy execution”, meaning things are defined first, but not run. It’s only executed when we tell it to do, which is done through the running of a session! (Note that TensorFlow also has eager execution. Check here for more info)

Session and Computational Graph

Now let’s define a session and run it:

with tf.Session() as session:                    
session.run(init)
print(session.run(c))

Notice that within the session we run both the initialization of variables and the calculation of c. We defined c as the sum of a and b:

c = tf.Variable(a + b)

This, in TensorFlow and Deep Learning speak, is the “computational graph”. Sounds pretty sophisticated, right? But it’s really just an expression of the calculation we want to carry out!

Placeholder

Another important tensor type is the placeholder. Its use case is to hold the place for data to be supplied. For example, we defined a computational graph, and we have lots of training data, we can then use placeholders to indicate we’ll feed these in later.

Let’s see an example. Say we have an equation like this:

Instead of one single x input, we have a vector of x’s. So we can use a placeholder to define x:

x = tf.placeholder(dtype=tf.float32)

We also need the coefficients. Let’s use constants:

a = tf.constant(1, dtype=tf.float32)
b = tf.constant(-20, dtype=tf.float32)
c = tf.constant(-100, dtype=tf.float32)

Now let’s make the computational graph and provide the input values for x:

y = a * (x ** 2) + b * x + c
x_feed = np.linspace(-10, 30, num=10)

And finally, we can run it:

with tf.Session() as sess:
results = sess.run(y, feed_dict={x: x_feed})
print(results)

which gives us:

[ 200.         41.975304  -76.54321  -155.55554  -195.06174  -195.06174  -155.55554   -76.54324    41.97534   200.      ]

Putting it All Together

Now that we have the basics of TensorFlow, let’s do a mini project to build a linear regression model, aka, a neural network :) (The code is adapted from the example given in TensorFlow’s guide here)

Let’s say we have a bunch of x, y value pairs, and we need to find the best fit line. First, since both x and y have values to be fed in the model, we’ll define them as placeholders:

x = tf.placeholder(dtype=tf.float32, shape=(None, 1))
y_true = tf.placeholder(dtype=tf.float32, shape=(None, 1))

The number of rows is defined as None to have the flexibility of feeding in any number of rows we want.

Next, we need to define a model. In this case here, our model has just one layer with one weight and one bias.

TensorFlow allows us to define neural network layers very easily:

linear_model = tf.layers.Dense(
units=1,
bias_initializer=tf.constant_initializer(1))
y_pred = linear_model(x)

The number of units is set to be one since we only have one node in the hidden layer.

Furthermore, we need to have a loss function and set up the optimization method. The loss function is basically a way to measure how bad our model is when measured using the training data, so of course, we want it to be minimized. We’ll use the gradient descent algorithm to optimize this loss function (I’ll explain gradient descent in a future post).

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

Then we can initialize all the variables. In this case here, all our variables including weight and bias are part of the layer we defined above.

init = tf.global_variables_initializer()

Lastly, we can supply the training data for the placeholders and start the training:

x_values = np.array([[1], [2], [3], [4]])
y_values = np.array([[0], [-1], [-2], [-3]])
with tf.Session() as sess:
sess.run(init)
for i in range(1000):
_, loss_value = sess.run((train, loss),
feed_dict={x: x_values, y_true: y_values})

and we can get the weights and make the predictions like so:

weights = sess.run(linear_model.weights)
bias = sess.run(linear_model.bias)
preds = sess.run(y_pred,
feed_dict={x: x_values})

which yields these predictions:

[[-0.00847495]  [-1.0041066 ]  [-1.9997383 ]  [-2.99537   ]]

If you are curious like me, you can verify to confirm the model did make the predictions using its trained weight and bias by:

w = weights[0].tolist()[0][0]
b = weights[1].tolist()[0]
x_values * w + b

which gives us exactly the same result!

array([[-0.00847495],        [-1.00410664],        [-1.99973834],        [-2.99537003]])

Voila! A simple neural network built using TensorFlow in Google Colab! Hope you find this tutorial interesting and informative.

The notebook containing all the code can be found here. Do give it a try!

Final thoughts

Cloud computing is definitely the future of Deep Learning computing. Google Colab is clearly a future-ready product. It’s hard to imagine people still wanting to spend time setting up Deep Learning environments when we can just fire up a notebook on the cloud and start building models!

--

--

Business Savvy Data Scientist Specializing in Analytics & Machine Learning 👨‍🎓 Lifelong Learner Fascinated by Tech 👉 linkedin.com/in/georgeliu2