神经网络4:卷积神经网络学习 2
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data", one_hot=True) in_x = tf.placeholder(shape=[None, 784], dtype=tf.float32) in_label = tf.placeholder(shape=[None, 10], dtype=tf.float32) keep_prob = tf.placeholder(shape=None, dtype=tf.float32) sess = tf.Session() def createWeightVariable(shape): initial = tf.truncated_normal(shape=shape, mean=0.0, stddev=0.1, dtype=tf.float32) return tf.Variable(initial_value=initial, dtype=tf.float32) def createBiasesVariable(shape): initial = tf.constant(value=1.0, shape=shape, dtype=tf.float32) return tf.Variable(initial_value=initial) def conv2d(inputs, Weight): return tf.nn.conv2d(input=inputs, filter=Weight, strides=[1, 1, 1, 1], padding="SAME") def max_pool_2x2(inputs): return tf.nn.max_pool(value=inputs, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") def add_layer(inputs, in_size, out_size, activation_func=None): Weight = createWeightVariable(shape=[in_size, out_size]) biases = createBiasesVariable(shape=[out_size]) outputs = tf.matmul(inputs, Weight) + biases if activation_func is None: return outputs else: return activation_func(outputs) def compute_accuracy(test_x, test_label): global prediction pre_label = sess.run(prediction, feed_dict={in_x: test_x, keep_prob: 1.0}) ok_label = tf.equal(tf.argmax(pre_label, axis=1), tf.argmax(test_label, axis=1)) accuracy = tf.reduce_mean(tf.cast(ok_label, dtype=tf.float32)) result = sess.run(accuracy, feed_dict={in_x: test_x, keep_prob: 1.0}) return result x_inputs = tf.reshape(tensor=in_x, shape=[-1, 28, 28, 1]) Weights1 = createWeightVariable(shape=[5, 5, 1, 32]) biases1 = createBiasesVariable(shape=[32]) h_conv1 = tf.nn.relu(conv2d(x_inputs, Weights1)) h_pool1 = max_pool_2x2(h_conv1) Weights2 = createWeightVariable(shape=[5, 5, 32, 64]) biases2 = createBiasesVariable(shape=[64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, Weights2)) h_pool2 = max_pool_2x2(h_conv2) h_pool2_flat = tf.reshape(tensor=h_pool2, shape=[-1, 7 * 7 * 64]) f1_outs = add_layer(h_pool2_flat, 7 * 7 * 64, 1024, tf.nn.relu) f1_drop_outs = tf.nn.dropout(f1_outs, keep_prob) prediction = add_layer(f1_drop_outs, 1024, 10, tf.nn.softmax) cross_entropy = tf.reduce_mean(-tf.reduce_sum(in_label * tf.log(prediction), reduction_indices=[1])) train = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) sess.run(tf.global_variables_initializer()) for i in range(1000): batch_x, batch_y = mnist.train.next_batch(100) sess.run(train, feed_dict={in_x: batch_x, in_label: batch_y, keep_prob: 0.5}) if i % 50 == 0: print(compute_accuracy(mnist.test.images, mnist.test.labels))
By Ginfoo