<jupyter_start><jupyter_text>Saving and Loading Models<jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt

from sklearn.preprocessing import MinMaxScaler

%matplotlib inline
print("Numpy: ", np.__version__)
print("Pandas: ", pd.__version__)
print("Tensorflow: ", tf.__version__)

np.random.seed(101)
tf.set_random_seed(101)<jupyter_output><empty_output><jupyter_text>Full Network ExampleLet's work on a regression example, we are trying to solve a very simple equation:y = mx + by will be the y_labels and x is the x_data. We are trying to figure out the slope and the intercept for the line that best fits our data!  Artifical Data (Some Made Up Regression Data)<jupyter_code>x_data = np.linspace(0, 10, 10) + np.random.uniform(-1.5, 1.5, 10)
x_data
y_label = np.linspace(0, 10, 10) + np.random.uniform(-1.5, 1.5, 10)
plt.plot(x_data, y_label, '*')<jupyter_output><empty_output><jupyter_text>** Variables **<jupyter_code>np.random.rand(2)
m = tf.Variable(0.39)
b = tf.Variable(0.2)<jupyter_output><empty_output><jupyter_text>Cost Function<jupyter_code>error = tf.reduce_mean(y_label - (m*x_data+b))<jupyter_output><empty_output><jupyter_text>Optimizer<jupyter_code>optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train = optimizer.minimize(error)<jupyter_output><empty_output><jupyter_text>Initialize Variables<jupyter_code>init = tf.global_variables_initializer()<jupyter_output><empty_output><jupyter_text>_______________________________  Saving The Model<jupyter_code>saver = tf.train.Saver()<jupyter_output><empty_output><jupyter_text>Create Session and Run!<jupyter_code>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])
    
    # ONCE YOU ARE DONE
    # GO AHEAD AND SAVE IT!
    # Make sure to provide a directory for it to make or go to. May get errors otherwise
    #saver.save(sess,'models/my_first_model.ckpt')
    saver.save(sess,'new_models/my_second_model.ckpt')<jupyter_output><empty_output><jupyter_text>Evaluate Results<jupyter_code>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,'*')<jupyter_output><empty_output><jupyter_text>Loading a Model<jupyter_code>with tf.Session() as sess:
    
    # Restore the model
    saver.restore(sess,'new_models/my_second_model.ckpt')
    

    # Fetch Back Results
    restored_slope , restored_intercept = sess.run([m,b])
x_test = np.linspace(-1,11,10)
y_pred_plot = restored_slope*x_test + restored_intercept

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

plt.plot(x_data,y_label,'*')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11691097604576332, 'fraction_numerical': 0.018233821952091528, 'mean_word_length': 5.874692874692875, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '114345', 'license': 'MIT', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1050, 'n_words': 198, 'path': 'deeplearning/tensorflow/intro_tf_1.ipynb', 'repo_name': 'futureseadev/hgwxx7', 'size': '2797'}