15-5-DNN-recognise-images-DNN.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from datasets import Datasets
  2. from exts import get_one_hot
  3. import tensorflow.compat.v1 as tf
  4. tf.disable_v2_behavior()
  5. # DNN
  6. def main():
  7. # 加载数据集
  8. train_data, vaild_data, test_data = Datasets.load_mnist()
  9. x_train, y_train = train_data
  10. x_test, y_test = test_data
  11. # one-hot编码又称一位有效编码,其方法是使用N位状态寄存器来对N个状态进行编码,每个状态都有其独立的寄存器位,并且在任意时刻,其中只有一位有效。
  12. y_train = get_one_hot(y_train)
  13. y_test = get_one_hot(y_test)
  14. # 每次训练的数据子集的个数
  15. batch_size = 100
  16. # x 输入28 * 28 向量, y 维度为10的向量
  17. x = tf.placeholder(tf.float32, [None, 784])
  18. y_ = tf.placeholder(tf.float32, [None, 10])
  19. # drop_out的比例
  20. keep_prob = tf.placeholder(tf.float32)
  21. # 定义系统变量,3个隐藏层
  22. in_units = 784
  23. out_units = 10
  24. h1_units = 30
  25. h2_units = 20
  26. h3_units = 10
  27. W1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1))
  28. b1 = tf.Variable(tf.zeros([h1_units]))
  29. W2 = tf.Variable(tf.zeros([h1_units, h2_units]))
  30. b2 = tf.Variable(tf.zeros([h2_units]))
  31. W3 = tf.Variable(tf.zeros([h2_units, h3_units]))
  32. b3 = tf.Variable(tf.zeros([h3_units]))
  33. W4 = tf.Variable(tf.zeros([h3_units, out_units]))
  34. b4 = tf.Variable(tf.zeros([out_units]))
  35. # 操作函数,隐藏层有3层(relu函数)
  36. hidden1 = tf.nn.relu(tf.matmul(x, W1) + b1)
  37. hidden2 = tf.nn.relu(tf.matmul(hidden1, W2) + b2)
  38. hidden3 = tf.nn.relu(tf.matmul(hidden2, W3) + b3)
  39. hidden_drop = tf.nn.dropout(hidden3, keep_prob)
  40. y = tf.nn.softmax(tf.matmul(hidden_drop, W4) + b4)
  41. # 定义衰减函数,这里的衰减函数使用交叉熵来衡量,通过Adagrad自适应调节,学习速率为0.3(训练模型)
  42. cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
  43. train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
  44. # 初始化全部变量并定义会话
  45. init = tf.initialize_all_variables()
  46. sess = tf.Session()
  47. sess.run(init)
  48. # 顺序取出100个数据用于训练
  49. for i in range(int(len(x_train) / batch_size)):
  50. batch_xs = x_train[(i * batch_size):((i + 1) * batch_size)]
  51. batch_ys = y_train[(i * batch_size):((i + 1) * batch_size)]
  52. # 整个训练的次数取决于整个数据集合的长度以及每次训练的数据个数,其中keep_prob比例为75%
  53. sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.75})
  54. # 验证模型
  55. correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  56. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  57. print(sess.run(accuracy, feed_dict={x: x_test, y_: y_test, keep_prob: 1.0})) # 0.1135 数据量太少
  58. if __name__ == "__main__":
  59. main()