Getting Started With Tensorflow : Constants, Variables, Placeholders and Sessions

Nishank Sharma
the ML blog
Published in
3 min readMay 19, 2018

Hello guys,

It’s Nishank here, welcome to your first Getting Started With Tensorflow tutorial. Here is a quick intro to tensorflow to get things rolling. Starting here, we will build upon many concepts of how to use Tensorflow for building neural networks. We will start from beginner and will gradually move towards becoming a pro!

Show your support by subscribing to our newsletter!

What is Tensorflow?

I would describe tensorflow as a open source machine learning framework developed by Google which can be used to build neural networks and perform a variety of (almost all) machine learning tasks. Tensorflow works on data flow graphs where nodes are the mathematical operations and the edges are the data in the for tensors, hence the name Tensor-Flow.

You can download tensorflow from here.

Constants, Variables and Placeholders

After downloading tensorflow on your system, you can start using it by a simple import statement in python terminal.

Importing Tensorflow

If everything goes right, you are ready to go!

Tesorflow supports three main type of data types namely Constants, Variables and Placeholders. I think it is fairly evident from the name what they are and what are they used for. We will keep discussing them as we use them later but for now it’s enough to know that they are the three data types used in tensorflow. You can click the link associated with them to know more about them. However let’s discuss constants a bit as we will use them a lot in the coming posts.

Constants

Constant can be created using tf.constant() function.

Tensorflow Constants

A constant has the following arguments which can be tweaked as required to get the desired function.

  • value: A constant value (or list) of output type dtype.
  • dtype: The type of the elements of the resulting tensor.
  • shape: Optional dimensions of resulting tensor.
  • name: Optional name for the tensor.
  • verify_shape: Boolean that enables verification of a shape of values.

But declaring a constant is only half the job done. How will you use those constants to perform basic functions like multiplication and get the desired output?

That’s where Sessions come!

Session

A session is basically the backbone of a tensorflow program. A session fires up the program to get the constants ready and perform the desired operation. It can be done in the following way-

Tensorflow Session

Here, we declared two constants x and y. Then we used tf.multiply() function to multiply them and store the product in result variable. This is not when actual calculation happens, it is just a declaration. The actual operation happens after the session is run.

Next, we defined a session sess by using tf.Session() function. This fires up our program and we are ready to calculate and display the result. We do that by using session.run(result) function inside the print() function.

Lastly, we close the session by using sess.close() function.

This will give an output like this, which is the exact multiplication of simultaneous terms of the matrices-

Output

Alright then. this was a quick tutorial to start things up. We will dive deep into building different models and doing many cool things using tensorflow.

All the code used in this post can be found by clicking the banner below. I will be posting the second post to the series soon. Stay tuned and subscribe to our newsletter for an awesome experience and never missing an update.

--

--

Nishank Sharma
the ML blog

Hello, I’m Nishank. I design beautiful, usable and enjoyable interfaces.