<jupyter_start><jupyter_text>Introduction to TensorFlow, fitting point by point In this notebook, we introduce TensorFlow by fitting a line of the form y=m\*x+b point by point. This is a derivation of <NAME>'s [Naked Tensor](https://github.com/jostmey/NakedTensor/) code.   Load dependencies and set seeds for reproducibility<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Create a very small data set<jupyter_code>xs = [0., 1., 2., 3., 4., 5., 6., 7.] # feature (independent variable)
ys = [-.82, -.94, -.12, .26, .39, .64, 1.02, 1.] # labels (dependent variable)
fig, ax = plt.subplots()
_ = ax.scatter(xs, ys)<jupyter_output><empty_output><jupyter_text>Define *variables* -- the model parameters we'll learn -- and initialize them with "random" values<jupyter_code>m = tf.Variable(-0.5)
b = tf.Variable(1.0)<jupyter_output><empty_output><jupyter_text>One single point at a time, define the error between the true label and the model's prediction of the label<jupyter_code>total_error = 0.0
for x,y in zip(xs, ys):
    y_model = m*x + b # the predicted model output; often called y_hat, but we'll use that later in notebook
    total_error += (y-y_model)**2 # sum of squared error between true and predicted y; the "cost" to be minimized<jupyter_output><empty_output><jupyter_text>Define optimizer as SSE-minimizing gradient descent<jupyter_code>optimizer_operation = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(total_error) 
# illustrate learning rate too high: 
# optimizer_operation = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(total_error) 
# optimizer_operation = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(total_error)<jupyter_output><empty_output><jupyter_text>Define an operator that will initialize the graph with all available global variables<jupyter_code>initializer_operation = tf.global_variables_initializer()<jupyter_output><empty_output><jupyter_text>With the computational graph designed, we initialize a session to execute it<jupyter_code>with tf.Session() as session:
    
    session.run(initializer_operation) 
    
    n_epochs = 1000 # run notebook through with 10, then run again with 1000
    for iteration in range(n_epochs):
        session.run(optimizer_operation) # this line executes the graph once, taking a single step toward minimizing cost
    
    slope, intercept = session.run([m, b])  # fetch the m and b operators
slope
intercept<jupyter_output><empty_output><jupyter_text>Calculate the predicted model outputs given the inputs xs<jupyter_code>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])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10782380013149244, 'fraction_numerical': 0.01775147928994083, 'mean_word_length': 5.807606263982103, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '139802', 'license': 'MIT', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1007, 'n_words': 283, 'path': 'notebooks/point_by_point_intro_to_tensorflow.ipynb', 'repo_name': 'GodefroyClair/TensorFlow-LiveLessons', 'size': '3050'}