<jupyter_start><jupyter_text>(Introduction to Tensorflow) * 10^6 In this notebook, we modify the [tensor-fied intro to TensorFlow notebook](https://github.com/the-deep-learners/TensorFlow-LiveLessons/blob/master/notebooks/tensor-fied_intro_to_tensorflow.ipynb) to use placeholder tensors and feed in data from a data set of millions of points. This is a derivation of <NAME>'s [Naked Tensor](https://github.com/jostmey/NakedTensor/) code.<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)
xs = np.linspace(0., 8., 8000000) # eight million points spaced evenly over the interval zero to eight
ys = 0.3*xs-0.8+np.random.normal(scale=0.25, size=len(xs)) # eight million labels given xs, m=0.3, b=-0.8, plus normally-distributed noise
fig, ax = plt.subplots()
data_subset = pd.DataFrame(list(zip(xs, ys)), columns=['x', 'y']).sample(n=1000)
_ = ax.scatter(data_subset.x, data_subset.y)
m = tf.Variable(-0.5)
b = tf.Variable(1.0)
batch_size = 8 # sample mini-batches of size eight for each step of gradient descent<jupyter_output><empty_output><jupyter_text>Define *placeholder tensors* of length *batch_size* whose values will be filled in during graph execution<jupyter_code>xs_placeholder = tf.placeholder(tf.float32, [batch_size])
ys_placeholder = tf.placeholder(tf.float32, [batch_size])<jupyter_output><empty_output><jupyter_text>Define graph that incorporates placeholders<jupyter_code>ys_model = m*xs_placeholder+b
total_error = tf.reduce_sum((ys_placeholder-ys_model)**2)
optimizer_operation = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(total_error) # demo 0.01, 0.0001
initializer_operation = tf.global_variables_initializer()<jupyter_output><empty_output><jupyter_text>Sample from the full data set while running the session<jupyter_code>with tf.Session() as session:
    
    session.run(initializer_operation)
    
    n_batches = 1000 # 10, then 1000
    for iteration in range(n_batches):
        random_indices = np.random.randint(len(xs), size=batch_size) # sample the batch by random selection
        feed = { # feeds are dictionaries
            xs_placeholder: xs[random_indices],
            ys_placeholder: ys[random_indices]
        }
        session.run(optimizer_operation, feed_dict=feed) # minimize cost with the mini-batch
    
    slope, intercept = session.run([m, b])
slope
intercept<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10725806451612903, 'fraction_numerical': 0.025403225806451612, 'mean_word_length': 5.394329896907217, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '206788', 'license': 'MIT', 'n_tokens_mistral': 858, 'n_tokens_neox': 803, 'n_words': 199, 'path': 'notebooks/intro_to_tensorflow_times_a_million.ipynb', 'repo_name': 'GodefroyClair/TensorFlow-LiveLessons', 'size': '2488'}