convolutional_network.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_epochs = 3
  8. batch_size = 64
  9. display_batch = 200 #set to 0 to turn off
  10. display_step = 1
  11. #Network Parameters
  12. n_hidden_1 = 256
  13. n_hidden_2 = 256
  14. n_input = 784 #MNIST data input
  15. n_classes = 10 #MNIST total classes
  16. # Create model
  17. x = tf.placeholder("float", [None, n_input])
  18. y = tf.placeholder("float", [None, n_classes])
  19. def conv2d(img, w, b):
  20. return tf.nn.relu(tf.nn.conv2d(img, w, strides=[1, 1, 1, 1], padding='SAME') + b)
  21. def max_pool(img, k):
  22. return tf.nn.max_pool(img, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')
  23. def conv_net(_X, _weights, _biases):
  24. _X = tf.reshape(_X, shape=[-1, 28, 28, 1])
  25. conv1 = conv2d(_X, _weights['wc1'], _biases['bc1'])
  26. conv1 = max_pool(conv1, k=2)
  27. conv1 = tf.nn.dropout(conv1, 0.75)
  28. conv2 = conv2d(conv1, _weights['wc2'], _biases['bc2'])
  29. conv2 = max_pool(conv2, k=2)
  30. conv2 = tf.nn.dropout(conv2, 0.75)
  31. dense1 = tf.reshape(conv2, [-1, _weights['wd1'].get_shape().as_list()[0]])
  32. dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'])
  33. dense1 = tf.nn.dropout(dense1, 0.75)
  34. out = tf.matmul(dense1, _weights['out']) + _biases['out']
  35. return out
  36. weights = {
  37. 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
  38. 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
  39. 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
  40. 'out': tf.Variable(tf.random_normal([1024, 10]))
  41. }
  42. biases = {
  43. 'bc1': tf.Variable(tf.random_normal([32])),
  44. 'bc2': tf.Variable(tf.random_normal([64])),
  45. 'bd1': tf.Variable(tf.random_normal([1024])),
  46. 'out': tf.Variable(tf.random_normal([n_classes]))
  47. }
  48. pred = conv_net(x, weights, biases)
  49. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
  50. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
  51. # Train
  52. #load mnist data
  53. init = tf.initialize_all_variables()
  54. with tf.Session() as sess:
  55. sess.run(init)
  56. #one epoch can take a long time on CPU
  57. for epoch in range(training_epochs):
  58. avg_cost = 0.
  59. total_batch = int(mnist.train.num_examples/batch_size)
  60. for i in range(total_batch):
  61. batch_xs, batch_ys = mnist.train.next_batch(batch_size)
  62. sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
  63. avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch
  64. if i % display_batch == 0 and display_batch > 0:
  65. print "Epoch:", '%04d' % (epoch+1), "Batch " + str(i) + "/" + str(total_batch), "cost=", \
  66. "{:.9f}".format(sess.run(cost, feed_dict={x: batch_xs, y: batch_ys}))
  67. if epoch % display_step == 0:
  68. print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)
  69. print "Optimization Finished!"
  70. # Test trained model
  71. correct_prediction = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
  72. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  73. print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})