this is an example how to save and restore a trained model. hope this would help for beginners.
generating 1 hidden layer neural network with relu activation function. (heard relu has been proved much better than sigmoid, especially for neural network with large number of hidden layer.)
training data is apparently XOR.
train and save "tf_train_save.py"
import tensorflow as tf
import numpy as np
x = np.matrix([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.matrix([[0], [1], [1], [0]])
n_batch = x.shape[0]
n_input = x.shape[1]
n_hidden = 5
n_classes = y.shape[1]
X = tf.placeholder(tf.float32, [None, n_input], name="X")
Y = tf.placeholder(tf.float32, [None, n_classes], name="Y")
w_h = tf.Variable(tf.random_normal([n_input, n_hidden], stddev=0.01), tf.float32, name="w_h")
w_o = tf.Variable(tf.random_normal([n_hidden, n_classes], stddev=0.01), tf.float32, name="w_o")
l_h = tf.nn.relu(tf.matmul(X, w_h))
hypo = tf.nn.relu(tf.matmul(l_h, w_o), name="output")
cost = tf.reduce_mean(tf.square(Y-hypo))
train = tf.train.GradientDescentOptimizer(0.1).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(1000):
for i in range(4):
sess.run(train, feed_dict = {X:x[i,:], Y:y[i,:]})
result = sess.run([hypo, tf.floor(hypo+0.5)], feed_dict={X:x})
print(*result[0])
print(*result[1])
output_graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ["output"])
tf.train.write_graph(output_graph_def, "./logs/mp_logs", "test.pb", False)
load "tf_load.py"
import tensorflow as tf
from tensorflow.python.platform import gfile
import numpy as np
x = np.matrix([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.matrix([[0], [1], [1], [0]])
with gfile.FastGFile("./logs/mp_logs/test.pb",'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
X = sess.graph.get_tensor_by_name("X:0")
print(X)
output = sess.graph.get_tensor_by_name("output:0")
print(output)
tf.global_variables_initializer().run()
result = sess.run([output, tf.floor(output+0.5)], feed_dict={X:x})
print(*result[0])
print(*result[1])
would there be simpler way?