"""
例子为MNIST，对手写图片进行分类。
神经网络hello world。
"""
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# 封装网络用到的API


def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)


def conv2d(x, W):
    return tf.nn.conv2d(x,
                        W,
                        strides= [1, 1, 1, 1],
                        padding= 'SAME')


def max_pool_2x2(x):
    return tf.nn.max_pool(x,
                          ksize= [1, 2, 2, 1],
                          strides= [1, 2, 2, 1],
                          padding='SAME')


"""
MNIST进阶
"""
sess = tf.InteractiveSession()
# [batch_size, 784]
x = tf.placeholder('float', shape=[None, 784])
y_ = tf.placeholder('float', shape=[None, 10])

"""
第一层卷积
"""
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])

# [batch_size, 28, 28, 1]
x_image = tf.reshape(x, [-1, 28, 28, 1])

# [batch_size, 28, 28, 32]
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
# [batch_size, 14, 14, 32]
h_pool1 = max_pool_2x2(h_conv1)

"""
第二层卷积
"""
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

# [batch_size, 14, 14, 64]
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
# [batch_size, 7, 7, 64]
h_pool2 = max_pool_2x2(h_conv2)

"""
全连接层
"""
w_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])

# [batch_size, 7*7*64]
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
# [batch_size, 1024]
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)

"""
dropout
"""
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

"""
输出层
"""
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])

# [batch_size, 10]
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
y_sum = tf.reduce_sum(y_conv[0])

# 计算损失和添加优化器
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

# 评估模型
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

# 初始化
sess.run(tf.initialize_all_variables())

for i in range(1):
    batch = mnist.train.next_batch(50)

    #     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))

    y_conv_re = y_conv.eval(feed_dict={x: batch[0],
                                       y_: batch[1],
                                       keep_prob: 1.0})
    #     print(y_conv_re.shape)
    print(y_conv_re)
    y_sum_re = y_sum.eval(feed_dict={x: batch[0],
                                     y_: batch[1],
                                     keep_prob: 1.0})
    print(y_sum_re)

    train_step.run(feed_dict={x: batch[0],
                              y_: batch[1],
                              keep_prob: 0.5})

print("test accuracy %g" % accuracy.eval(feed_dict={x: mnist.test.images,
                                                    y_: mnist.test.labels,
                                                    keep_prob: 1.0}))





[((8, 8, 8, 61), 'tensorflow.examples.tutorials.mnist.input_data.read_data_sets', 'input_data.read_data_sets', (), '', False, 'from tensorflow.examples.tutorials.mnist import input_data\n'), ((40, 7, 40, 30), 'tensorflow.InteractiveSession', 'tf.InteractiveSession', ({}, {}), '()', True, 'import tensorflow as tf\n'), ((42, 4, 42, 46), 'tensorflow.placeholder', 'tf.placeholder', (), '', True, 'import tensorflow as tf\n'), ((43, 5, 43, 46), 'tensorflow.placeholder', 'tf.placeholder', (), '', True, 'import tensorflow as tf\n'), ((52, 10, 52, 40), 'tensorflow.reshape', 'tf.reshape', ({(52, 21, 52, 22): 'x', (52, 24, 52, 39): '[-1, 28, 28, 1]'}, {}), '(x, [-1, 28, 28, 1])', True, 'import tensorflow as tf\n'), ((77, 15, 77, 52), 'tensorflow.reshape', 'tf.reshape', ({(77, 26, 77, 33): 'h_pool2', (77, 35, 77, 51): '[-1, 7 * 7 * 64]'}, {}), '(h_pool2, [-1, 7 * 7 * 64])', True, 'import tensorflow as tf\n'), ((84, 12, 84, 35), 'tensorflow.placeholder', 'tf.placeholder', ({(84, 27, 84, 34): '"""float"""'}, {}), "('float')", True, 'import tensorflow as tf\n'), ((85, 13, 85, 44), 'tensorflow.nn.dropout', 'tf.nn.dropout', ({(85, 27, 85, 32): 'h_fc1', (85, 34, 85, 43): 'keep_prob'}, {}), '(h_fc1, keep_prob)', True, 'import tensorflow as tf\n'), ((95, 8, 95, 32), 'tensorflow.reduce_sum', 'tf.reduce_sum', ({(95, 22, 95, 31): 'y_conv[0]'}, {}), '(y_conv[0])', True, 'import tensorflow as tf\n'), ((14, 14, 14, 52), 'tensorflow.truncated_normal', 'tf.truncated_normal', (), '', True, 'import tensorflow as tf\n'), ((15, 11, 15, 31), 'tensorflow.Variable', 'tf.Variable', ({(15, 23, 15, 30): 'initial'}, {}), '(initial)', True, 'import tensorflow as tf\n'), ((19, 14, 19, 43), 'tensorflow.constant', 'tf.constant', (), '', True, 'import tensorflow as tf\n'), ((20, 11, 20, 31), 'tensorflow.Variable', 'tf.Variable', ({(20, 23, 20, 30): 'initial'}, {}), '(initial)', True, 'import tensorflow as tf\n'), ((24, 11, 27, 40), 'tensorflow.nn.conv2d', 'tf.nn.conv2d', (), '', True, 'import tensorflow as tf\n'), ((31, 11, 34, 41), 'tensorflow.nn.max_pool', 'tf.nn.max_pool', (), '', True, 'import tensorflow as tf\n'), ((102, 30, 102, 50), 'tensorflow.argmax', 'tf.argmax', ({(102, 40, 102, 46): 'y_conv', (102, 48, 102, 49): '1'}, {}), '(y_conv, 1)', True, 'import tensorflow as tf\n'), ((102, 52, 102, 68), 'tensorflow.argmax', 'tf.argmax', ({(102, 62, 102, 64): 'y_', (102, 66, 102, 67): '1'}, {}), '(y_, 1)', True, 'import tensorflow as tf\n'), ((103, 26, 103, 62), 'tensorflow.cast', 'tf.cast', ({(103, 34, 103, 52): 'correct_prediction', (103, 54, 103, 61): '"""float"""'}, {}), "(correct_prediction, 'float')", True, 'import tensorflow as tf\n'), ((106, 9, 106, 38), 'tensorflow.initialize_all_variables', 'tf.initialize_all_variables', ({}, {}), '()', True, 'import tensorflow as tf\n'), ((79, 19, 79, 49), 'tensorflow.matmul', 'tf.matmul', ({(79, 29, 79, 41): 'h_pool2_flat', (79, 43, 79, 48): 'w_fc1'}, {}), '(h_pool2_flat, w_fc1)', True, 'import tensorflow as tf\n'), ((94, 23, 94, 51), 'tensorflow.matmul', 'tf.matmul', ({(94, 33, 94, 43): 'h_fc1_drop', (94, 45, 94, 50): 'W_fc2'}, {}), '(h_fc1_drop, W_fc2)', True, 'import tensorflow as tf\n'), ((99, 13, 99, 41), 'tensorflow.train.AdamOptimizer', 'tf.train.AdamOptimizer', ({(99, 36, 99, 40): '0.0001'}, {}), '(0.0001)', True, 'import tensorflow as tf\n'), ((98, 36, 98, 50), 'tensorflow.log', 'tf.log', ({(98, 43, 98, 49): 'y_conv'}, {}), '(y_conv)', True, 'import tensorflow as tf\n')]