Advertisement
Guest User

Untitled

a guest
May 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon May 13 22:37:02 2019
  4.  
  5. @author: 12718
  6. """
  7.  
  8. import tensorflow as tf
  9. from tensorflow.examples.tutorials.mnist import input_data
  10. import matplotlib.pyplot as plt
  11.  
  12. tf.reset_default_graph()
  13. tf.set_random_seed(1)
  14. mnist = input_data.read_data_sets(
  15. 'C:\\Users\\12718\\Desktop\\python\\machinelearning\\MNIST_data',one_hot = True)
  16. print (mnist.train.num_examples) #(55000,784)
  17. print (mnist.validation.num_examples)
  18. print (mnist.test.num_examples) #(10000,784)
  19. #plt.figure()
  20. #plt.imshow(mnist.train.images[5].reshape(28,28),cmap = 'gray')
  21. #plt.show()
  22. INPUT_SIZE = 784
  23. OUTPUT_SIZE = 10
  24. HIDDEN_UNITS = 500
  25. BATCH_SIZE = 100
  26. LR = 0.8
  27. LEARNING_RATE_DECAY = 0.99
  28. REGULATIZATION_WEIGHT = 0.0001 #正则化
  29. TRAINING_STEPS = 20000
  30. MOVING_AVERAGE_DECAY = 0.99 #滑动平均
  31.  
  32. def inference(x, avg_class, w1, b1,w2,b2): #这里变量名有w1,b1,w2,b2可以通过scope增加可读性
  33. if avg_class is None:
  34. layer1 = tf.nn.relu(tf.matmul(x,w1)+b1)
  35. return tf.matmul(layer1, w2)+b2
  36. else:
  37. #计算滑动平均值
  38. layer1 = tf.nn.relu(tf.matmul(x,avg_class.average(w1))+avg_class.average(b1))
  39. return tf.matmul(layer1, avg_class.average(w2))+avg_class.average(b2)
  40.  
  41. #def inference(x, avg_class, reuse = False):
  42. # with tf.variable_scope('layer1', reuse = reuse):
  43. # w = weights([INPUT_SIZE,HIDDEN_UNITS])
  44. # b = bias([HIDDEN_UNITS])
  45. # if avg_class is None:
  46. # layer1 = tf.nn.relu(tf.matmul(x,w)+b)
  47. # else:
  48. # layer1 = tf.nn.relu(tf.matmul(x,avg_class.average(w))+avg_class.average(b))
  49. # with tf.variable_scope('layer2',reuse = reuse):
  50. # w = weights([HIDDEN_UNITS, OUTPUT_SIZE])
  51. # b = bias([OUTPUT_SIZE])
  52. # if avg_class is None:
  53. # layer2 = tf.matmul(layer1,w)+b
  54. # else:
  55. # layer2 = tf.matmul(layer1, avg_class.average(w))+avg_class.average(b)
  56. # return layer2
  57.  
  58.  
  59. x = tf.placeholder('float', [None, INPUT_SIZE])
  60. y_ = tf.placeholder('float', [None, OUTPUT_SIZE])
  61.  
  62. def weights(shape):
  63. initial = tf.truncated_normal(shape = shape,stddev = 0.1)
  64. return tf.Variable(initial)
  65.  
  66. def bias(shape):
  67. initial = tf.constant(0.1,shape = shape)
  68. return tf.Variable(initial)
  69. #
  70. w1 = weights([INPUT_SIZE,HIDDEN_UNITS])
  71. b1 = bias([HIDDEN_UNITS])
  72. w2 = weights([HIDDEN_UNITS, OUTPUT_SIZE])
  73. b2 = bias([OUTPUT_SIZE])
  74.  
  75. y = inference(x, None,w1,b1,w2,b2)
  76. ### LR exponential decay
  77. global_step = tf.Variable(0,trainable = False)
  78. LR2 = tf.train.exponential_decay(LR,global_step,
  79. mnist.train.num_examples/BATCH_SIZE,LEARNING_RATE_DECAY)
  80. ###
  81.  
  82. ### 滑动平均
  83. variables_avg = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)
  84. variables_avg_ops = variables_avg.apply(tf.trainable_variables())
  85. average_y = inference(x, variables_avg,w1,b1,w2,b2)
  86. ####
  87. #### Regularization
  88. regularizer = tf.contrib.layers.l2_regularizer(REGULATIZATION_WEIGHT)
  89. regularization = regularizer(w1)+regularizer(w2)
  90. cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y,labels = y_))
  91. loss = cross_entropy + regularization
  92. train_ops = tf.train.GradientDescentOptimizer(LR2).minimize(loss,global_step = global_step)
  93. ####
  94.  
  95. ######由于要更新滑动平均值
  96. #train_op = tf.group(train_ops, variables_avg_ops)
  97. # or
  98. with tf.control_dependencies([train_ops, variables_avg_ops]):
  99. train_op = tf.no_op(name = 'train') #tf.no_op 什么操作都不做,仅作为点位符使用控制边界
  100.  
  101. prediction = tf.equal(tf.argmax(y_,1),tf.argmax(average_y,1))
  102. accuracy = tf.reduce_mean(tf.cast(prediction,'float'))
  103. train_losses = []
  104. test_losses = []
  105. steps = []
  106. LRs = []
  107. train_accuracy = []
  108. validate_accuracy = []
  109. with tf.Session() as sess:
  110. sess.run(tf.global_variables_initializer())
  111. validate_feed = {x:mnist.validation.images, y_:mnist.validation.labels}
  112. test_feed = {x:mnist.test.images, y_:mnist.test.labels}
  113. for i in range(TRAINING_STEPS+1):
  114. xs, ys = mnist.train.next_batch(BATCH_SIZE)
  115. train_feed = {x:xs, y_:ys}
  116. sess.run(train_op, feed_dict = train_feed)
  117. if i%1000==0:
  118. train_acc = sess.run(accuracy, feed_dict = {x:xs,y_:ys})
  119. validate_acc = sess.run(accuracy, feed_dict = validate_feed)
  120. train_cross = sess.run(cross_entropy, feed_dict = train_feed)
  121. validate_cross = sess.run(cross_entropy, feed_dict = validate_feed)
  122. train_accuracy.append(train_acc)
  123. validate_accuracy.append(validate_acc)
  124. lr = sess.run(LR2)
  125. train_losses.append(train_cross)
  126. test_losses.append(validate_cross)
  127. steps.append(i)
  128. LRs.append(lr)
  129. print ('after traing %d steps, train: %g, validation accuracy: %g'
  130. %(i,train_acc,validate_acc))
  131. print ('global step: ',sess.run(global_step))
  132. test_acc = sess.run(accuracy, feed_dict = test_feed)
  133. print ('the accuracy: ', test_acc)
  134.  
  135. fig, ax1 = plt.subplots(1)
  136. ax1.plot(steps, train_accuracy, 'o-', color = 'g',label = 'train_acc')
  137. ax1.plot(steps, validate_accuracy, 'o-', color = 'r', label = 'validate_acc')
  138. ax1.set_ylabel('ACC')
  139. plt.legend(loc = 'best')
  140. ax2 = ax1.twinx()
  141. ax2.plot(steps, LRs, 'o-', color = 'blue')
  142. ax2.set_ylabel('LR')
  143. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement