<jupyter_start><jupyter_text>1.4a Introduction to TensorFlow (single points)  N.B.: Remember to set random seed!  Use TensorFlow to fit a line of the form y=m*x+b  based on https://github.com/jostmey/NakedTensor/<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf<jupyter_output><empty_output><jupyter_text>Create a very small data set<jupyter_code># feature (independent variable):
xs = [0., 1., 2., 3., 4., 5., 6., 7.]
# labels (dependent variable): 
ys = [-.82, -.94, -.12, .26, .39, .64, 1.02, 1.]
fig, ax = plt.subplots()
ax.scatter(xs, ys)<jupyter_output><empty_output><jupyter_text>"Random" initialization of the variables *m* and *b*<jupyter_code>m_initial = -0.5
b_initial = 1.0<jupyter_output><empty_output><jupyter_text>Define *variables* -- the model parameters we will learn<jupyter_code>m = tf.Variable(m_initial)
b = tf.Variable(b_initial)<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 # y_model is the predicted model output (often portrayed with caret as "y hat")
    total_error += (y-y_model)**2 # the squared error between true and predicted y; the "cost" to be minimized<jupyter_output><empty_output><jupyter_text>Use gradient descent to minimize "cost" (this will be detailed in Lesson 2.2)<jupyter_code>optimizer_operation = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(total_error) # makes only one step<jupyter_output><empty_output><jupyter_text>Create the 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>Now that the graph is initalized, all of the calculations can be performed in a session<jupyter_code>with tf.Session() as session:

    session.run(initializer_operation) # call operator to initialize graph
    
    _EPOCHS = 10000 # define the number of times we'll pass the full data set through the graph
    for iteration in range(_EPOCHS):
        session.run(optimizer_operation) # call operator to optimize, i.e., to minimize error
    
    slope, intercept = session.run((m, b)) # fetch "m" and "b", which are operators
    print('Slope:', slope, 'Intercept:', intercept)<jupyter_output>Slope: 0.297022 Intercept: -0.860827<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.11582608695652175, 'fraction_numerical': 0.02017391304347826, 'mean_word_length': 5.703962703962704, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '293128', 'license': 'MIT', 'n_tokens_mistral': 1045, 'n_tokens_neox': 987, 'n_words': 284, 'path': 'notebooks/old/L1-4a__TensorFlow_intro_single.ipynb', 'repo_name': 'thesaikot/TensorFlow-LiveLessons', 'size': '2875'}