Skip to content

Commit fbc0e2e

Browse files
committed
add trainer python scripts
1 parent f00ec5b commit fbc0e2e

File tree

3 files changed

+408
-0
lines changed

3 files changed

+408
-0
lines changed

trainer-script/beginner.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import tensorflow as tf
2+
import shutil
3+
import os.path
4+
5+
if os.path.exists("./tmp/beginner-export"):
6+
shutil.rmtree("./tmp/beginner-export")
7+
8+
# Import data
9+
from tensorflow.examples.tutorials.mnist import input_data
10+
11+
mnist = input_data.read_data_sets("./tmp/data/", one_hot=True)
12+
13+
g = tf.Graph()
14+
15+
with g.as_default():
16+
c = tf.constant(5.0)
17+
assert c.graph is g
18+
19+
# Create the model
20+
x = tf.placeholder("float", [None, 784])
21+
W = tf.Variable(tf.zeros([784, 10]), name="vaiable_W")
22+
b = tf.Variable(tf.zeros([10]), name="variable_b")
23+
y = tf.nn.softmax(tf.matmul(x, W) + b)
24+
25+
# Define loss and optimizer
26+
y_ = tf.placeholder("float", [None, 10])
27+
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
28+
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
29+
30+
sess = tf.Session()
31+
32+
# Train
33+
init = tf.initialize_all_variables()
34+
sess.run(init)
35+
36+
for i in range(1000):
37+
batch_xs, batch_ys = mnist.train.next_batch(100)
38+
train_step.run({x: batch_xs, y_: batch_ys}, sess)
39+
40+
# Test trained model
41+
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
42+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
43+
44+
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}, sess))
45+
46+
# Store variable
47+
_W = W.eval(sess)
48+
_b = b.eval(sess)
49+
50+
sess.close()
51+
52+
# Create new graph for exporting
53+
g2 = tf.Graph()
54+
with g2.as_default():
55+
# Reconstruct graph
56+
x2 = tf.placeholder("float", [None, 784], name="input")
57+
W2 = tf.constant(_W, name="constant_W")
58+
b2 = tf.constant(_b, name="constant_b")
59+
y2 = tf.nn.softmax(tf.matmul(x2, W2) + b2, name="output")
60+
61+
sess2 = tf.Session()
62+
63+
init2 = tf.initialize_all_variables();
64+
sess2.run(init2)
65+
66+
67+
graph_def = g2.as_graph_def()
68+
69+
tf.train.write_graph(graph_def, './tmp/beginner-export',
70+
'beginner-graph.pb', as_text=False)
71+
72+
# Test trained model
73+
y_2 = tf.placeholder("float", [None, 10])
74+
correct_prediction2 = tf.equal(tf.argmax(y2, 1), tf.argmax(y_2, 1))
75+
accuracy2 = tf.reduce_mean(tf.cast(correct_prediction2, "float"))
76+
print(accuracy2.eval({x2: mnist.test.images, y_2: mnist.test.labels}, sess2))

