Guest User

Untitled

a guest
Jan 20th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. 1 0 0 0 0
  2. 0 1 0 0 0
  3. 0 1 0 0 0  →  0 1 0 0 0
  4. 0 0 1 0 0
  5. 0 0 0 0 1
  6.  入力データ      答えデータ
  7.  
  8. 1 0 0 0 0
  9. 0 0 1 0 0
  10. 0 0 1 0 0  →  0 0 0 1 0
  11. 0 0 0 1 0
  12. 0 0 0 0 1
  13.  入力データ      答えデータ
  14.  
  15. 1 0 0 0 0
  16. 0 1 0 0 0
  17. 0 0 1 0 0 
  18. 0 0 0 0 1
  19. 0 0 0 0 1
  20.  
  21. import input_data
  22. mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
  23.  
  24. import tensorflow as tf
  25. sess = tf.InteractiveSession()
  26.  
  27. x = tf.placeholder(tf.float32, shape=[None, 25])
  28. y_ = tf.placeholder(tf.float32, shape=[None, 5])
  29.  
  30. W = tf.Variable(tf.zeros([25,5]))
  31. b = tf.Variable(tf.zeros([5]))
  32.  
  33. sess.run(tf.initialize_all_variables())
  34.  
  35. y = tf.nn.softmax(tf.matmul(x,W) + b)
  36.  
  37. def weight_variable(shape):
  38. initial = tf.truncated_normal(shape, stddev=0.1)
  39. return tf.Variable(initial)
  40.  
  41. def bias_variable(shape):
  42. initial = tf.constant(0.1, shape=shape)
  43. return tf.Variable(initial)
  44.  
  45. def conv2d(x, W):
  46. return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
  47.  
  48. def max_pool_2x2(x):
  49. return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  50. strides=[1, 2, 2, 1], padding='SAME')
  51.  
  52. W_conv1 = weight_variable([5, 5, 1, 32])
  53. b_conv1 = bias_variable([32])
  54.  
  55. x_image = tf.reshape(x, [-1,5,5,1])
  56.  
  57. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
  58. h_pool1 = max_pool_2x2(h_conv1)
  59.  
  60. W_conv2 = weight_variable([5, 5, 32, 64])
  61. b_conv2 = bias_variable([64])
  62.  
  63. h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)
  64. h_pool2 = max_pool_2x2(h_conv2)
  65.  
  66. W_fc1 = weight_variable([5 * 5 * 64, 512])
  67. b_fc1 = bias_variable([512])
  68.  
  69. h_pool2_flat = tf.reshape(h_conv2, [-1, 5*5*64])
  70. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
  71.  
  72. keep_prob = tf.placeholder(tf.float32)
  73. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  74.  
  75. W_fc2 = weight_variable([512, 5])
  76. b_fc2 = bias_variable([5])
  77.  
  78. y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
  79.  
  80. cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
  81. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  82. correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
  83. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  84. sess.run(tf.initialize_all_variables())
  85.  
  86. for i in range(10000):
  87. batch = mnist.train.next_batch(500)
  88. if i%100 == 0:
  89. train_accuracy = accuracy.eval(feed_dict={
  90. x:batch[0], y_: batch[1], keep_prob: 1.0})
  91. print("step %d, training accuracy %g"%(i, train_accuracy))
  92. train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
  93.  
  94. print("test accuracy %g"%accuracy.eval(feed_dict={
  95. x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
Add Comment
Please, Sign In to add comment