You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tf_train_new.py 5.9 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # coding: utf-8
  2. import tensorflow as tf
  3. from tensorflow.examples.tutorials.mnist import input_data
  4. import os
  5. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  6. mnist = input_data.read_data_sets('mnist_data', one_hot=True)
  7. #初始化过滤器
  8. def weight_variable(shape):
  9. return tf.Variable(tf.truncated_normal(shape, stddev=0.1))
  10. #初始化偏置,初始化时,所有值是0.1
  11. def bias_variable(shape):
  12. return tf.Variable(tf.constant(0.1, shape=shape))
  13. #卷积运算,strides表示每一维度滑动的步长,一般strides[0]=strides[3]=1
  14. #第四个参数可选"Same"或"VALID",“Same”表示边距使用全0填充
  15. def conv2d(x, W):
  16. return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME")
  17. #池化运算
  18. def max_pool_2x2(x):
  19. return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
  20. #创建x占位符,用于临时存放MNIST图片的数据,
  21. # [None, 784]中的None表示不限长度,而784则是一张图片的大小(28×28=784)
  22. x = tf.placeholder(tf.float32, [None, 784], name='input')
  23. #y_存的是实际图像的标签,即对应于每张输入图片实际的值
  24. y_ = tf.placeholder(tf.float32, [None, 10])
  25. #将图片从784维向量重新还原为28×28的矩阵图片,
  26. # 原因参考卷积神经网络模型图,最后一个参数代表深度,
  27. # 因为MNIST是黑白图片,所以深度为1,
  28. # 第一个参数为-1,表示一维的长度不限定,这样就可以灵活设置每个batch的训练的个数了
  29. x_image = tf.reshape(x, [-1, 28, 28, 1])
  30. #第一层卷积
  31. #将过滤器设置成5×5×1的矩阵,
  32. #其中5×5表示过滤器大小,1表示深度,因为MNIST是黑白图片只有一层。所以深度为1
  33. #32表示我们要创建32个大小5×5×1的过滤器,经过卷积后算出32个特征图(每个过滤器得到一个特征图),即输出深度为64
  34. W_conv1 = weight_variable([5, 5, 1, 32])
  35. #有多少个特征图就有多少个偏置
  36. b_conv1 = bias_variable([32])
  37. #使用conv2d函数进行卷积计算,然后再用ReLU作为激活函数
  38. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
  39. #卷积以后再经过池化操作
  40. h_pool1 = max_pool_2x2(h_conv1)
  41. #第二层卷积
  42. #因为经过第一层卷积运算后,输出的深度为32,所以过滤器深度和下一层输出深度也做出改变
  43. W_conv2 = weight_variable([5, 5, 32, 64])
  44. b_conv2 = bias_variable([64])
  45. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
  46. h_pool2 = max_pool_2x2(h_conv2)
  47. #全连接层
  48. #经过两层卷积后,图片的大小为7×7(第一层池化后输出为(28/2)×(28/2),
  49. #第二层池化后输出为(14/2)×(14/2)),深度为64,
  50. #我们在这里加入一个有1024个神经元的全连接层,所以权重W的尺寸为[7 * 7 * 64, 1024]
  51. W_fc1 = weight_variable([7 * 7 * 64, 1024])
  52. #偏置的个数和权重的个数一致
  53. b_fc1 = bias_variable([1024])
  54. #这里将第二层池化后的张量(长:7 宽:7 深度:64) 变成向量(跟上一节的Softmax模型的输入一样了)
  55. h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
  56. #使用ReLU激活函数
  57. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
  58. #dropout
  59. #为了减少过拟合,我们在输出层之前加入dropout
  60. keep_prob = tf.placeholder(tf.float32, name='keep_prob')
  61. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  62. #输出层
  63. #全连接层输入的大小为1024,而我们要得到的结果的大小是10(0~9),
  64. # 所以这里权重W的尺寸为[1024, 10]
  65. W_fc2 = weight_variable([1024, 10])
  66. b_fc2 = bias_variable([10])
  67. #最后都要经过Softmax函数将输出转化为概率问题
  68. y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2, name='output')
  69. #损失函数和损失优化
  70. cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv)))
  71. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  72. #测试准确率,跟Softmax回归模型的一样
  73. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  74. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  75. # #将训练结果保存,如果不保存我们这次训练结束后的结果也随着程序运行结束而释放了
  76. # savePath = './mnist_conv/'
  77. # saveFile = savePath + 'mnist_conv.ckpt'
  78. # if os.path.exists(savePath) == False:
  79. # os.mkdir(savePath)
  80. # saver = tf.train.Saver()
  81. #开始训练
  82. with tf.Session() as sess:
  83. #初始化所有变量
  84. sess.run(tf.global_variables_initializer())
  85. #训练两万次
  86. for i in range(2000):
  87. #每次获取50张图片数据和对应的标签
  88. batch = mnist.train.next_batch(50)
  89. #每训练100次,我们打印一次训练的准确率
  90. if i % 100 == 0:
  91. train_accuracy =sess.run(accuracy, feed_dict={x:batch[0], y_:batch[1], keep_prob:1.0})
  92. print("step %d, training accuracy %g" % (i, train_accuracy))
  93. #这里是真的训练,将数据传入
  94. sess.run(train_step, feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})
  95. # print ("end train, start testing...")
  96. # mean_value = 0.0
  97. # for i in range(mnist.test.labels.shape[0]):
  98. # batch = mnist.test.next_batch(50)
  99. # train_accuracy = sess.run(accuracy, feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
  100. # mean_value += train_accuracy
  101. # print("test accuracy %g" % (mean_value / mnist.test.labels.shape[0]))
  102. # #训练结束后,我们使用mnist.test在测试最后的准确率
  103. # print("test accuracy %g" % sess.run(accuracy, feed_dict={x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0}))
  104. # 最后,将会话保存下来
  105. # saver.save(sess, saveFile)
  106. # 用SavedModel的方式保存
  107. tf.compat.v1.saved_model.simple_save(sess,
  108. "./saved_model",
  109. inputs={"input": x, 'keep_prob':keep_prob},
  110. outputs={"output": y_conv})

No Description