trainer-script/expert.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
from __future__ import absolute_import, unicode_literals
2+
import input_data
3+
import tensorflow as tf
4+
import shutil
5+
import os.path
6+
7+
export_dir = './tmp/expert-export'
8+
9+
if os.path.exists(export_dir):
10+
shutil.rmtree(export_dir)
11+
12+
def weight_variable(shape):
13+
initial = tf.truncated_normal(shape, stddev=0.1)
14+
return tf.Variable(initial)
15+
16+
def bias_variable(shape):
17+
initial = tf.constant(0.1, shape=shape)
18+
return tf.Variable(initial)
19+
20+
def conv2d(x, W):
21+
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
22+
23+
def max_pool_2x2(x):
24+
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
25+
strides=[1, 2, 2, 1], padding='SAME')
26+
27+
mnist = input_data.read_data_sets("data/", one_hot=True)
28+
29+
g = tf.Graph()
30+
with g.as_default():
31+
x = tf.placeholder("float", shape=[None, 784])
32+
y_ = tf.placeholder("float", shape=[None, 10])
33+
34+
W_conv1 = weight_variable([5, 5, 1, 32])
35+
b_conv1 = bias_variable([32])
36+
x_image = tf.reshape(x, [-1, 28, 28, 1])
37+
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
38+
h_pool1 = max_pool_2x2(h_conv1)
39+
40+
W_conv2 = weight_variable([5, 5, 32, 64])
41+
b_conv2 = bias_variable([64])
42+
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
43+
h_pool2 = max_pool_2x2(h_conv2)
44+
45+
W_fc1 = weight_variable([7 * 7 * 64, 1024])
46+
b_fc1 = bias_variable([1024])
47+
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
48+
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
49+
50+
keep_prob = tf.placeholder("float")
51+
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
52+
53+
W_fc2 = weight_variable([1024, 10])
54+
b_fc2 = bias_variable([10])
55+
56+
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
57+
58+
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
59+
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
60+
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
61+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
62+
63+
sess = tf.Session()
64+
sess.run(tf.initialize_all_variables())
65+
66+
for i in range(20000):
67+
batch = mnist.train.next_batch(50)
68+
if i % 100 == 0:
69+
train_accuracy = accuracy.eval(
70+
{x: batch[0], y_: batch[1], keep_prob: 1.0}, sess)
71+
print "step %d, training accuracy %g" % (i, train_accuracy)
72+
train_step.run(
73+
{x: batch[0], y_: batch[1], keep_prob: 0.5}, sess)
74+
75+
print "test accuracy %g" % accuracy.eval(
76+
{x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}, sess)
77+
78+
# Store variable
79+
_W_conv1 = W_conv1.eval(sess)
80+
_b_conv1 = b_conv1.eval(sess)
81+
_W_conv2 = W_conv2.eval(sess)
82+
_b_conv2 = b_conv2.eval(sess)
83+
_W_fc1 = W_fc1.eval(sess)
84+
_b_fc1 = b_fc1.eval(sess)
85+
_W_fc2 = W_fc2.eval(sess)
86+
_b_fc2 = b_fc2.eval(sess)
87+
88+
sess.close()
89+
90+
# Create new graph for exporting
91+
g_2 = tf.Graph()
92+
with g_2.as_default():
93+
x_2 = tf.placeholder("float", shape=[None, 784], name="input")
94+
95+
W_conv1_2 = tf.constant(_W_conv1, name="constant_W_conv1")
96+
b_conv1_2 = tf.constant(_b_conv1, name="constant_b_conv1")
97+
x_image_2 = tf.reshape(x_2, [-1, 28, 28, 1])
98+
h_conv1_2 = tf.nn.relu(conv2d(x_image_2, W_conv1_2) + b_conv1_2)
99+
h_pool1_2 = max_pool_2x2(h_conv1_2)
100+
101+
W_conv2_2 = tf.constant(_W_conv2, name="constant_W_conv2")
102+
b_conv2_2 = tf.constant(_b_conv2, name="constant_b_conv2")
103+
h_conv2_2 = tf.nn.relu(conv2d(h_pool1_2, W_conv2_2) + b_conv2_2)
104+
h_pool2_2 = max_pool_2x2(h_conv2_2)
105+
106+
W_fc1_2 = tf.constant(_W_fc1, name="constant_W_fc1")
107+
b_fc1_2 = tf.constant(_b_fc1, name="constant_b_fc1")
108+
h_pool2_flat_2 = tf.reshape(h_pool2_2, [-1, 7 * 7 * 64])
109+
h_fc1_2 = tf.nn.relu(tf.matmul(h_pool2_flat_2, W_fc1_2) + b_fc1_2)
110+
111+
W_fc2_2 = tf.constant(_W_fc2, name="constant_W_fc2")
112+
b_fc2_2 = tf.constant(_b_fc2, name="constant_b_fc2")
113+
114+
# DropOut is skipped for exported graph.
115+
116+
y_conv_2 = tf.nn.softmax(tf.matmul(h_fc1_2, W_fc2_2) + b_fc2_2, name="output")
117+
118+
sess_2 = tf.Session()
119+
init_2 = tf.initialize_all_variables();
120+
sess_2.run(init_2)
121+
122+
graph_def = g_2.as_graph_def()
123+
tf.train.write_graph(graph_def, export_dir, 'expert-graph.pb', as_text=False)
124+
125+
# Test trained model
126+
y__2 = tf.placeholder("float", [None, 10])
127+
correct_prediction_2 = tf.equal(tf.argmax(y_conv_2, 1), tf.argmax(y__2, 1))
128+
accuracy_2 = tf.reduce_mean(tf.cast(correct_prediction_2, "float"))
129+
130+
print "check accuracy %g" % accuracy_2.eval(
131+
{x_2: mnist.test.images, y__2: mnist.test.labels}, sess_2)

0 commit comments

Comments
 (0)