# ---
# 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/merazlab/tensorflow/blob/master/mod_01/mod_01_03regreesion.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="imtz2uLFi7Cp" colab_type="code" outputId="b3d1be57-69b5-4238-b42d-2e80cd3c387e" colab={"base_uri": "https://localhost:8080/", "height": 35}
# %tensorflow_version 1.x

# + id="S4PerbUnjCK4" colab_type="code" outputId="c6b57125-6554-40ee-b747-713a06743b78" colab={"base_uri": "https://localhost:8080/", "height": 35}
import tensorflow as tf
print(tf.__version__)

# + [markdown] id="sonBgfgUjNfy" colab_type="text"
# Two types of tensor object in Graph
#
#
#
# 1.   Variabel
# 2.   placeholder

# + [markdown] id="tTsGIYoBjlIx" colab_type="text"
# ###Variabels
#
# * Variabels holds the value through out the session
#
# * Variabe need to be initialized
#
#
#

# + id="QYWD63IsjF3s" colab_type="code" colab={}
sess = tf.InteractiveSession()

# + id="gAZcw6nTk_e2" colab_type="code" outputId="7e8d3e0f-9ea7-4f88-a343-d84d9a47f691" colab={"base_uri": "https://localhost:8080/", "height": 35}
my_var = tf.Variable([2, 4, 6])
print(my_var)

# + id="FOzNUfPulrRy" colab_type="code" outputId="d3b78f82-059a-46f3-e263-6c5724f70f1e" colab={"base_uri": "https://localhost:8080/", "height": 505}
sess.run(my_var) #before run ininitialize

# + id="I9y2aSZnmfF2" colab_type="code" colab={}
init = tf.global_variables_initializer()
sess.run(init)

# + id="RmoDlErsmzRf" colab_type="code" outputId="6c93f193-106e-4b25-f352-13c6ddd83491" colab={"base_uri": "https://localhost:8080/", "height": 35}
sess.run(my_var) 

# + [markdown] id="W7S5N9t8kKlZ" colab_type="text"
# ###Placeholder
#
# * PlaceHolder are initially empty and use for feeding actual examle
# * ned to devtlare expectred data types
#
#

# + id="MTWkmQDknSci" colab_type="code" outputId="77f88cc8-1b8b-4ba9-c824-ee3d601f8b97" colab={"base_uri": "https://localhost:8080/", "height": 35}
my_ph = tf.placeholder(tf.float32, shape=[2, 3])
print(my_ph)

# + id="dMkM6rjKoFf-" colab_type="code" outputId="1b67b0a1-05bf-4578-eb1b-f0e3b12f2d01" colab={"base_uri": "https://localhost:8080/", "height": 54}
a = tf.placeholder(tf.float32, [])
b = tf.constant(1.0)
c = a + b
with tf.Session() as session:
    print(session.run(c, feed_dict={a: 3.0}))
    print(session.run(c, feed_dict={a: 5.0}))

# + [markdown] id="0IN_aQj2qLRI" colab_type="text"
# Example

# + id="_rbxB7QSz21v" colab_type="code" colab={}
import numpy as np
import matplotlib.pyplot as plt

# + id="54cK4Us1oND1" colab_type="code" colab={}
x_data = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)
y_label = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)

# + id="6nEvsFClz0-0" colab_type="code" outputId="9f118857-6f90-4337-c2ee-7fe2be15e9e0" colab={"base_uri": "https://localhost:8080/", "height": 283}
plt.plot(x_data,y_label,'*')

# + id="_e1cB1Taz9-W" colab_type="code" colab={}
m = tf.Variable(0.12)
b = tf.Variable(0.3)

error = 0

for x,y in zip(x_data,y_label):
    
    y_hat = m*x + b  #Our predicted value
    
    error += (y-y_hat)**2 # The cost we want to minimize (we'll need to use an optimization function for the minimization!)


# + id="obutLAsG0EYA" colab_type="code" outputId="d7d56bfd-2298-4404-940b-1318925c513c" colab={"base_uri": "https://localhost:8080/", "height": 92}
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train = optimizer.minimize(error)

# + id="qHN2TaZn0ecr" colab_type="code" colab={}
init = tf.global_variables_initializer()

# + id="kWQ2dJZj0hDn" colab_type="code" colab={}
with tf.Session() as sess:
    
    sess.run(init)
    
    epochs = 100
    
    for i in range(epochs):
        
        sess.run(train)
        

    # Fetch Back Results
    final_slope, final_intercept = sess.run([m,b])

# + id="97oKij6K1cLH" colab_type="code" outputId="1ff3f70d-0589-4e71-916b-b26a1e316b42" colab={"base_uri": "https://localhost:8080/", "height": 283}
x_test = np.linspace(-1,11,10)
y_pred_plot = final_slope*x_test + final_intercept

plt.plot(x_test,y_pred_plot,'r')

plt.plot(x_data,y_label,'*')

# + id="T4GUi2Yv1iJe" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered