convolutional_network.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Import MINST data
  2. import input_data
  3. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
  4. import tensorflow as tf
  5. # Parameters
  6. learning_rate = 0.001
  7. training_iters = 100000
  8. batch_size = 128
  9. display_step = 10
  10. #Network Parameters
  11. n_input = 784 #MNIST data input
  12. n_classes = 10 #MNIST total classes
  13. dropout = 0.75
  14. # Create model
  15. x = tf.placeholder(tf.types.float32, [None, n_input])
  16. y = tf.placeholder(tf.types.float32, [None, n_classes])
  17. keep_prob = tf.placeholder(tf.types.float32) #dropout
  18. def conv2d(img, w, b):
  19. return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(img, w, strides=[1, 1, 1, 1], padding='SAME'),b))
  20. def max_pool(img, k):
  21. return tf.nn.max_pool(img, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')
  22. def conv_net(_X, _weights, _biases, _dropout):
  23. _X = tf.reshape(_X, shape=[-1, 28, 28, 1])
  24. conv1 = conv2d(_X, _weights['wc1'], _biases['bc1'])
  25. conv1 = max_pool(conv1, k=2)
  26. conv1 = tf.nn.dropout(conv1, _dropout)
  27. conv2 = conv2d(conv1, _weights['wc2'], _biases['bc2'])
  28. conv2 = max_pool(conv2, k=2)
  29. conv2 = tf.nn.dropout(conv2, _dropout)
  30. dense1 = tf.reshape(conv2, [-1, _weights['wd1'].get_shape().as_list()[0]])
  31. dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'])
  32. dense1 = tf.nn.dropout(dense1, _dropout)
  33. out = tf.matmul(dense1, _weights['out']) + _biases['out']
  34. return out
  35. weights = {
  36. 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
  37. 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
  38. 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
  39. 'out': tf.Variable(tf.random_normal([1024, 10]))
  40. }
  41. biases = {
  42. 'bc1': tf.Variable(tf.random_normal([32])),
  43. 'bc2': tf.Variable(tf.random_normal([64])),
  44. 'bd1': tf.Variable(tf.random_normal([1024])),
  45. 'out': tf.Variable(tf.random_normal([n_classes]))
  46. }
  47. pred = conv_net(x, weights, biases, keep_prob)
  48. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
  49. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
  50. #Evaluate model
  51. correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
  52. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.types.float32))
  53. # Train
  54. init = tf.initialize_all_variables()
  55. with tf.Session() as sess:
  56. sess.run(init)
  57. step = 1
  58. while step * batch_size < training_iters:
  59. batch_xs, batch_ys = mnist.train.next_batch(batch_size)
  60. sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})
  61. if step % display_step == 0:
  62. acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
  63. loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
  64. print "Iter " + str(step*batch_size) + ", Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)
  65. step += 1
  66. print "Optimization Finished!"
  67. #Accuracy on 256 mnist test images
  68. print "Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.})