# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/JyothishArumugam/TensorflowTutorials/blob/main/1_Hello_World_to_Tensorflow.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="KBInnj6mb16z"
# # A Hello World to Tensorflow
# - Tensorflow is an opensource library which is mostly used in the machine learning , itis also a math library which is based on dataflow graphs and differential programming paradigms.
# - According to the  wikipedia definition <br>*Differentiable programming is a programming paradigm in which a numeric computer program can be differentiated throughout via automatic differentiation. This allows for gradient based optimization of parameters in the program, often via gradient descent. Differentiable programming has found use in a wide variety of areas, particularly scientific computing and artificial intelligence.*
#
#
# ---
#
#
# # What is different with Machine learning?
# In a conventional programming setup , a programmer would break any computational problem into simpler sub problems, which will be then coded, using logic flows. 
#
# # # !Todo: the figure for the conventional programming 
#
# But the machine learning way of solving the problem is bit inverted. Here the machine learning system will take mapping of the input to output pairs as input to the system, and will learn the pattern from those mappings and outputs the rules.
#
# - Let us break this down. Let us consider we are having two arrays
#
#
# ```
# x = [0,1,2,3,4,5]
# y = [2,4,6,8,10,12]
# ```
# When taking a closer look at the above equations, we can see the relationship between the y and x as ```y = 2x + 2``` 
# In the convetional setup we will be coding like:
#

# + id="GaTXCrLu_xm3" colab={"base_uri": "https://localhost:8080/"} outputId="d40926c4-6b4d-4c78-c964-7c0a5340a9b4"
x = [0,1,2,3,4,5]
rule ="2*x +2"
y = []
#inorder to figure out the y values ,using this rule
for every_element in x:
  y.append(every_element*2 +2)
print("y value is :",y)


# + [markdown] id="zey2lJmmg0qb"
# Let us make this as a function which can take the input x values and gives out the corresponding y value

# + id="ehakFqt4gtOr" colab={"base_uri": "https://localhost:8080/"} outputId="09ec5d96-7b89-4e5a-fbe5-63ff7f72ab2e"
def calculate_y_value(x):
  return 2*x +2
# Let us calculate the value for x= 10
print("The value of y , when x =10 is :",calculate_y_value(10))


# + [markdown] id="uElPkZRphWtp"
# #### **Let us do the same calculation in tensorflow way**
# The program breaks down as follows:
# - create a machine architecutre, which will take the input/output mappings.
# - Learn the mapping rules
# -  Use the learned rule to predict the value of y for an unseen x value

# + id="UC4Z8QV_hSki" colab={"base_uri": "https://localhost:8080/"} outputId="4b7e0b7b-6f8e-42af-b694-a98056de4d34"
# Step 1: Creating a learning machine architecutre to learn the mappings
import tensorflow as tf 
print("Tensorflow version we are using",tf.__version__)
model = tf.keras.Sequential(tf.keras.layers.Dense(units = 1,input_shape=[1]))
#here units is the value which measures the shape of output value.
#input_shape is the indicator for the input shape

# + [markdown] id="cICjcVpfjO-p"
# - Our machine has to learn a mapping between one x value to one y value.
# - Thus we can see that the  machine has 1 units (output) and input_shape =1 one input x value.

# + id="M6W4gmn6ic-z"
model.compile(optimizer = 'sgd', loss='mean_squared_error')

# + [markdown] id="Ry845ab_kNV9"
# - Imagine the optimizer and the loss function as internal  mechanism the machine has to follow inorder to learn the best mapping between the provided input and output values
# - Let us ask the machine with the above setup to learn the mappings

# + id="IKLJXYNBkNCa"
model.fit(x,y,epochs=500) # The output is cleared to increase the readability of the notebook feel free to run the notebook

# + id="nxXmAXccklkb" colab={"base_uri": "https://localhost:8080/"} outputId="da2a3dd0-9b82-4701-d432-c4dba63d0013"
print("y value when x is 10:",model.predict([10])[0][0])

# + [markdown] id="OQz0RTG6lLcP"
# Let us spend some time in understanding some of the words used above like epoch, predict and fit
#
#  **Epoch**
# - Imagine the machine as  a student who learns a new skill. Epoch is a measure of number of time he repeatedly does the same task of learning adjusting and correcting the mistakes every time. 
# - In a normal case imagine like the more he learns the more he captures the mapping between the input and output. <br>
# {Try running the above cells with less epochs, say 100 and then check the model predict output.}
#
# **Fit**
# - Fit is the equivalent of asking the machine to learn the mapping.
#
# **Predict**
# - Predict is the way of asking the machine (here the machine is named as model) to predict the value of y given the x value based on the learning it has.
#
# **Optimizer and Loss**
#
# - Imagine loss as the numerical measure of the difference between expected ouput and the actual output.
# - In the above model fit output, you can see the loss during the initial epochs are greater and the sucessive epochs result in lower loss.
# - This reduced loss during sucessive epochs are achieved with the help of the Optimizer function. The optimizer function adjusts the machine parameters with the goal of reducing the loss.
#
# **Why is the output value not exactly 22 as like the convetional programming methods?**
# - This is because , the machine always consider the things in the probabilistic way, thus it will give the output which is closer to the expected value. 
# - When the machine has a large number of input output pairs to learn from then it will be giving the exact values
#
# ##### Thus we have finished the Hello World of Tensorflow, let us see how can we use this to solve some intresting problems too

# + id="h1Ym1f9HqC1V"


StarcoderdataJupyterScriptsDedupFiltered