import tensorflow as tf


# y=ax+b linear model
def regression(x):
    a = tf.Variable(tf.zeros([784, 10]), name="a")
    b = tf.Variable(tf.zeros([10]), name="b")
    y = tf.nn.softmax(tf.matmul(x, a) + b)

    return y, [a, b]


# 定义卷积模型
def convolutional(x, keep_prob):
    def conv2d(x, w):
        return tf.nn.conv2d(x, w, [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')

    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)

    x_image = tf.reshape(x, [-1, 28, 28, 1])
    w_conv1 = weight_variable([5, 5, 1, 32])
    b_conv1 = bias_variable([32])
    h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
    h_pool1 = max_pool_2x2(h_conv1)

    w_conv2 = weight_variable([5, 5, 32, 64])
    b_conv2 = bias_variable([64])
    h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
    h_pool2 = max_pool_2x2(h_conv2)

    # 全连接层
    w_fc1 = weight_variable([7 * 7 * 64, 1024])
    b_fc1 = bias_variable([1024])
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
    w_fc2 = weight_variable([1024, 10])
    b_fc2 = bias_variable([10])
    y = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2))

    return y, [w_conv1, b_conv1, w_conv2, b_conv2, w_fc1, w_fc2, b_fc2]

[((30, 14, 30, 44), 'tensorflow.reshape', 'tf.reshape', ({(30, 25, 30, 26): 'x', (30, 28, 30, 43): '[-1, 28, 28, 1]'}, {}), '(x, [-1, 28, 28, 1])', True, 'import tensorflow as tf\n'), ((44, 19, 44, 56), 'tensorflow.reshape', 'tf.reshape', ({(44, 30, 44, 37): 'h_pool2', (44, 39, 44, 55): '[-1, 7 * 7 * 64]'}, {}), '(h_pool2, [-1, 7 * 7 * 64])', True, 'import tensorflow as tf\n'), ((46, 17, 46, 48), 'tensorflow.nn.dropout', 'tf.nn.dropout', ({(46, 31, 46, 36): 'h_fc1', (46, 38, 46, 47): 'keep_prob'}, {}), '(h_fc1, keep_prob)', True, 'import tensorflow as tf\n'), ((6, 20, 6, 39), 'tensorflow.zeros', 'tf.zeros', ({(6, 29, 6, 38): '[784, 10]'}, {}), '([784, 10])', True, 'import tensorflow as tf\n'), ((7, 20, 7, 34), 'tensorflow.zeros', 'tf.zeros', ({(7, 29, 7, 33): '[10]'}, {}), '([10])', True, 'import tensorflow as tf\n'), ((16, 15, 16, 63), 'tensorflow.nn.conv2d', 'tf.nn.conv2d', (), '', True, 'import tensorflow as tf\n'), ((19, 15, 20, 72), 'tensorflow.nn.max_pool', 'tf.nn.max_pool', (), '', True, 'import tensorflow as tf\n'), ((23, 18, 23, 56), 'tensorflow.truncated_normal', 'tf.truncated_normal', (), '', True, 'import tensorflow as tf\n'), ((24, 15, 24, 35), 'tensorflow.Variable', 'tf.Variable', ({(24, 27, 24, 34): 'initial'}, {}), '(initial)', True, 'import tensorflow as tf\n'), ((27, 18, 27, 47), 'tensorflow.constant', 'tf.constant', (), '', True, 'import tensorflow as tf\n'), ((28, 15, 28, 35), 'tensorflow.Variable', 'tf.Variable', ({(28, 27, 28, 34): 'initial'}, {}), '(initial)', True, 'import tensorflow as tf\n'), ((49, 22, 49, 50), 'tensorflow.matmul', 'tf.matmul', ({(49, 32, 49, 42): 'h_fc1_drop', (49, 44, 49, 49): 'w_fc2'}, {}), '(h_fc1_drop, w_fc2)', True, 'import tensorflow as tf\n'), ((8, 22, 8, 37), 'tensorflow.matmul', 'tf.matmul', ({(8, 32, 8, 33): 'x', (8, 35, 8, 36): 'a'}, {}), '(x, a)', True, 'import tensorflow as tf\n'), ((45, 23, 45, 53), 'tensorflow.matmul', 'tf.matmul', ({(45, 33, 45, 45): 'h_pool2_flat', (45, 47, 45, 52): 'w_fc1'}, {}), '(h_pool2_flat, w_fc1)', True, 'import tensorflow as tf\n')]