Skip to main content

How to set Tensorflow 2 on Colab.research.google.com


This simple tutorial show you how to set Tensorflow 2 version in google Colab. It is simple step by step, picture by picture guide. 

I try to find a way to set Tensorflow 2 version on Colab. Are you using Colab? I love it.

Set a version of Tensorflow

To set the Tensorflow version in colab.research.google.com
  1. execute following code %tensorflow_version
  2. go to top menu - Runtime - Restart and Run all
 [0]:%tensorflow_version 2.x

colab.research.google tensorflow 2 version
Point 1 set Tensorflow version

tensorflow 2
point 2 restart runtime

Test the Tensorflow 2 version 

After set version and restart of runtime try to use Tensorflow 2.0 and print version. If you have version of Tensorflow 1.x somethink weht wrong. Add new code section as follows

Tensorflow 2 on Colab.research.google.com


In [0]:
from __future__ import absolute_import, division, print_function, unicode_literals
try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
print(tf.__version__)

# This looks like a vector right ? 
tensor_OneDimension = np.array([1,2,3,4,5])

# The acess tensor elenents by index  
first_element = tensor_OneDimension[0]
last_element = tensor_OneDimension[4]

print("first element :",first_element)
print("second element:",last_element)
print("--------------------------------")

# This tensor looks like an Matrix. 
tensor_TwoDimensionMatrix = np.array([[1, 2],[3, 4]])
# Access matrix elements by index 
print(tensor_TwoDimensionMatrix)
print("--------------------------------")
print(tensor_TwoDimensionMatrix[0][0])
print(tensor_TwoDimensionMatrix[1][0])
print(tensor_TwoDimensionMatrix[0][1])
print(tensor_TwoDimensionMatrix[1][1])
print("--------------------------------")

# print all elements of first Row and second Row
print("Print 1 row by [:][0]",tensor_TwoDimensionMatrix[:][0])
print("Print 2 row by [:][1]",tensor_TwoDimensionMatrix[:][0])
print("--------------------------------")
# print columen by
print("Print 1 row by [0][:]",tensor_TwoDimensionMatrix[:][0])
print("Print 2 row by [1][:]",tensor_TwoDimensionMatrix[:][0])
print("--------------------------------")

Expected result of print(tf.__version__)

Tensorflow  2 on colab.research by google
print Tensorflow version 2.1.0


Comments