4

I am trying to save and restore my model in tensorflow , I tried to search and found many tutorials but None of them are giving clear instructions that while restoring the model should i use same program which was used during training or just restore the model ??

This is simple linear regression model in tensorflow :

import numpy as np    
import tensorflow as tf

tf.set_random_seed(777)

x_data = [[73., 80., 75.],
          [93., 88., 93.],
          [89., 91., 90.],
          [96., 98., 100.],
          [73., 66., 70.]]
y_data = [[152.],
          [185.],
          [180.],
          [196.],
          [142.]]

class regression_model():
    def __init__(self):

        input_x = tf.placeholder(tf.float32,shape=[None,3])    
        output_y=tf.placeholder(tf.float32,shape=[None,1])    
        self.placeholder={'input':input_x,'output':output_y}    

        weights= tf.get_variable('weights',shape=[3,1],dtype=tf.float32,initializer=tf.random_uniform_initializer(-0.01,0.01))    
        bias = tf.get_variable('bias',shape=[1],dtype=tf.float32,initializer=tf.random_uniform_initializer(-0.01,0.01))

        result=tf.matmul(input_x,weights) + bias    
        cost=tf.square(result-output_y)    
        loss=tf.reduce_mean(cost)

        train=tf.train.GradientDescentOptimizer(learning_rate=1e-5).minimize(loss)

        self.out ={'result':result,'loss':loss,'train':train}

def exe_func(model):
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for step in range(2001):
            out=sess.run(model.out,feed_dict={model.placeholder['input']:x_data,model.placeholder['output']:y_data})
            print("loss", out['loss'], "prediction", out['result'])


if __name__=='__main__':    
    model=regression_model()    
    exe_func(model)

when i run i am getting this output:

......

loss 0.73689765 prediction [[152.12286]
 [184.14502]
 [180.76541]
 [196.88777]
 [140.74924]]
loss 0.7366613 prediction [[152.12263]
 [184.1452 ]
 [180.76535]
 [196.88771]
 [140.74948]]

Process finished with exit code 0

Now how i save this model and how to restore in new file? I tried this stackoverflow question and did something like this:

def exe_func(model):
    saver = tf.train.Saver()

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for step in range(2001):
            out=sess.run(model.out,feed_dict={model.placeholder['input']:x_data,model.placeholder['output']:y_data})
            print("loss", out['loss'], "prediction", out['result'])

    saver.save(sess, '/Users/exepaul/Desktop/only_rnn_1/')


if __name__=='__main__':

    model=regression_model()

    exe_func(model)

But i am not getting how to use this saved model and how to give input to model and get a prediction output?

Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88

2 Answers2

3

@MPA answer helped a lot , But i had to do some modifications in @MPA answer to get the result , I would like to mention that for other people :

first thing if you want to save and restore a graph , then give a name parameter value in those operations which you will use later , so i change

this line

input_x = tf.placeholder(tf.float32,shape=[None,3]) 

to this:

input_x = tf.placeholder(tf.float32,shape=[None,3],name='input')

and this line

result=tf.matmul(input_x,weights) + bias    

to this:

result=tf.add(tf.matmul(input_x,weights),bias,name='result')

Now in new file I run this program :

import tensorflow as tf


x_data = [[73., 80., 75.]]



with tf.Session() as sess:
    saver = tf.train.import_meta_graph('/Users/exepaul/Desktop/.meta')
    new=saver.restore(sess, tf.train.latest_checkpoint('/Users/exepaul/Desktop/'))

    graph = tf.get_default_graph()
    input_x = graph.get_tensor_by_name("input:0")
    result = graph.get_tensor_by_name("result:0")

    feed_dict = {input_x: x_data,}

    predictions = result.eval(feed_dict=feed_dict)
    print(predictions)

and i got the output:

[[152.12238]]
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • can I ask something that is for me a source of confusion: what's happening between `new = saver.restore()` and `graph = tf.get_default_graph()` so that graph contains now restored data (graph+weights)? Does the restore command import in default graph? I'm always confused with those *magical* tensorflow commands that you cannot understand by simply reading them... @MPA also – Patrick Oct 04 '19 at 15:05
2

The "restore" code snippet you gave simply restarts the training process. Once you have trained your NN, you don't have to continue training to get a prediction. All of the model parameters should be fixed, and you evaluate the output for a given input only once. See the following example:

with tf.Session() as sess:
    saver = tf.train.import_meta_graph(savefile)
    saver.restore(sess, tf.train.latest_checkpoint(savedir))

    graph = tf.get_default_graph()
    input_x = graph.get_tensor_by_name("input_x:0")
    result = graph.get_tensor_by_name("result:0")

    feed_dict = {input_x: x_data,}

    predictions = result.eval(feed_dict=feed_dict)
MPA
  • 1,878
  • 2
  • 26
  • 51