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)

[((7, 7, 7, 63), 'tensorflow.examples.tutorials.mnist.input_data.read_data_sets', 'input_data.read_data_sets', (), '', False, 'from tensorflow.examples.tutorials.mnist import input_data\n'), ((16, 5, 16, 43), 'tensorflow.placeholder', 'tf.placeholder', ({(16, 20, 16, 30): 'tf.float32', (16, 32, 16, 42): '[None, 10]'}, {}), '(tf.float32, [None, 10])', True, 'import tensorflow as tf\n'), ((22, 8, 22, 33), 'tensorflow.train.Saver', 'tf.train.Saver', ({(22, 23, 22, 32): 'variables'}, {}), '(variables)', True, 'import tensorflow as tf\n'), ((10, 5, 10, 39), 'tensorflow.variable_scope', 'tf.variable_scope', ({(10, 23, 10, 38): '"""convolutional"""'}, {}), "('convolutional')", True, 'import tensorflow as tf\n'), ((11, 8, 11, 47), 'tensorflow.placeholder', 'tf.placeholder', ({(11, 23, 11, 33): 'tf.float32', (11, 35, 11, 46): '[None, 784]'}, {}), '(tf.float32, [None, 784])', True, 'import tensorflow as tf\n'), ((12, 16, 12, 42), 'tensorflow.placeholder', 'tf.placeholder', ({(12, 31, 12, 41): 'tf.float32'}, {}), '(tf.float32)', True, 'import tensorflow as tf\n'), ((13, 19, 13, 52), 'mnist.model.convolutional', 'model.convolutional', ({(13, 39, 13, 40): 'x', (13, 42, 13, 51): 'keep_prob'}, {}), '(x, keep_prob)', False, 'from mnist import model\n'), ((19, 30, 19, 45), 'tensorflow.argmax', 'tf.argmax', ({(19, 40, 19, 41): 'y', (19, 43, 19, 44): '1'}, {}), '(y, 1)', True, 'import tensorflow as tf\n'), ((19, 47, 19, 63), 'tensorflow.argmax', 'tf.argmax', ({(19, 57, 19, 59): 'y_', (19, 61, 19, 62): '1'}, {}), '(y_, 1)', True, 'import tensorflow as tf\n'), ((20, 26, 20, 65), 'tensorflow.cast', 'tf.cast', ({(20, 34, 20, 52): 'correct_prediction', (20, 54, 20, 64): 'tf.float32'}, {}), '(correct_prediction, tf.float32)', True, 'import tensorflow as tf\n'), ((23, 5, 23, 17), 'tensorflow.Session', 'tf.Session', ({}, {}), '()', True, 'import tensorflow as tf\n'), ((18, 13, 18, 41), 'tensorflow.train.AdamOptimizer', 'tf.train.AdamOptimizer', ({(18, 36, 18, 40): '0.0001'}, {}), '(0.0001)', True, 'import tensorflow as tf\n'), ((24, 13, 24, 46), 'tensorflow.global_variables_initializer', 'tf.global_variables_initializer', ({}, {}), '()', True, 'import tensorflow as tf\n'), ((17, 36, 17, 45), 'tensorflow.log', 'tf.log', ({(17, 43, 17, 44): 'y'}, {}), '(y)', True, 'import tensorflow as tf\n'), ((35, 27, 35, 52), 'os.path.dirname', 'os.path.dirname', ({(35, 43, 35, 51): '__file__'}, {}), '(__file__)', False, 'import os\n')]