import os
from mnist import model
import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

data = input_data.read_data_sets("data/dataset/", one_hot=True)

# model
with tf.variable_scope("convolutional"):
    x = tf.placeholder(tf.float32, [None, 784])
    keep_prob = tf.placeholder(tf.float32)
    y, variables = model.convolutional(x, keep_prob)

# train
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

saver = tf.train.Saver(variables)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(20000):
        batch = data.train.next_batch(50)
        if i % 100 == 0:
            train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
            print("step %d, training accuracy %g" % (i, train_accuracy))
        sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

    print(sess.run(accuracy, feed_dict={x: data.test.images, y_: data.test.labels, keep_prob: 1.0}))

    path = saver.save(
        sess, os.path.join(os.path.dirname(__file__), 'data', 'convolutional.ckpt'),
        write_meta_graph=False, write_state=False)
    print("Saved:", path)

[((126, 182), 'tensorflow.examples.tutorials.mnist.input_data.read_data_sets', 'input_data.read_data_sets', (['"""data/dataset/"""'], {'one_hot': '(True)'}), "('data/dataset/', one_hot=True)\n", (151, 182), False, 'from tensorflow.examples.tutorials.mnist import input_data\n'), ((391, 429), 'tensorflow.placeholder', 'tf.placeholder', (['tf.float32', '[None, 10]'], {}), '(tf.float32, [None, 10])\n', (405, 429), True, 'import tensorflow as tf\n'), ((684, 709), 'tensorflow.train.Saver', 'tf.train.Saver', (['variables'], {}), '(variables)\n', (698, 709), True, 'import tensorflow as tf\n'), ((197, 231), 'tensorflow.variable_scope', 'tf.variable_scope', (['"""convolutional"""'], {}), "('convolutional')\n", (214, 231), True, 'import tensorflow as tf\n'), ((241, 280), 'tensorflow.placeholder', 'tf.placeholder', (['tf.float32', '[None, 784]'], {}), '(tf.float32, [None, 784])\n', (255, 280), True, 'import tensorflow as tf\n'), ((297, 323), 'tensorflow.placeholder', 'tf.placeholder', (['tf.float32'], {}), '(tf.float32)\n', (311, 323), True, 'import tensorflow as tf\n'), ((343, 376), 'mnist.model.convolutional', 'model.convolutional', (['x', 'keep_prob'], {}), '(x, keep_prob)\n', (362, 376), False, 'from mnist import model\n'), ((573, 588), 'tensorflow.argmax', 'tf.argmax', (['y', '(1)'], {}), '(y, 1)\n', (582, 588), True, 'import tensorflow as tf\n'), ((590, 606), 'tensorflow.argmax', 'tf.argmax', (['y_', '(1)'], {}), '(y_, 1)\n', (599, 606), True, 'import tensorflow as tf\n'), ((634, 673), 'tensorflow.cast', 'tf.cast', (['correct_prediction', 'tf.float32'], {}), '(correct_prediction, tf.float32)\n', (641, 673), True, 'import tensorflow as tf\n'), ((715, 727), 'tensorflow.Session', 'tf.Session', ([], {}), '()\n', (725, 727), True, 'import tensorflow as tf\n'), ((490, 520), 'tensorflow.train.AdamOptimizer', 'tf.train.AdamOptimizer', (['(0.0001)'], {}), '(0.0001)\n', (512, 520), True, 'import tensorflow as tf\n'), ((750, 783), 'tensorflow.global_variables_initializer', 'tf.global_variables_initializer', ([], {}), '()\n', (781, 783), True, 'import tensorflow as tf\n'), ((466, 475), 'tensorflow.log', 'tf.log', (['y'], {}), '(y)\n', (472, 475), True, 'import tensorflow as tf\n'), ((1287, 1312), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (1302, 1312), False, 'import os\n')]