TensorFlow笔记-05-反向传播 搭建神经网络的八股

                       

TensorFlow笔记-05-反向传播,搭建神经网络的八股

反向传播

  • 反向传播:
    • 训练模型参数,在所有参数上用梯度下降,使用神经网络模型在训练数据上的损失函数最小
  • 损失函数:(loss)
    • 计算得到的预测值 y 与已知答案 y_ 差距
    • 损失函数的计算有很多方法,均方误差MSE是比较常用的方法之一
    • 关于损失函数,会在下一篇仔细讲
  • 均方误差:
    • 求前向传播计算结果与已知答案之差的平方再求平均
    • 用 Tensorflow 函数表示:
      • loss = tf.reduce_mean(tf.square(y-y_))
  • 反向传播训练方法:
    • 以减小 loss 值为优化目标,有梯度下降,moment 优化器,adm 优化器等优化方法
  • 这三种优化方法分别是:
    • train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
    • train_step = tf.train.GMomentumOptimizer(0.001, 0.9).minimize(loss)
    • train_step = tf.train.AdamOptimizer(0.001).minimize(loss)

搭建神经网络的八股

  • 先回顾神经网络的实现过程,可以总结出:神经网络的搭建分4步:
    • 1.准备工作
    • 2.前向传播
    • 3.反向传播
    • 4.循环迭代

回顾一下,帮助理解

  • 1.准备数据,提取特征,作为输入喂给神经网络
  • 2.搭建神经网络结构,从输入到输出(先搭建计算图,再用会话执行)
    (NN前向传播算法===>计算输出)
  • 3.大量特征数据喂给NN,迭代优化NN参数
    (NN反向传播算法===>优化参数训练模型)
  • 4.使用训练好的模型,预测和分类
  • 通过源代码,进一步理解 神经网络的实现过程

案例及代码

# coding:utf-8# 导入模块,生成随机数据集import tensorflow as tf# 导入numpy模块,numpy是python的科学计算模块import numpy as np# 一次喂入神经网络多少组数据,数值不可以过大BATCH_SIZE = 8seed = 23455# 基于seed产生随机数rng = np.random.RandomState(seed)# 随机数返回32行2列的矩阵 表示32组 体积和重量 作为输入数据集X = rng.rand(32, 2)# 从32行2列的矩阵中 去除一行判断如果和小于1 给Y赋值1 如果和不小于1 给Y赋值0# 作为输入数据集的标签(正确答案)Y = [[int(x0 + x1 <1)] for (x0, x1) in X]print("X:\n", X)print("Y:\n", Y)# 定义神经网络的输入,参数和输出,定义前向传播过程x = tf.placeholder(tf.float32, shape=(None, 2))# y_即为0或1y_ = tf.placeholder(tf.float32, shape=(None, 1))# w1为2行3列w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))# matmul矩阵相乘a = tf.matmul(x, w1)y = tf.matmul(a, w2)# 定义损失函数及反向传播方法loss = tf.reduce_mean(tf.square(y-y_))train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)# 其他优化方法# train_step = tf.train.GMomentumOptimizer(0.001, 0.9).minimize(loss)# train_step = tf.train.AdamOptimizer(0.001).minimize(loss)# 生成会话,训练STEPS轮with tf.Session() as sess:    init_op = tf.global_variables_initializer()    sess.run(init_op)    # 输出目前(未经训练)的参数取值    print("w1:\n", sess.run(w1))    print("w2:\n", sess.run(w2))    print("\n")    # 训练模型3000轮    STEPS = 3000    for i in range(STEPS):        start = (i*BATCH_SIZE) % 32        end = start + BATCH_SIZE        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})        # 没500轮打印一次loss值        if i % 500 == 0:            total_loss = sess.run(loss, feed_dict={x: X, y_: Y})            print("After %d training step(s), loss on all data is %g" %(i, total_loss))    # 输出训练后的参数取值    print("\n")    print("w1:\n", sess.run(w1))    print("w2:\n", sess.run(w2))
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

运行结果

X: [[0.83494319 0.11482951] [0.66899751 0.46594987] [0.60181666 0.58838408] [0.31836656 0.20502072] [0.87043944 0.02679395] [0.41539811 0.43938369] ........ [0.10409134 0.88235166] [0.06727785 0.57784761] [0.38492705 0.48384792] [0.69234428 0.19687348] [0.42783492 0.73416985] [0.09696069 0.04883936]]Y: [[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]]w1: [[-0.8113182   1.4845988   0.06532937] [-2.4427042   0.0992484   0.5912243 ]]w2: [[-0.8113182 ] [ 1.4845988 ] [ 0.06532937]]# 可以看到loss逐渐减小After 0 training step(s), loss on all data is 5.13118After 500 training step(s), loss on all data is 0.429111After 1000 training step(s), loss on all data is 0.409789After 1500 training step(s), loss on all data is 0.399923After 2000 training step(s), loss on all data is 0.394146After 2500 training step(s), loss on all data is 0.390597w1: [[-0.7000663   0.91363174  0.0895357 ] [-2.3402493  -0.14641264  0.58823055]]w2: [[-0.06024268] [ 0.91956186] [-0.06820709]]
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

运行结果分析

由神经网路的实现结果,我们可以看出,总共训练3000轮,每轮从X的数据集合Y的标签中抽取相对应的从start开始到end结束个特征值和标签,喂入神经网络,用sess,run求出loss,没500轮打印一次loss值,经过3000轮后我们打印出最终训练好的参数w1, w2

搭建神经网络的八股

  • 搭建神经网络的八股:准备,前向传播,反向传播,迭代
  • 以上面的代码为例
  • (1)准备
    • 1.import
    • 2.常量定义
    • 3.生成数据集
  • (2)前向传播: 定义输入,参数和输出
    • x =
    • y_ =
    • w1 =
    • w2 =
    • a =
    • y =
  • (3)反向传播:定义损失函数,反向传播的方法
    • loss =
    • train_step =
  • (4)在with中完成迭代,生成会话,训练STEPS轮
with tf.Session() as sess:    init_op = tf.global_variables_initializer()    sess.run(init_op)    # 训练模型3000轮    STEPS = 3000    for i in range(STEPS):        start = (i*BATCH_SIZE) % 32        end = start + BATCH_SIZE        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})        # 没500轮打印一次loss值        if i % 500 == 0:            total_loss = sess.run(loss, feed_dict={x: X, y_: Y})            print("After %d training step(s), loss on all data is %g" %(i, total_loss))
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

这样4步就可以实现神经网络的搭建了

更多文章链接:Tensorflow 笔记


- 本笔记不允许任何个人和组织转载           

猜你喜欢

转载自blog.csdn.net/qq_44906554/article/details/89353079