Introduction to Deep Learning  Basics of TensoFlow
Posted on Sat 23 December 2017 in DL
In this short tutorial, we are going to thought the basic commands of the most famous deep learning framework  TensorFlow.
What are we going to learn?

You will learn about tensors;

How to create tensors using TensorFlow;
 And finally, how to make simple arithmetic operation with tensorflow.
Installation
But, first let's install the framework, there is a very detailed instruction on the TensorFlow website here. (I recommend this way)
However, in general, you can quickly install TensorFlow with package manager pip
pip install tensorflow
Finally, we can start coding. Let's import the framework and check the version of it using the version module.
import tensorflow as tf
print(tf.__version__)
>> '1.4.1'
This tutorial based on the TensorFlow 1.4.1 version, but don't worry if you have an older version or new, the basic commands should stay the same. However, another version may have some syntax variations, so please do check the TensorFlow updates.
What is a tensor?
We are going to start with a creation of a tensor. But, what is a tensor?
The word "tensor" is just a fancy name for ndimensional array (matrix).
So, a 1D tensor in tesor is :
one_d_tesor = [1,] # or just = 1
a 2d tensor:
two_d_tensor = [[1, 2],
[3, 4]]
a 3d tensor:
three_d_tensor = [[[ 1, 2],
[ 3, 4]],
#
[[ 5, 6],
[ 7, 8]]]
... and so on I hope you got the idea, if not just ask me.
Also, a tensor can be constructed with stings. Consider the code below:
a = tf.constant("Hey! I'm using ")
b = tf.constant("TensorFlow")
And if we check the type of the variable a
, we are going to see that is a tensor.
print(type(a))
>> tensorflow.python.framework.ops.Tensor
TensorFlow graphs
And here is the most important thing of this short tutorial if you print out variable a
, you won't see the Hey! I'm using
.
print(a)
>> Tensor("Const_2:0", shape=(),dtype=string)
It is because TensorFlow uses a dataflow graph to represent your computation regarding the dependencies between individual operations. This leads to a lowlevel programming model in which you first define the dataflow graph, then create a TensorFlow session to run parts of the graph across a set of local and remote devices.
Shortly, the reason for that you need to all your tensors first and then using the TensorFlow framework to run.
Ok, let's difine (construct) our tensors!
with tf.Session() as sess:
you_can_do_it = sess.run(a+b)
As you can see we are using the standard python with function, because it is super convenient and here is why:
 you don't need to close the session after the code is finished;
 it's just one line, and we're ready to go.
So, if the print `you_can_do_it
now:
print(you_can_do_it)
>>b"Hey! I'm using TensorFlow"
I hope you got my point ;)
TensorFlow operations
Ok, time to increase the complexity of tutorial and move to numbers!
num1 = tf.constant(23)
num2 = tf.constant(32)
type(num1)
>>tensorflow.python.framework.ops.Tensor
in general if you familiar with the pythonNumPy library you will get it quickly:
tensor_with_10 = tf.fill([4,4], value=10)
zeros = tf.zeros([4,4])
ones = tf.ones([4,4])
random = tf.random_normal([4,4], stddev=2)
random_uniform = tf.random_uniform([4,4], minval=23, maxval=32)
Since none of these variables have been executed yet, we can not see them.
print(random_uniform)
>>Tensor("random_uniform:0", shape=(4, 4), dtype=float32)
As a small bonus to this tutorial, we are going to lear one more thing about the sessions in TensorFlow. And this is an interactive session, which may be useful and more convenient for some cases.
So, you can run your function exactly after the creation:
# define the interactive session
sess = tf.InteractiveSession()
print(sess.run(const))
>>[[10 10 10 10]
[10 10 10 10]
[10 10 10 10]
[10 10 10 10]]
:::python
print(sess.run(random))
>>[[3.60729289 1.08835888 1.47455573 1.05923688]
[ 0.69042277 0.28798997 0.89985424 0.40999499]
[2.63693094 1.56255245 5.02441311 2.26954246]
[ 2.18336272 1.77352071 2.07218504 0.89866173]]
Also, you can use eval method to any tensor.
ones.eval()
>>array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]], dtype=float32)
Ok, enough for today, we did a lot. Hope you are going to continue to explore tensor with the tensorflow. So, visit my blog, subscribe to me on Twitter.
For those who want to know even more here are few links, enjoy!