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

# # Introduction to TensorFlow, now leveraging tensors!

# In this notebook, we modify our [intro to TensorFlow notebook](https://github.com/the-deep-learners/TensorFlow-LiveLessons/blob/master/notebooks/point_by_point_intro_to_tensorflow.ipynb) to use tensors in place of our *for* loop. This is a derivation of <NAME>'s [Naked Tensor](https://github.com/jostmey/NakedTensor/) code. 

# #### The initial steps are identical to the earlier notebook

import numpy as np
np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline
import tensorflow as tf
tf.set_random_seed(42)

xs = [0., 1., 2., 3., 4., 5., 6., 7.] 
ys = [-.82, -.94, -.12, .26, .39, .64, 1.02, 1.] 

fig, ax = plt.subplots()
_ = ax.scatter(xs, ys)

m = tf.Variable(-0.5)
b = tf.Variable(1.0)

# #### Define the cost as a tensor -- more elegant than a *for* loop and enables distributed computing in TensorFlow

ys_model = m*xs+b
total_error = tf.reduce_sum((ys-ys_model)**2) # use an op to calculate SSE across all values instead of one by one

# #### The remaining steps are also identical to the earlier notebook!

optimizer_operation = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(total_error)

initializer_operation = tf.global_variables_initializer()

with tf.Session() as session:
    
    session.run(initializer_operation)
    
    n_epochs = 1000 # 10, then 1000
    for iteration in range(n_epochs):
        session.run(optimizer_operation)
        
    slope, intercept = session.run([m, b])

slope

intercept

y_hat = intercept + slope*np.array(xs)

pd.DataFrame(list(zip(ys, y_hat)), columns=['y', 'y_hat'])

# +
fig, ax = plt.subplots()

ax.scatter(xs, ys)
x_min, x_max = ax.get_xlim()
y_min, y_max = intercept, intercept + slope*(x_max-x_min)

ax.plot([x_min, x_max], [y_min, y_max])
_ = ax.set_xlim([x_min, x_max])
# -



StarcoderdataJupyterScriptsDedupFiltered