A guide to Google Colab - Part 2

Change hardware and compare runtimes

This notebook contains some time-consuming operations (like matrix multiplications). You can copy the following codes and run them in your local machine. It might be intresting to compare the runtime of your machine with free GPU or CPU that is provided by Google Colab.

After opening this notebook in Google Colab, you can change the default hardware:
Edit -> Notebook Settings -> Hardware Accelarator
Select the GPU and run the following codes
Select the None and run the following codes (This option will use CPU.)

In the following cell, a computation graph is built which contains some time-consuming operations.

In [0]:
import tensorflow as tf

tf.reset_default_graph()

def mean_norms():
#   Generates random matrices with different sizes. Then, multiplies them and
#     computes the mean of their norms.
  sizes = range(1000,2000,1000)  
  
  S = 0
  for dim in sizes:
    sh = [dim,dim]
    Var1 = tf.random_uniform(shape=sh, minval=0, maxval=1, dtype=tf.float16)
    Var2 = tf.random_uniform(shape=sh, minval=0, maxval=1, dtype=tf.float16)
    
    mult = tf.matmul(Var1, Var2)
    S = S + tf.norm(mult,ord='fro',axis = (0,1))
  output = S/len(sizes)
  return output

model_out = mean_norms()

Let's run the graph!

In [11]:
import time

sess = tf.Session()

start_time = time.time()
out_value = sess.run(model_out)
end_time = time.time() - start_time
print(end_time)

sess.close()
11.3840351105