forked from bignerdranch/tensorflow-cocoa-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_softmax.py
82 lines (57 loc) · 2.6 KB
/
mnist_softmax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""A very simple MNIST classifier.
See extensive documentation at
http://tensorflow.org/tutorials/mnist/beginners/index.md
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Import data
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import freeze_graph
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '/tmp/data/', 'Directory for storing data')
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
sess = tf.InteractiveSession()
# Create the model
x = tf.placeholder(tf.float32, [None, 784], name='pix_in')
W = tf.Variable(tf.zeros([784, 10], name='zeroing_weights'), name='weights')
b = tf.Variable(tf.zeros([10], name='zeroing_baises'), name='biases')
y = tf.nn.softmax(tf.add(tf.matmul(x, W, name='convoluting'), b, name='biasing'), name='normalized_guesses')
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10], name='correct_answers')
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]), name='cross_entropy')
print(cross_entropy.get_shape());
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# Create saver to save and restore all the variables
saver = tf.train.Saver([W, b])
# Train
init_op = tf.initialize_all_variables()
sess.run(init_op)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_: batch_ys})
save_path = saver.save(sess, "/tmp/parameters.pb")
print("Parameters in file: %s" % save_path)
graph_def = tf.get_default_graph().as_graph_def()
tf.train.write_graph(graph_def, '/tmp', 'graphdef.pb', False)
print("Model in file: /tmp/graphdef.pb")
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))