tensorflow tutorials:用tensorflow实现卷积神经网络(Convolutional Neural Networks)
Posted 风雪夜归子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow tutorials:用tensorflow实现卷积神经网络(Convolutional Neural Networks)相关的知识,希望对你有一定的参考价值。
声明:版权所有,转载请联系作者并注明出处: http://blog.csdn.net/u013719780?viewmode=contents
from __future__ import print_function import tensorflow as tf # Import MNIST data from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) # Parameters learning_rate = 0.001 training_iters = 200000 batch_size = 128 display_step = 10 # Network Parameters n_input = 784 # MNIST data input (img shape: 28*28) n_classes = 10 # MNIST total classes (0-9 digits) dropout = 0.75 # Dropout, probability to keep units # tf Graph input x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_classes]) keep_prob = tf.placeholder(tf.float32) #dropout (keep probability) # Create some wrappers for simplicity def conv2d(x, W, b, strides=1): # Conv2D wrapper, with bias and relu activation x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') x = tf.nn.bias_add(x, b) return tf.nn.relu(x) def maxpool2d(x, k=2): # MaxPool2D wrapper return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME') # Create model def conv_net(x, weights, biases, dropout): # Reshape input picture x = tf.reshape(x, shape=[-1, 28, 28, 1]) # Convolution Layer conv1 = conv2d(x, weights['wc1'], biases['bc1']) # Max Pooling (down-sampling) conv1 = maxpool2d(conv1, k=2) # Convolution Layer conv2 = conv2d(conv1, weights['wc2'], biases['bc2']) # Max Pooling (down-sampling) conv2 = maxpool2d(conv2, k=2) # Fully connected layer # Reshape conv2 output to fit fully connected layer input fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]]) fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) fc1 = tf.nn.relu(fc1) # Apply Dropout fc1 = tf.nn.dropout(fc1, dropout) # Output, class prediction out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) return out # Store layers weight & bias weights = # 5x5 conv, 1 input, 32 outputs 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), # 5x5 conv, 32 inputs, 64 outputs 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), # fully connected, 7*7*64 inputs, 1024 outputs 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])), # 1024 inputs, 10 outputs (class prediction) 'out': tf.Variable(tf.random_normal([1024, n_classes])) biases = 'bc1': tf.Variable(tf.random_normal([32])), 'bc2': tf.Variable(tf.random_normal([64])), 'bd1': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_classes])) # Construct model pred = conv_net(x, weights, biases, keep_prob) # Define loss and optimizer cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Evaluate model correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # Initializing the variables init = tf.initialize_all_variables() # Launch the graph with tf.Session() as sess: sess.run(init) step = 1 # Keep training until reach max iterations while step * batch_size < training_iters: batch_x, batch_y = mnist.train.next_batch(batch_size) # Run optimization op (backprop) sess.run(optimizer, feed_dict=x: batch_x, y: batch_y, keep_prob: dropout) if step % display_step == 0: # Calculate batch loss and accuracy loss, acc = sess.run([cost, accuracy], feed_dict=x: batch_x, y: batch_y, keep_prob: 1.) print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \\ ":.6f".format(loss) + ", Training Accuracy= " + \\ ":.5f".format(acc)) step += 1 print("Optimization Finished!") # Calculate accuracy for 256 mnist test images print("Testing Accuracy:", \\ sess.run(accuracy, feed_dict=x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.))
Extracting /tmp/data/train-images-idx3-ubyte.gz Extracting /tmp/data/train-labels-idx1-ubyte.gz Extracting /tmp/data/t10k-images-idx3-ubyte.gz Extracting /tmp/data/t10k-labels-idx1-ubyte.gz Iter 1280, Minibatch Loss= 33119.816406, Training Accuracy= 0.21875 Iter 2560, Minibatch Loss= 16051.991211, Training Accuracy= 0.36719 Iter 3840, Minibatch Loss= 10761.388672, Training Accuracy= 0.60938 Iter 5120, Minibatch Loss= 4721.864746, Training Accuracy= 0.75781 Iter 6400, Minibatch Loss= 2037.779785, Training Accuracy= 0.85156 Iter 7680, Minibatch Loss= 7413.786133, Training Accuracy= 0.74219 Iter 8960, Minibatch Loss= 3249.669434, Training Accuracy= 0.83594 Iter 10240, Minibatch Loss= 4321.263672, Training Accuracy= 0.81250 Iter 11520, Minibatch Loss= 1479.329956, Training Accuracy= 0.92188 Iter 12800, Minibatch Loss= 2355.179688, Training Accuracy= 0.84375 Iter 14080, Minibatch Loss= 1105.266357, Training Accuracy= 0.92188 Iter 15360, Minibatch Loss= 1615.281494, Training Accuracy= 0.89844 Iter 16640, Minibatch Loss= 2617.854248, Training Accuracy= 0.89062 Iter 17920, Minibatch Loss= 1187.729980, Training Accuracy= 0.92188 Iter 19200, Minibatch Loss= 919.462341, Training Accuracy= 0.89844 Iter 20480, Minibatch Loss= 257.105347, Training Accuracy= 0.96094 Iter 21760, Minibatch Loss= 2607.273438, Training Accuracy= 0.85156 Iter 23040, Minibatch Loss= 793.234375, Training Accuracy= 0.95312 Iter 24320, Minibatch Loss= 1252.133911, Training Accuracy= 0.91406 Iter 25600, Minibatch Loss= 1356.419678, Training Accuracy= 0.91406 Iter 26880, Minibatch Loss= 894.269165, Training Accuracy= 0.95312 Iter 28160, Minibatch Loss= 1081.752197, Training Accuracy= 0.89062 Iter 29440, Minibatch Loss= 1214.221924, Training Accuracy= 0.92969 Iter 30720, Minibatch Loss= 1026.619263, Training Accuracy= 0.90625 Iter 32000, Minibatch Loss= 895.326416, Training Accuracy= 0.93750 Iter 33280, Minibatch Loss= 996.706665, Training Accuracy= 0.93750 Iter 34560, Minibatch Loss= 1258.645020, Training Accuracy= 0.92969 Iter 35840, Minibatch Loss= 757.918152, Training Accuracy= 0.95312 Iter 37120, Minibatch Loss= 1724.612305, Training Accuracy= 0.90625 Iter 38400, Minibatch Loss= 63.499573, Training Accuracy= 0.99219 Iter 39680, Minibatch Loss= 180.159348, Training Accuracy= 0.97656 Iter 40960, Minibatch Loss= 1820.881470, Training Accuracy= 0.90625 Iter 42240, Minibatch Loss= 740.729248, Training Accuracy= 0.96094 Iter 43520, Minibatch Loss= 452.834045, Training Accuracy= 0.95312 Iter 44800, Minibatch Loss= 168.801666, Training Accuracy= 0.97656 Iter 46080, Minibatch Loss= 149.422440, Training Accuracy= 0.98438 Iter 47360, Minibatch Loss= 1192.790039, Training Accuracy= 0.92969 Iter 48640, Minibatch Loss= 1069.188232, Training Accuracy= 0.93750 Iter 49920, Minibatch Loss= 578.359985, Training Accuracy= 0.92188 Iter 51200, Minibatch Loss= 185.997269, Training Accuracy= 0.96875 Iter 52480, Minibatch Loss= 269.750519, Training Accuracy= 0.97656 Iter 53760, Minibatch Loss= 188.991730, Training Accuracy= 0.97656 Iter 55040, Minibatch Loss= 1715.177734, Training Accuracy= 0.95312 Iter 56320, Minibatch Loss= 294.539795, Training Accuracy= 0.94531 Iter 57600, Minibatch Loss= 955.764709, Training Accuracy= 0.92969 Iter 58880, Minibatch Loss= 176.014526, Training Accuracy= 0.97656 Iter 60160, Minibatch Loss= 1202.663574, Training Accuracy= 0.94531 Iter 61440, Minibatch Loss= 1249.316406, Training Accuracy= 0.91406 Iter 62720, Minibatch Loss= 642.846130, Training Accuracy= 0.95312 Iter 64000, Minibatch Loss= 380.056335, Training Accuracy= 0.94531 Iter 65280, Minibatch Loss= 826.421265, Training Accuracy= 0.92969 Iter 66560, Minibatch Loss= 294.462433, Training Accuracy= 0.96875 Iter 67840, Minibatch Loss= 588.712280, Training Accuracy= 0.96875 Iter 69120, Minibatch Loss= 703.491882, Training Accuracy= 0.94531 Iter 70400, Minibatch Loss= 467.652283, Training Accuracy= 0.97656 Iter 71680, Minibatch Loss= 412.892883, Training Accuracy= 0.96875 Iter 72960, Minibatch Loss= 516.359497, Training Accuracy= 0.96875 Iter 74240, Minibatch Loss= 483.566406, Training Accuracy= 0.95312 Iter 75520, Minibatch Loss= 138.074493, Training Accuracy= 0.97656 Iter 76800, Minibatch Loss= 400.362335, Training Accuracy= 0.96875 Iter 78080, Minibatch Loss= 729.972046, Training Accuracy= 0.92188 Iter 79360, Minibatch Loss= 288.753296, Training Accuracy= 0.98438 Iter 80640, Minibatch Loss= 34.889206, Training Accuracy= 0.97656 Iter 81920, Minibatch Loss= 446.821838, Training Accuracy= 0.94531 Iter 83200, Minibatch Loss= 53.508987, Training Accuracy= 0.99219 Iter 84480, Minibatch Loss= 362.887177, Training Accuracy= 0.96094 Iter 85760, Minibatch Loss= 418.652954, Training Accuracy= 0.95312 Iter 87040, Minibatch Loss= 92.926300, Training Accuracy= 0.97656 Iter 88320, Minibatch Loss= 314.797424, Training Accuracy= 0.96875 Iter 89600, Minibatch Loss= 295.765839, Training Accuracy= 0.96875 Iter 90880, Minibatch Loss= 638.518188, Training Accuracy= 0.92969 Iter 92160, Minibatch Loss= 525.749329, Training Accuracy= 0.96094 Iter 93440, Minibatch Loss= 409.661530, Training Accuracy= 0.96094 Iter 94720, Minibatch Loss= 397.676514, Training Accuracy= 0.95312 Iter 96000, Minibatch Loss= 327.677795, Training Accuracy= 0.96094 Iter 97280, Minibatch Loss= 580.521729, Training Accuracy= 0.96875 Iter 98560, Minibatch Loss= 42.764221, Training Accuracy= 0.96875 Iter 99840, Minibatch Loss= 293.447510, Training Accuracy= 0.96094 Iter 101120, Minibatch Loss= 233.889969, Training Accuracy= 0.98438 Iter 102400, Minibatch Loss= 300.799316, Training Accuracy= 0.96094 Iter 103680, Minibatch Loss= 210.885757, Training Accuracy= 0.96094 Iter 104960, Minibatch Loss= 654.990173, Training Accuracy= 0.93750 Iter 106240, Minibatch Loss= 291.870728, Training Accuracy= 0.94531 Iter 107520, Minibatch Loss= 587.544617, Training Accuracy= 0.94531 Iter 108800, Minibatch Loss= 177.339050, Training Accuracy= 0.96094 Iter 110080, Minibatch Loss= 393.805206, Training Accuracy= 0.95312 Iter 111360, Minibatch Loss= 113.489090, Training Accuracy= 0.95312 Iter 112640, Minibatch Loss= 278.391144, Training Accuracy= 0.95312 Iter 113920, Minibatch Loss= 64.654800, Training Accuracy= 0.99219 Iter 115200, Minibatch Loss= 272.650635, Training Accuracy= 0.96875 Iter 116480, Minibatch Loss= 248.082993, Training Accuracy= 0.97656 Iter 117760, Minibatch Loss= 25.483871, Training Accuracy= 0.99219 Iter 119040, Minibatch Loss= 832.794373, Training Accuracy= 0.92188 Iter 120320, Minibatch Loss= 113.149963, Training Accuracy= 0.99219 Iter 121600, Minibatch Loss= 137.738678, Training Accuracy= 0.98438 Iter 122880, Minibatch Loss= 439.732605, Training Accuracy= 0.95312 Iter 124160, Minibatch Loss= 283.381012, Training Accuracy= 0.96875 Iter 125440, Minibatch Loss= 361.409546, Training Accuracy= 0.94531 Iter 126720, Minibatch Loss= 101.087547, Training Accuracy= 0.98438 Iter 128000, Minibatch Loss= 308.690063, Training Accuracy= 0.96094 Iter 129280, Minibatch Loss= 188.306870, Training Accuracy= 0.96094 Iter 130560, Minibatch Loss= 0.000000, Training Accuracy= 1.00000 Iter 131840, Minibatch Loss= 300.978424, Training Accuracy= 0.96875 Iter 133120, Minibatch Loss= 260.767548, Training Accuracy= 0.96094 Iter 134400, Minibatch Loss= 520.364746, Training Accuracy= 0.95312 Iter 135680, Minibatch Loss= 26.482552, Training Accuracy= 0.99219 Iter 136960, Minibatch Loss= 576.294434, Training Accuracy= 0.95312 Iter 138240, Minibatch Loss= 103.803009, Training Accuracy= 0.97656 Iter 139520, Minibatch Loss= 108.436554, Training Accuracy= 0.99219 Iter 140800, Minibatch Loss= 159.441193, Training Accuracy= 0.96875 Iter 142080, Minibatch Loss= 193.125519, Training Accuracy= 0.98438 Iter 143360, Minibatch Loss= 188.117294, Training Accuracy= 0.96875 Iter 144640, Minibatch Loss= 0.000000, Training Accuracy= 1.00000 Iter 145920, Minibatch Loss= 321.186157, Training Accuracy= 0.96094 Iter 147200, Minibatch Loss= 437.349396, Training Accuracy= 0.95312 Iter 148480, Minibatch Loss= 27.928650, Training Accuracy= 0.99219 Iter 149760, Minibatch Loss= 159.316650, Training Accuracy= 0.99219 Iter 151040, Minibatch Loss= 218.392944, Training Accuracy= 0.95312 Iter 152320, Minibatch Loss= 86.327057, Training Accuracy= 0.96094 Iter 153600, Minibatch Loss= 187.457947, Training Accuracy= 0.95312 Iter 154880, Minibatch Loss= 147.812164, Training Accuracy= 0.95312 Iter 156160, Minibatch Loss= 273.637848, Training Accuracy= 0.96094 Iter 157440, Minibatch Loss= 624.448669, Training Accuracy= 0.95312 Iter 158720, Minibatch Loss= 398.916992, Training Accuracy= 0.96094 Iter 160000, Minibatch Loss= 302.056213, Training Accuracy= 0.96875 Iter 161280, Minibatch Loss= 141.484192, Training Accuracy= 0.97656 Iter 162560, Minibatch Loss= 500.844543, Training Accuracy= 0.96875 Iter 163840, Minibatch Loss= 120.265915, Training Accuracy= 0.97656 Iter 165120, Minibatch Loss= 67.206924, Training Accuracy= 0.97656 Iter 166400, Minibatch Loss= 303.770020, Training Accuracy= 0.98438 Iter 167680, Minibatch Loss= 302.175598, Training Accuracy= 0.98438 Iter 168960, Minibatch Loss= 14.445847, Training Accuracy= 0.98438 Iter 170240, Minibatch Loss= 174.764893, Training Accuracy= 0.97656 Iter 171520, Minibatch Loss= 87.963837, Training Accuracy= 0.97656 Iter 172800, Minibatch Loss= 268.049377, Training Accuracy= 0.98438 Iter 174080, Minibatch Loss= 123.035660, Training Accuracy= 0.96875 Iter 175360, Minibatch Loss= 30.370827, Training Accuracy= 0.98438 Iter 176640, Minibatch Loss= 41.883797, Training Accuracy= 0.98438 Iter 177920, Minibatch Loss= 113.069115, Training Accuracy= 0.97656 Iter 179200, Minibatch Loss= 592.399658, Training Accuracy= 0.93750 Iter 180480, Minibatch Loss= 21.242783, Training Accuracy= 0.99219 Iter 181760, Minibatch Loss= 67.023407, Training Accuracy= 0.99219 Iter 183040, Minibatch Loss= 140.905319, Training Accuracy= 0.97656 Iter 184320, Minibatch Loss= 196.006165, Training Accuracy= 0.96875 Iter 185600, Minibatch Loss= 80.115158, Training Accuracy= 0.97656 Iter 186880, Minibatch Loss= 67.482613, Training Accuracy= 0.98438 Iter 188160, Minibatch Loss= 20.215111, Training Accuracy= 0.99219 Iter 189440, Minibatch Loss= 60.191788, Training Accuracy= 0.98438 Iter 190720, Minibatch Loss= 68.743011, Training Accuracy= 0.99219 Iter 192000, Minibatch Loss= 43.774590, Training Accuracy= 0.98438 Iter 193280, Minibatch Loss= 172.976425, Training Accuracy= 0.98438 Iter 194560, Minibatch Loss= 78.267181, Training Accuracy= 0.98438 Iter 195840, Minibatch Loss= 250.249496, Training Accuracy= 0.97656 Iter 197120, Minibatch Loss= 119.354599, Training Accuracy= 0.97656 Iter 198400, Minibatch Loss= 138.678864, Training Accuracy= 0.96875 Iter 199680, Minibatch Loss= 13.272423, Training Accuracy= 0.98438 Optimization Finished! Testing Accuracy: 0.988281
以上是关于tensorflow tutorials:用tensorflow实现卷积神经网络(Convolutional Neural Networks)的主要内容,如果未能解决你的问题,请参考以下文章
tensorflow tutorials:用tensorflow实现卷积神经网络(Convolutional Neural Networks)
tensorflow tutorials:用tensorflow实现降噪自编码器(Denoising Auto-Encoder)
TensorFlow Tutorial: 10 minutes Practical TensorFlow lesson for quick learners(译)