Learning Tensorflow as a new programming language

ProgrammingLanguages

I have been using Tensorflow since my enrollment of Udacity deep learning Nanodegree. I have done a few projects using Tensorflow. Honestly speaking, it has been quite a while. I still have been constantly confused by Tensorflow’s weird syntax and structures. Therefore, I decided to start over again.

Yes, we use Tensorflow on Jupyter Notebook.  Yes, Tensorflow works in Python. But it is not Python.

It seems so confusing if I try to understand and learn Tensorflow with my Python mindset. I thought it might be helpful to learn Tensorflow as a totally new language, instead of considering it as a library in Python. Let’s forget about Python.

In order to understand the basic syntax of Tensorflow, let’s just jump into solving a easy problem. Calculate the result of 3 + 5 in Tensorflow. We know it can be done with the following Python code.

However, it is not going to work in Tensorflow.

In Tensorflow, we define a variable with following syntax:

In Tensorflow, we need to define the operation as well, like ADD arithmetic operation in the above problem.

In order to get the calculation result, we have to use Session as shown in below.

I hope you got my idea of why I am saying that it is totally a new programming language.

Now let’s start learning two important ideas of Tensorflow, which are tf.placeholder and tf.Variable.

tf.placeholder

Placeholder is unique feature of Tensorflow. We can create a placeholder which is empty, and use it in all types of operations. Later on, we can feed actual values to the placeholder with feed_dict. In order to bring computational efficiency, Tensorflow adopted this idea. Let’s see how it works. Let’s apply this to same problem I mentioned in above, 3 + 5.

We use feed_dict={a:3, b:5} to feed values to placeholder a and b.

We can define many operations using placeholders. Tensorflow will get all these operation done whenever we feed the values to the placeholders. See below example. We defined add,divide and multiply operations. When we feed values to placeholders, it gives the final result.

tf.Variable

What if we want to use a variable which constantly update its value in Tensorflow. The tf.Variable is designed to fit this scenario. I have prepared following code in order to demonstrate the constant changing situation. The defined Variable w, its value update itself gradually.

Above learning experience helped me a lot to understand the basic of Tensorflow. I know Tensorflow is very powerful tool. It can solve very complicated problems like language translation, image classification etc. However, it is hard to understand the basic of Tensorflow in the complex code. Trying to solve simple problem in Tensorflow, such as 3 + 5 , actually help me learn better. I wish it can benefit you as well.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s