Save and Restore variable in TensoFlow-Python

Saving and restoring of variable is frequently used in Machine learning applications. Once a model is trained, trained variable should be stored for future usage of model without re-training.
I am presenting a simple code of Linear regression using Gradient descent. If you will run this code first time, it will do training and save the variable value. If you are running the code again it will used saved value for testing.

import tensorflow as tf
import numpy as np
import os.path



itr=20#number of iteration for training

x=np.array([1,2,3,4,5,6]).astype(np.float32)
y=np.array([1.8,3,3.98,5,6.1,7.2]).astype(np.float32)
xt=np.array([2.5]).astype(np.float32) #for testing

m=tf.Variable(2.0)
c=tf.Variable(1.0)



y_p=m*x+c
yt=m*xt+c#output for test

loss=tf.reduce_mean(tf.square(y_p-y))

optimizer=tf.train.GradientDescentOptimizer(0.01)
train=optimizer.minimize(loss)

with tf.Session() as sess:
    init_op=tf.global_variables_initializer()
    sess.run(init_op)
    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()
    flag=os.path.isfile("/tmp/reg_model.ckpt.index")#file presence
    


    if flag:                 
        saver.restore(sess, "/tmp/reg_model.ckpt")
        print("Model restored. No need of training")
    else:
            for i in range(itr):
                sess.run(train)
                print("Loss: ",sess.run(loss))
            print("m: ",sess.run(loss)," c: ",sess.run(c))
            save_path = saver.save(sess, "/tmp/reg_model.ckpt")
            print("Model saved in file: %s" % save_path)
       
   

    print("Test input: ",xt)
    print("Output by model: ", sess.run(yt))

   
Output on running the code first time:

Loss:  6.8055177
Loss:  3.1509101
Loss:  1.4594502
Loss:  0.6765916
Loss:  0.31426063
Loss:  0.1465635
Loss:  0.06894786
Loss:  0.03302495
Loss:  0.01639891
Loss:  0.008703876
Loss:  0.005142315
Loss:  0.003493938
Loss:  0.0027310376
Loss:  0.0023779308
Loss:  0.00221451
Loss:  0.0021388675
Loss:  0.00210386
Loss:  0.0020876636
Loss:  0.002080158
Loss:  0.002076687
m:  0.002076687  c:  0.78193426
Model saved in file: /tmp/reg_model.ckpt
Test input:  [2.5]
Output by model:  [3.448408]


Output on running the code next times:

Model restored. No need of training
Test input:  [2.5]
Output by model:  [3.448408]


External Link: https://www.tensorflow.org/programmers_guide/saved_model

Comments

Post a Comment

Popular posts from this blog

Notes on pointer in C

Physical Significance of Convolution

Convolutional Neural Network