```python
# import the library

import tensorflow as tf
```

### Some Basic terms in Tensorflow
* `Variable`: Something that was able to __vary__ data, and that can __updated__ in any point of time

* `constant`: Something that can __not__ be change after initialization

* `placeholder`: A __promise__ to provide value later


```python
# parameters

w = tf.Variable(0.3, tf.float32, name = "weight")
b = tf.Variable(-0.3, tf.float32, name = "bias")

# inputs and outputs

x = tf.placeholder(tf.float32, name="input")
y = tf.placeholder(tf.float32, name="output")

```


```python
# Basic linear Model

linear_model = (w * x) + b
```

### Build the computational Graph and Run it

In tensorflow `Variables`, `constant`, `placeholder` doesn't hold anything except the structure of it, until it `run` in a `Session`. When we initialize a `Variable`, `constant` or a `placeholder` it's just creates a computational graph. Nothing happens until a `Session` being Start. The acutal works done with a Tensorflow `Session`.


```python
# initialize the variable with global_variables_initializer()

init = tf.global_variables_initializer()
```

#### The mathematical formula behind loss function:
\begin{equation}
J(\theta_0, \theta_1) = \frac{1}{2m} \sum(h_\theta x^i - y^i)^2
\end{equation}

here,
* $h_\theta x^i$ is the hypotheis or in other words is the prediction we made
* $ y^i $ is the actual output
* m is the number of trainning examples



```python
# Calculating loss/ loss function

squared_delta = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_delta)
```


```python
# x and y are placeholder and we promise to provide value later, Now this was the time when we provide the values.

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))
```

    23.66


__So here we see that we have a loss of: `23.66`__

### Now we need to reduce the loss using a Optimizer

An `Optimizer` is used for reduce the `loss`. `Optimizier` looks the changes of loss by updating/changing the Variable in any (forward/postive or backward/negative) direction. If it see that changes in postive direction will increase the loss then it will try to update the variable in negative direction. The main goal of a `Optimizer` is to reduce the `loss`.


```python
# Reducing the Loss using Gradient Descent Optimizer with a 0.01 learning rate
# learning rate is nothing but the steps, number of steps the Variable will update by the Optimizer

optimizer = tf.train.GradientDescentOptimizer(0.01) 
train = optimizer.minimize(loss)

```


```python
# After all structure are defined its time to run the computation graph into a Session

with tf.Session() as sess:
    sess.run(init)
    for i in range(1000):
        sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})
        
    print("Updated weight and bias to reduce the loss upto 100%\n",sess.run([w,b]))
```

    Updated weight and bias to reduce the loss upto 100%
     [-0.9999969, 0.9999908]


* updated weight ~= -1  and
* updated bias ~= 1

according to the result, if we update the `weight` from `0.3` to `-1` and `bias` from `-0.3` to `1` the approximate `error/loss` will be `0`

### Lets Validating the optimizer result:

```
inputs  =>  x:[1,2,3,4]
outputs =>  y:[0,-1,-2,-3]
weight  => -1
bias    =>  1
```

for `x = 1`,

\begin{equation}
linearModel = (w * x) +b\\
= (-1 * 1 + 1)\\
= 0
\end{equation}
so, for x = 1 the y = 0 is correct.

for `x = 2`,

\begin{equation}
linearModel = (-1 * 2 + 1)\\
= -1
\end{equation}

for `x = 3`,

\begin{equation}
linearModel = (-1 * 3 + 1)\\
= -2
\end{equation}
for `x = 4`,

\begin{equation}
linearModel = (-1 * 4 + 1)\\
= -3
\end{equation}


### Hence, we can say that the `Optimizer` result is correct.

__For Visualizing a Tensorflow Graph we have tensorboard: Let us see how to use it:__



```python
with tf.Session() as sess:
    sess.run(init)
    for i in range(1000):
        sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})
    
    File_Writer = tf.summary.FileWriter("tensorboard_output", sess.graph)
    print("Updated weight and bias to reduce the loss upto 100%\n",sess.run([w,b]))
```

    Updated weight and bias to reduce the loss upto 100%
     [-0.9999969, 0.9999908]


__In `FileWriter` we need to provide a directory or we can simply put a folder name. The folder will be created by the `FileWriter` to your default working directory__

In my case i created a folder named `tensorboard_output`, and it was created in my working directory.

To visualize the graph in a browser we need to open up the `Command Prompt`,

and need to write `tensorboard --logdir="path of your tensorboard output folder directory"`

and then you need to browse `http:6006`


```python

```

c70cc7274e9b89d54b755a48c7fd8048be639ebf
ipynb
Jupyter Notebook
Introduction to Tensorflow/A very first introduction with Tensorflow.ipynb
78526Nasir/Kaggle-Students-Academic-Performance
6b8510b96d864f009e212a23e3c0762f9f4acf50
MIT
Introduction to Tensorflow/A very first introduction with Tensorflow.ipynb
78526Nasir/Kaggle-Students-Academic-Performance
6b8510b96d864f009e212a23e3c0762f9f4acf50
MIT
Introduction to Tensorflow/A very first introduction with Tensorflow.ipynb
78526Nasir/Kaggle-Students-Academic-Performance
6b8510b96d864f009e212a23e3c0762f9f4acf50
MIT
2019-05-26T14:03:35.000Z
2019-10-31T08:53:53.000Z
Qwen/Qwen-72B
1. YES
2. YES