1 相关概念

(1)反向传播:训练模型参数,在所有参数上用梯度下降,使NN模型在训练数据上的损失函数最小。
(2)损失函数(loss):预测值(y)与已知答案(y_)的差距
(3)均方误差MSE

loss = tf.reducemean(tf.square(y-y))

(4)反向传播训练方法:以减小loss值为优化目标
(5)学习率:决定参数每次更新的幅度

2 神经网络实现过程

(1)准备数据集,提取特征,作为输入喂给神经网络
(2)搭建NN结构,从输入到输出(先搭建计算图,再用会话执行)
(3)大量特征数据喂给NN,迭代优化NN参数
(4)使用训练好的模型预测和分类

3 代码实现

  1. #coding:utf-8
  2. #0导入模块,生成模拟数据集。
  3. #tensorflow学习笔记(北京大学) tf3_6.py 完全解析神经网络搭建学习
  4. #QQ群:476842922(欢迎加群讨论学习
  5. import tensorflow as tf
  6. import numpy as np
  7. BATCH_SIZE = 8
  8. SEED = 23455
  9. rdm = np.random.RandomState(SEED)
  10. X = rdm.rand(32,2)
  11. Y_ = [[int(x0 + x1 < 1)] for (x0, x1) in X]
  12. print("X:\n",X)
  13. print("Y_:\n",Y_)
  14. x = tf.placeholder(tf.float32, shape=(None, 2))#用placeholder实现输入定义,2表示体积和重量两个特征
  15. y_= tf.placeholder(tf.float32, shape=(None, 1))#用placeholder实现占位。1表示输出的特征只有一个特征,就是标签的可能性,只有合格或不合格
  16. w1= tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))#正态分布随机数。2个输入,对应3个神经元
  17. w2= tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))#正态分布随机数。3个神经元,对应1个输出
  18. a = tf.matmul(x, w1)#点积
  19. y = tf.matmul(a, w2)#点积
  20. #2定义损失函数及反向传播方法。
  21. loss_mse = tf.reduce_mean(tf.square(y-y_))
  22. train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss_mse)#0.001是学习率,可以选择不同优化器
  23. #train_step = tf.train.MomentumOptimizer(0.001,0.9).minimize(loss_mse)不同的优化器
  24. #train_step = tf.train.AdamOptimizer(0.001).minimize(loss_mse)不同的优化器
  25. #3生成会话,训练STEPS轮
  26. with tf.Session() as sess:
  27. init_op = tf.global_variables_initializer()#初始化
  28. sess.run(init_op)
  29. # 输出目前(未经训练)的参数取值。
  30. print("w1:\n", sess.run(w1))
  31. print("w2:\n", sess.run(w2))
  32. print("\n")
  33. # 训练模型。
  34. STEPS = 3000
  35. for i in range(STEPS):#3000
  36. start = (i*BATCH_SIZE) % 32 #i*8%32 计算取数据集的下标
  37. end = start + BATCH_SIZE #i*8%32+8 计算取标签的下标
  38. sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
  39. if i % 500 == 0:
  40. total_loss = sess.run(loss_mse, feed_dict={x: X, y_: Y_})
  41. print("After %d training step(s), loss_mse on all data is %g" % (i, total_loss))
  42. # 输出训练后的参数取值。
  43. print("\n")
  44. print("w1:\n", sess.run(w1))
  45. print("w2:\n", sess.run(w2))
  46. #,只搭建承载计算过程的
  47. #计算图,并没有运算,如果我们想得到运算结果就要用到“会话 Session()”了。
  48. #√会话(Session): 执行计算图中的节点运算
  49. print("w1:\n", w1)
  50. print("w2:\n", w2)
  51. """
  52. X:
  53. [[ 0.83494319 0.11482951]
  54. [ 0.66899751 0.46594987]
  55. [ 0.60181666 0.58838408]
  56. [ 0.31836656 0.20502072]
  57. [ 0.87043944 0.02679395]
  58. [ 0.41539811 0.43938369]
  59. [ 0.68635684 0.24833404]
  60. [ 0.97315228 0.68541849]
  61. [ 0.03081617 0.89479913]
  62. [ 0.24665715 0.28584862]
  63. [ 0.31375667 0.47718349]
  64. [ 0.56689254 0.77079148]
  65. [ 0.7321604 0.35828963]
  66. [ 0.15724842 0.94294584]
  67. [ 0.34933722 0.84634483]
  68. [ 0.50304053 0.81299619]
  69. [ 0.23869886 0.9895604 ]
  70. [ 0.4636501 0.32531094]
  71. [ 0.36510487 0.97365522]
  72. [ 0.73350238 0.83833013]
  73. [ 0.61810158 0.12580353]
  74. [ 0.59274817 0.18779828]
  75. [ 0.87150299 0.34679501]
  76. [ 0.25883219 0.50002932]
  77. [ 0.75690948 0.83429824]
  78. [ 0.29316649 0.05646578]
  79. [ 0.10409134 0.88235166]
  80. [ 0.06727785 0.57784761]
  81. [ 0.38492705 0.48384792]
  82. [ 0.69234428 0.19687348]
  83. [ 0.42783492 0.73416985]
  84. [ 0.09696069 0.04883936]]
  85. Y_:
  86. [[1], [0], [0], [1], [1], [1], [1], [0], [1], [1], [1], [0], [0], [0], [0], [0], [0], [1], [0], [0], [1], [1], [0], [1], [0], [1], [1], [1], [1], [1], [0], [1]]
  87. w1:
  88. [[-0.81131822 1.48459876 0.06532937]
  89. [-2.4427042 0.0992484 0.59122431]]
  90. w2:
  91. [[-0.81131822]
  92. [ 1.48459876]
  93. [ 0.06532937]]
  94. After 0 training step(s), loss_mse on all data is 5.13118
  95. After 500 training step(s), loss_mse on all data is 0.429111
  96. After 1000 training step(s), loss_mse on all data is 0.409789
  97. After 1500 training step(s), loss_mse on all data is 0.399923
  98. After 2000 training step(s), loss_mse on all data is 0.394146
  99. After 2500 training step(s), loss_mse on all data is 0.390597
  100. w1:
  101. [[-0.70006633 0.9136318 0.08953571]
  102. [-2.3402493 -0.14641267 0.58823055]]
  103. w2:
  104. [[-0.06024267]
  105. [ 0.91956186]
  106. [-0.0682071 ]]
  107. """

4 总结

(1)前向传播中:需要定义输入、参数和输出

  1. x =
  2. y_=
  3. w1 =
  4. w2 =
  5. a=
  6. y=

(2)反向传播:定义损失函数、反向传播方法

  1. loss =
  2. train_step =

(3)生成会话,训练Steps论

  1. with tf.Session() as sess:
  2. init_op = tf.global_variables_initializer()#初始化
  3. sess.run(init_op)
  4. # 训练模型。
  5. STEPS = 3000
  6. for i in range(STEPS):#3000
  7. start =
  8. end =
  9. sess.run(train_step, feed_dict:)