Tensorflow2.0 HelloWorld using google colab.
In this article, we use the most popular deep learning framework TensorFlow and we will take a basic hello world example to do this example you no need to set up a local environment on your machine.
We are using google Colab If you are not aware of what it is? here you go and check out my article on the same Colab getting started!!
Train deep neural network free using google colaboratory.medium.com
Now visit https://colab.research.google.com/ and you will see
Brief About Colab:
Once you opened the Colab and if you are already logged in Gmail account.
The google colab is available with zero configuration and free access to GPU and the best part is it sharable. The Google Collaboration is free service for the developers to try TensorFlow on CPU and GPU over the cloud instance of Google. This service is totally free for improving Python programming skills, developers can log in with their Google Gmail account and connect to this service. Here developers can try deep learning applications using popular machine learning libraries such as Keras, TensorFlow, PyTorch, OpenCV & others.
Sign in to google colab and create a new notebook for our HelloWorld example.
Go to File → New NoteBook(Google sign-in is required) →
Now new notebook is ready we want to use TF2.0.0 for our example so let us first install TensorFlow 2.0.0 is already released as a production version. For installing TensorFlow2.0.0 run the following command.
!pip install tensorflow==2.0.0
After a successful installation, we can verify the installed version.
import tensorflow as tf
Now everything is ready and looking promising. We have installed TensorFlow and verified versions too. Now let us look at helicopter overview and create a hello world example.
To change Runtime: Click on Runtime →Change Runtime Type → one popup will open choose perticular runtime and hardware accelrator such as GPU and TPU.
There are a lot of changes that are there in TF1.0 and TF 2.0.0 TF comes with the ease of development less coding it needs in this version of TF2.0.0. TensorFlow 2.0.0 is developed to remove the issues and complexity of previous versions.
In the TF 2.0 eager execution is enabled by default.
The eager execution mode evaluates the program immediately and without building the graph. The eager execution mode operation returns the concrete value instead of constructing a computational graph and then execute the program.
We will use the same Hello world code from tensorflow 1.x version for this and let us observe the output.
#This code snippet is from tensorflow 1.X version
import tensorflow as tf msg = tf.constant('Hello and welcome to Tensorflow world') #session sess = tf.Session() #print the message print(sess.run(msg))
In this example, we are using Tensorflow 1.X.X version code to print the message, but Session has been removed in TF2.0.0 this will cause the exception i.e
AttributeError: module 'tensorflow' has no attribute 'Session'
We will use the same above code snippet by removing the Session
import tensorflow as tf msg = tf.constant('Hello and welcome to Tensorflow world') #print the message print(msg) #print using tf.print() tf.print(msg)
Here we have two print statement observe output for both print:
- tf.Tensor(b’Hello and welcome to Tensorflow world’, shape=(), dtype=string)
- Hello and welcome to Tensorflow world.
This is it, for now, we will start exploring different API of TF in the next article.
Code is available over github you can directly import that in colab and run it.
More Articles on Tensorflows: