# Getting Started with TensorFlow

## What are Tensors?

Tensors are the data units which flow through the computational model of a neural Network. A tensor is just any array of data which can take up any number of dimensions. Therefore, in a tensor, we can store any number of attributes in a very organized manner. The number of dimensions of a tensor is known as it's rank. Various examples of a tensor with their rank are given below:
` 6 # a rank 0 tensor; it has shape [] (a scalar)[4. ,5., 6.] # a rank 1 tensor; It has shape [3] (a vector)[[3., 4., 5.], [10., 20., 30.]] # a rank 2 tensor; it has shape [2, 3] (a matrix)[[[66., 77., 88.]], [[11., 12., 13.]]] # a rank 3 tensor,it has shape [2, 1, 3] `

## Working of TensorFlow

To use all the methods, classes of Tensorflow first we need to import Tensorflow to our python file.
`import tensorflow as tf`
The way the tensorflow works is that first, we need to build a computational graph or a model .The model could be as few a single node or as many as possible. Then after the model is ready then we create a tensorflow session and start the session with the input nodes. When session's run method is called then only all the computation will happen.

## Variables and Constants in TensorFlow

To declare a node with a constant value in tensorflow we use tf.constant(value,datatype) .The first parameter of tf.constant() function is the value you what to put in the node and 2nd parameter is used to define the data type of the constant. TensorFlow can implicitly also identify the type of constant so if we leave out the second parameter then it would also be fine. for example
` firstNode=tf.constant(7.0, dtype=tf.float32)secondNode=tf.constant(14.0)print(firstNode,secondNode) `
Output will be a tensor
` Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32) `
Remember we can get the value 7.0 and 14.0 only when we run the nodes. Also, remember that output of the node will also be a tensor object.
To run them we need to create a tensorflow session as:
`sess=tf.Session()print(sess.run([firstNode , secondNode])) `
This will run those nodes and will give output as
`[7.0, 14.0] `
We can even perform operation on these two nodes as
`thirdNode=tf.add(node1, node2)print("Sum value is : ",sess.run(thirdNode)) `
this will display (7.0+14.0) value as
` 21.0 `
Notice if just print thirdNode then output will be a tensor with no computed answer on it.
We can even store array or list of values as:
` x=tf.constant([3,4,5])y=tf.constant(8)addOp=x+ywith tf.Session() as sess:    print(sess.run(addOp))` Output :`[ 11 12 13]`
Here, we have added 8 to [3,4,5] to all elements of array.
We can also perform array addition of two variables in tensorflow as:
`x=tf.constant([3,4,5])y=tf.constant([4,5,6])addOp=x+ywith tf.Session() as sess:    print(sess.run(addOp))` Output :`[ 7 9 11]`

### Placeholders

Placeholders are like the variable with no value on them but later we can place the value on it. tf.Placeholder is used to feed actual training data.The difference between a tensorflow Variable(tf.Variable) and a placeholder is that while creating Variable , we need to specify the initial value where as in placeholder we don't need to specify the value while defining placeholder. We can create a placeholder as:
`x=tf.placeholder(tf.float32)y=tf.placeholder(tf.float32)z=x+y `
Placeholders allows us to put multiple inputs
` print(sess.run(z,{x: 10.0, y:20.0})) `
the output will be
` 30.0 `
Here we specified 10.0 value to node x and 20.0 value to node y. Then we called the sess.run method on z to perform the addition operation on x and y nodes.

### Variables

tensorflow Variable(tf.Variable) is used to store the state of data. We can use tf.Variable for trainable variables such as weights (W) and biases (B) for our model. With Variable we need to provide the initial value. We can use Variables for parameters to learn and also for values which can be derived from training.Variable initialization must be before all other operations of the model. Since first we build a model so we have to run the initializer in the session first. `tf.global_variables_initializer()` does the job of initializing all the variables. Example as follows:
`W=tf.Variable(3.5, dtype=tf.float32,name="W")b=tf.Variable(4.6, dtype=tf.float32,name="b")addVar=W+b;with tf.Session() as sess:    init=tf.global_variables_initializer()    sess.run(init)    print(sess.run(addVar))` Output :`8.1`