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?