北京大学曹健——Tensorflow笔记 04 神经网络优化

时间:2024-03-25 16:02:06

北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化 北京大学曹健——Tensorflow笔记 04 神经网络优化

 

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

# 预测多或预测少的影响一样,即预测多与预测少给的权重相同
# 0 导入模块,生成数据集
import tensorflow as tf
import numpy as np

BATCH_SIZE = 8  # 一次喂入神经网络的一小撮特征是8个
SEED = 23455  # 随机种子是23455,保证每次生成的数据集相同

rdm = np.random.RandomState(SEED)
X = rdm.rand(32, 2)  # 生成32行个0~1之间的随机数,包括X1和X2,即32行2列的数据集
Y_ = [[x1 + x2 + (rdm.rand() / 10.0 - 0.05)] for (x1, x2) in X]  # 取出每组的x1与x2求和,
# 再加上随机噪声,构建标准答案Y_,.rand()函数会生成0~1之间的开区间随机数,除以10变成0~0.1
# 之间的随机数,再减去0.05再变成-0.05到+0.05之间的随机数

# 1定义神经网络的输入,参数和输出,定义前向传播过程。
# 给神经网络的输入x,y_用tf.placeholder占位
x = tf.placeholder(tf.float32, shape=(None, 2))
y_ = tf.placeholder(tf.float32, shape=(None, 1))
w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))  # 定义w1参数
y = tf.matmul(x, w1)  # 定义输出y

# 2 定义损失函数及反向传播方法
# 定义损失函数为MSE,反向传播方法为梯度下降,学习率为0.001,让均方误差向减小的方向优化
lose_mse = tf.reduce_mean(tf.square(y_ - y))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(lose_mse)

# 3 生成会话,训练STEPS轮
with tf.Session() as sess:  # with结构中初始化所有变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    STEPS = 20000  # 给出训练轮数
    for i in range(STEPS):  # 用for循环开始训练
        start = (i * BATCH_SIZE) % 32
        end = (i * BATCH_SIZE) % 32 + BATCH_SIZE
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
        # 每轮从X的数据集,以及标准答案Y_抽取相应的从start开始到end结束,Y是特征,喂入神经网络
        # 对train_step进行运算
        if i % 500 == 0:  # 计算第一层神经网络的参数,每500轮打印一次第一层神经网络参数的值w1
            print("After %d raining steps, w1 is:" % (i))
            print(sess.run(w1), "\n")
    print("Final w1 is", "\n", sess.run(w1))


C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/PycharmProjects/untitled4/TS4_1.py
2018-09-28 22:58:36.110264: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
After 0 raining steps, w1 is:
[[-0.80974597]
 [ 1.4852903 ]]

After 500 raining steps, w1 is:
[[-0.46074435]
 [ 1.641878  ]]

After 1000 raining steps, w1 is:
[[-0.21939856]
 [ 1.6984766 ]]

After 1500 raining steps, w1 is:
[[-0.04415595]
 [ 1.7003176 ]]

After 2000 raining steps, w1 is:
[[0.08942621]
 [1.673328  ]]

After 2500 raining steps, w1 is:
[[0.19583555]
 [1.6322677 ]]

After 3000 raining steps, w1 is:
[[0.28375748]
 [1.5854434 ]]

After 3500 raining steps, w1 is:
[[0.35848638]
 [1.5374472 ]]

After 4000 raining steps, w1 is:
[[0.42332518]
 [1.4907393 ]]

After 4500 raining steps, w1 is:
[[0.48040026]
 [1.4465574 ]]

After 5000 raining steps, w1 is:
[[0.53113604]
 [1.4054536 ]]

After 5500 raining steps, w1 is:
[[0.5765325]
 [1.3675941]]

After 6000 raining steps, w1 is:
[[0.61732584]
 [1.3329403 ]]

After 6500 raining steps, w1 is:
[[0.6540846]
 [1.3013426]]

After 7000 raining steps, w1 is:
[[0.6872685]
 [1.272602 ]]

After 7500 raining steps, w1 is:
[[0.71725976]
 [1.2465005 ]]

After 8000 raining steps, w1 is:
[[0.7443861]
 [1.2228197]]

After 8500 raining steps, w1 is:
[[0.7689324]
 [1.2013483]]

After 9000 raining steps, w1 is:
[[0.79115134]
 [1.1818889 ]]

After 9500 raining steps, w1 is:
[[0.811267 ]
 [1.1642567]]

After 10000 raining steps, w1 is:
[[0.8294814]
 [1.1482829]]

After 10500 raining steps, w1 is:
[[0.84597576]
 [1.1338125 ]]

After 11000 raining steps, w1 is:
[[0.8609128]
 [1.1207061]]

After 11500 raining steps, w1 is:
[[0.87444043]
 [1.1088346 ]]

After 12000 raining steps, w1 is:
[[0.88669145]
 [1.0980824 ]]

After 12500 raining steps, w1 is:
[[0.8977863]
 [1.0883439]]

After 13000 raining steps, w1 is:
[[0.9078348]
 [1.0795243]]

After 13500 raining steps, w1 is:
[[0.91693527]
 [1.0715363 ]]

After 14000 raining steps, w1 is:
[[0.92517716]
 [1.0643018 ]]

After 14500 raining steps, w1 is:
[[0.93264157]
 [1.0577497 ]]

After 15000 raining steps, w1 is:
[[0.9394023]
 [1.0518153]]

After 15500 raining steps, w1 is:
[[0.9455251]
 [1.0464406]]

After 16000 raining steps, w1 is:
[[0.95107025]
 [1.0415728 ]]

After 16500 raining steps, w1 is:
[[0.9560928]
 [1.037164 ]]

After 17000 raining steps, w1 is:
[[0.96064115]
 [1.0331714 ]]

After 17500 raining steps, w1 is:
[[0.96476096]
 [1.0295546 ]]

After 18000 raining steps, w1 is:
[[0.9684917]
 [1.0262802]]

After 18500 raining steps, w1 is:
[[0.9718707]
 [1.0233142]]

After 19000 raining steps, w1 is:
[[0.974931 ]
 [1.0206276]]

After 19500 raining steps, w1 is:
[[0.9777026]
 [1.0181949]]

Final w1 is
 [[0.98019385]
 [1.0159807 ]]

Process finished with exit code 0

北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化

# -*- coding: utf-8 -*-
#  酸奶成本1元,酸奶利润9元,预测少了损失大,故不要预测少,故生成的模型会多预测一些
# 0 导入模块,生成数据集
import tensorflow as tf
import numpy as np
BATCH_SIZE = 8  # 一次喂入神经网络的一小撮特征是8个
SEED = 23455  # 随机种子是23455,保证每次生成的数据集相同
COST = 1
PROFIT= 9

rdm = np.random.RandomState(SEED)
X = rdm.rand(32, 2)  # 生成32行个0~1之间的随机数,包括X1和X2,即32行2列的数据集
Y_ = [[x1 + x2 + (rdm.rand() / 10.0 - 0.05)] for (x1, x2) in X]  # 取出每组的x1与x2求和,
# 再加上随机噪声,构建标准答案Y_,.rand()函数会生成0~1之间的开区间随机数,除以10变成0~0.1
# 之间的随机数,再减去0.05再变成-0.05到+0.05之间的随机数

# 1定义神经网络的输入,参数和输出,定义前向传播过程。
# 给神经网络的输入x,y_用tf.placeholder占位
x = tf.placeholder(tf.float32, shape=(None, 2))
y_ = tf.placeholder(tf.float32, shape=(None, 1))
w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))  # 定义w1参数
y = tf.matmul(x, w1)  # 定义输出y

# 2 定义损失函数及反向传播方法
# 定义损失函数为MSE,反向传播方法为梯度下降,学习率为0.001,让均方误差向减小的方向优化
loss = tf.reduce_sum(tf.where(tf.greater(y,y_),(y - y_)*COST,(y_  -y)*PROFIT))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

# 3 生成会话,训练STEPS轮
with tf.Session() as sess:  # with结构中初始化所有变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    STEPS = 20000  # 给出训练轮数
    for i in range(STEPS):  # 用for循环开始训练
        start = (i * BATCH_SIZE) % 32
        end = (i * BATCH_SIZE) % 32 + BATCH_SIZE
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
        # 每轮从X的数据集,以及标准答案Y_抽取相应的从start开始到end结束,Y是特征,喂入神经网络
        # 对train_step进行运算
        if i % 500 == 0:  # 计算第一层神经网络的参数,每500轮打印一次第一层神经网络参数的值w1
            print("After %d raining steps, w1 is:" % (i))
            print(sess.run(w1), "\n")
    print("Final w1 is", "\n", sess.run(w1))



C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/PycharmProjects/untitled4/TS4_2.py
After 0 raining steps, w1 is:
[[-0.762993 ]
 [ 1.5095658]]

After 500 raining steps, w1 is:
[[1.0235443]
 [1.0463386]]

After 1000 raining steps, w1 is:
[[1.0174844]
 [1.0406483]]

After 1500 raining steps, w1 is:
[[1.0211805]
 [1.0472497]]

After 2000 raining steps, w1 is:
[[1.0179386]
 [1.0412899]]

After 2500 raining steps, w1 is:
[[1.0205938]
 [1.0390677]]

After 3000 raining steps, w1 is:
[[1.0242898]
 [1.0456691]]

After 3500 raining steps, w1 is:
[[1.01823  ]
 [1.0399789]]

After 4000 raining steps, w1 is:
[[1.021926 ]
 [1.0465802]]

After 4500 raining steps, w1 is:
[[1.0245812]
 [1.044358 ]]

After 5000 raining steps, w1 is:
[[1.0185213]
 [1.0386678]]

After 5500 raining steps, w1 is:
[[1.0245652]
 [1.0446368]]

After 6000 raining steps, w1 is:
[[1.0185053]
 [1.0389466]]

After 6500 raining steps, w1 is:
[[1.0222014]
 [1.045548 ]]

2018-09-28 23:22:54.803767: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
After 7000 raining steps, w1 is:
[[1.0161415]
 [1.0398577]]

After 7500 raining steps, w1 is:
[[1.0198376]
 [1.0464591]]

After 8000 raining steps, w1 is:
[[1.0224928]
 [1.0442369]]

After 8500 raining steps, w1 is:
[[1.0174738]
 [1.0473702]]

After 9000 raining steps, w1 is:
[[1.0222716]
 [1.0383747]]

After 9500 raining steps, w1 is:
[[1.0172527]
 [1.041508 ]]

After 10000 raining steps, w1 is:
[[1.0199078]
 [1.0392858]]

After 10500 raining steps, w1 is:
[[1.0236039]
 [1.0458871]]

After 11000 raining steps, w1 is:
[[1.017544 ]
 [1.0401969]]

After 11500 raining steps, w1 is:
[[1.0212401]
 [1.0467982]]

After 12000 raining steps, w1 is:
[[1.0238953]
 [1.044576 ]]

After 12500 raining steps, w1 is:
[[1.0178354]
 [1.0388858]]

After 13000 raining steps, w1 is:
[[1.0215315]
 [1.0454872]]

After 13500 raining steps, w1 is:
[[1.0154716]
 [1.039797 ]]

After 14000 raining steps, w1 is:
[[1.0191677]
 [1.0463983]]

After 14500 raining steps, w1 is:
[[1.0162914]
 [1.0427582]]

After 15000 raining steps, w1 is:
[[1.0189465]
 [1.040536 ]]

After 15500 raining steps, w1 is:
[[1.0216017]
 [1.0383139]]

After 16000 raining steps, w1 is:
[[1.0252978]
 [1.0449152]]

After 16500 raining steps, w1 is:
[[1.0192379]
 [1.039225 ]]

After 17000 raining steps, w1 is:
[[1.022934 ]
 [1.0458263]]

After 17500 raining steps, w1 is:
[[1.0168741]
 [1.0401361]]

After 18000 raining steps, w1 is:
[[1.0205702]
 [1.0467374]]

After 18500 raining steps, w1 is:
[[1.0232253]
 [1.0445153]]

After 19000 raining steps, w1 is:
[[1.0171654]
 [1.038825 ]]

After 19500 raining steps, w1 is:
[[1.0208615]
 [1.0454264]]

Final w1 is
 [[1.020171 ]
 [1.0425103]]

Process finished with exit code 0

北京大学曹健——Tensorflow笔记 04 神经网络优化

# -*- coding: utf-8 -*-
#  酸奶成本1元,酸奶利润9元,预测少了损失大,故不要预测少,故生成的模型会多预测一些
# 0 导入模块,生成数据集
import tensorflow as tf
import numpy as np
BATCH_SIZE = 8  # 一次喂入神经网络的一小撮特征是8个
SEED = 23455  # 随机种子是23455,保证每次生成的数据集相同
COST = 9
PROFIT= 1

rdm = np.random.RandomState(SEED)
X = rdm.rand(32, 2)  # 生成32行个0~1之间的随机数,包括X1和X2,即32行2列的数据集
Y_ = [[x1 + x2 + (rdm.rand() / 10.0 - 0.05)] for (x1, x2) in X]  # 取出每组的x1与x2求和,
# 再加上随机噪声,构建标准答案Y_,.rand()函数会生成0~1之间的开区间随机数,除以10变成0~0.1
# 之间的随机数,再减去0.05再变成-0.05到+0.05之间的随机数

# 1定义神经网络的输入,参数和输出,定义前向传播过程。
# 给神经网络的输入x,y_用tf.placeholder占位
x = tf.placeholder(tf.float32, shape=(None, 2))
y_ = tf.placeholder(tf.float32, shape=(None, 1))
w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))  # 定义w1参数
y = tf.matmul(x, w1)  # 定义输出y

# 2 定义损失函数及反向传播方法
# 定义损失函数为MSE,反向传播方法为梯度下降,学习率为0.001,让均方误差向减小的方向优化
loss = tf.reduce_sum(tf.where(tf.greater(y,y_),(y - y_)*COST,(y_  -y)*PROFIT))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

# 3 生成会话,训练STEPS轮
with tf.Session() as sess:  # with结构中初始化所有变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    STEPS = 20000  # 给出训练轮数
    for i in range(STEPS):  # 用for循环开始训练
        start = (i * BATCH_SIZE) % 32
        end = (i * BATCH_SIZE) % 32 + BATCH_SIZE
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
        # 每轮从X的数据集,以及标准答案Y_抽取相应的从start开始到end结束,Y是特征,喂入神经网络
        # 对train_step进行运算
        if i % 500 == 0:  # 计算第一层神经网络的参数,每500轮打印一次第一层神经网络参数的值w1
            print("After %d raining steps, w1 is:" % (i))
            print(sess.run(w1), "\n")
    print("Final w1 is", "\n", sess.run(w1))



C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/PycharmProjects/untitled4/TS4_3.py
2018-09-28 23:26:33.959127: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
After 0 raining steps, w1 is:
[[-0.80594873]
 [ 1.4873729 ]]

After 500 raining steps, w1 is:
[[0.8732146]
 [1.006204 ]]

After 1000 raining steps, w1 is:
[[0.9658064]
 [0.9698208]]

After 1500 raining steps, w1 is:
[[0.9645447]
 [0.9682946]]

After 2000 raining steps, w1 is:
[[0.9602475]
 [0.9742084]]

After 2500 raining steps, w1 is:
[[0.96100295]
 [0.96993417]]

After 3000 raining steps, w1 is:
[[0.9654102]
 [0.9761159]]

After 3500 raining steps, w1 is:
[[0.96414846]
 [0.9745897 ]]

After 4000 raining steps, w1 is:
[[0.95985126]
 [0.9805035 ]]

After 4500 raining steps, w1 is:
[[0.9636422]
 [0.9687893]]

After 5000 raining steps, w1 is:
[[0.959345 ]
 [0.9747031]]

After 5500 raining steps, w1 is:
[[0.9667877]
 [0.9734448]]

After 6000 raining steps, w1 is:
[[0.9641995]
 [0.9676626]]

After 6500 raining steps, w1 is:
[[0.9655712 ]
 [0.98128426]]

After 7000 raining steps, w1 is:
[[0.9653083]
 [0.9817019]]

After 7500 raining steps, w1 is:
[[0.9670821 ]
 [0.97273576]]

After 8000 raining steps, w1 is:
[[0.964802 ]
 [0.9759015]]

After 8500 raining steps, w1 is:
[[0.9658656]
 [0.9805752]]

After 9000 raining steps, w1 is:
[[0.9646039]
 [0.979049 ]]

After 9500 raining steps, w1 is:
[[0.96303403]
 [0.9685749 ]]

After 10000 raining steps, w1 is:
[[0.95873684]
 [0.9744887 ]]

After 10500 raining steps, w1 is:
[[0.9598004]
 [0.9791624]]

After 11000 raining steps, w1 is:
[[0.966935 ]
 [0.9689562]]

After 11500 raining steps, w1 is:
[[0.95929414]
 [0.97336197]]

After 12000 raining steps, w1 is:
[[0.96004957]
 [0.9690877 ]]

After 12500 raining steps, w1 is:
[[0.9600948 ]
 [0.97845334]]

After 13000 raining steps, w1 is:
[[0.96085024]
 [0.9741791 ]]

After 13500 raining steps, w1 is:
[[0.9649493 ]
 [0.97141284]]

After 14000 raining steps, w1 is:
[[0.96499455]
 [0.98077846]]

After 14500 raining steps, w1 is:
[[0.96373284]
 [0.9792523 ]]

After 15000 raining steps, w1 is:
[[0.962163  ]
 [0.96877813]]

After 15500 raining steps, w1 is:
[[0.9601911]
 [0.9808918]]

After 16000 raining steps, w1 is:
[[0.95892936]
 [0.97936565]]

After 16500 raining steps, w1 is:
[[0.9660639]
 [0.9691594]]

After 17000 raining steps, w1 is:
[[0.9604402 ]
 [0.97081715]]

After 17500 raining steps, w1 is:
[[0.96484745]
 [0.97699887]]

After 18000 raining steps, w1 is:
[[0.9642764 ]
 [0.96846855]]

After 18500 raining steps, w1 is:
[[0.9599792 ]
 [0.97438234]]

After 19000 raining steps, w1 is:
[[0.9630599]
 [0.976308 ]]

After 19500 raining steps, w1 is:
[[0.967159  ]
 [0.97354174]]

Final w1 is
 [[0.9661967 ]
 [0.97694933]]

Process finished with exit code 0

由上面三个实例可知,当cost偏大,模型向w偏小的方向预测,cost偏小,模型尽量向w偏大的方向预测,cost=profit,预测的w近似于1

北京大学曹健——Tensorflow笔记 04 神经网络优化

 交叉熵:用于表征两个概率分布之间的距离,交叉熵越大,两个概率分布越远,交叉熵越小,两个概率分布越近

y:预测结果的概率分布  y_:标准答案的概率分布

y_=(1,0):表示第一种情况发生的概率为100%,第二种情况发生的概率为0

北京大学曹健——Tensorflow笔记 04 神经网络优化

Tensor flow中,为了保证输入的信息有意义,设定大于1.0为1.0,小于1e-12的值为1e-12,预防出现log(0)

工程上用以下公式来计算

北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化

# -*- coding: utf-8 -*-
# 设损失函数 loss=(w+1)^2,另w初值是常数5。反向传播就是求最优的w,即求最小的loss对应的w值
# 0 导入模块,生成数据集
import tensorflow as tf

# 定义待优化参数初值为5
w = tf.Variable(tf.constant(5,dtype=tf.float32))

#定义损失函数loss
loss = tf.square(w+1)

#定义反向传播方法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

# 生成会话,训练STEPS轮
with tf.Session() as sess:  # with结构中初始化所有变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    STEPS = 40  # 给出训练轮数
    for i in range(STEPS):  # 用for循环开始训练
        sess.run(train_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print("After %s raining steps, w1 is: %f,loss is %f" % (i,w_val,loss_val))

C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/PycharmProjects/untitled4/TS4_4.py
2018-09-30 09:25:39.047364: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
After 0 raining steps, w1 is: 2.600000,loss is 12.959999
After 1 raining steps, w1 is: 1.160000,loss is 4.665599
After 2 raining steps, w1 is: 0.296000,loss is 1.679616
After 3 raining steps, w1 is: -0.222400,loss is 0.604662
After 4 raining steps, w1 is: -0.533440,loss is 0.217678
After 5 raining steps, w1 is: -0.720064,loss is 0.078364
After 6 raining steps, w1 is: -0.832038,loss is 0.028211
After 7 raining steps, w1 is: -0.899223,loss is 0.010156
After 8 raining steps, w1 is: -0.939534,loss is 0.003656
After 9 raining steps, w1 is: -0.963720,loss is 0.001316
After 10 raining steps, w1 is: -0.978232,loss is 0.000474
After 11 raining steps, w1 is: -0.986939,loss is 0.000171
After 12 raining steps, w1 is: -0.992164,loss is 0.000061
After 13 raining steps, w1 is: -0.995298,loss is 0.000022
After 14 raining steps, w1 is: -0.997179,loss is 0.000008
After 15 raining steps, w1 is: -0.998307,loss is 0.000003
After 16 raining steps, w1 is: -0.998984,loss is 0.000001
After 17 raining steps, w1 is: -0.999391,loss is 0.000000
After 18 raining steps, w1 is: -0.999634,loss is 0.000000
After 19 raining steps, w1 is: -0.999781,loss is 0.000000
After 20 raining steps, w1 is: -0.999868,loss is 0.000000
After 21 raining steps, w1 is: -0.999921,loss is 0.000000
After 22 raining steps, w1 is: -0.999953,loss is 0.000000
After 23 raining steps, w1 is: -0.999972,loss is 0.000000
After 24 raining steps, w1 is: -0.999983,loss is 0.000000
After 25 raining steps, w1 is: -0.999990,loss is 0.000000
After 26 raining steps, w1 is: -0.999994,loss is 0.000000
After 27 raining steps, w1 is: -0.999996,loss is 0.000000
After 28 raining steps, w1 is: -0.999998,loss is 0.000000
After 29 raining steps, w1 is: -0.999999,loss is 0.000000
After 30 raining steps, w1 is: -0.999999,loss is 0.000000
After 31 raining steps, w1 is: -1.000000,loss is 0.000000
After 32 raining steps, w1 is: -1.000000,loss is 0.000000
After 33 raining steps, w1 is: -1.000000,loss is 0.000000
After 34 raining steps, w1 is: -1.000000,loss is 0.000000
After 35 raining steps, w1 is: -1.000000,loss is 0.000000
After 36 raining steps, w1 is: -1.000000,loss is 0.000000
After 37 raining steps, w1 is: -1.000000,loss is 0.000000
After 38 raining steps, w1 is: -1.000000,loss is 0.000000
After 39 raining steps, w1 is: -1.000000,loss is 0.000000

Process finished with exit code 0

北京大学曹健——Tensorflow笔记 04 神经网络优化

 

北京大学曹健——Tensorflow笔记 04 神经网络优化北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化

 

# -*- coding: utf-8 -*-
# 设损失函数 loss=(w+1)^2,另w初值是常数10。反向传播就是求最优的w,即求最小的loss对应的w值
# 使用指数衰减学习率,在迭代初期得到较高的下降速率,可以在较小的训练轮数下取得更好的收敛度

import tensorflow as tf

LEARNING_RATE_BASE = 0.1  #最初学习率
LEARNING_RATE_DECAY = 0.99  #学习率衰减率
#  学习率多久更新一次
LEARNING_RATE_STEP = 1  #喂入多少轮BATCH_SIZE后,更新一次学习率,一般设为:总样本数/BATCH_SIZE

# 运行了几轮BATCH_SIZE计数器,初值为0,设为不被训练
global_step = tf.Variable(0,trainable=False)
# 定义指数下降学习率
learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,LEARNING_RATE_STEP,LEARNING_RATE_DECAY,staircase=True)
# 定义待优化参数初值为5
w = tf.Variable(tf.constant(5,dtype=tf.float32))
#定义损失函数loss
loss = tf.square(w+1)
#定义反向传播方法
train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss,global_step=global_step)

# 生成会话,训练STEPS轮
with tf.Session() as sess:  # with结构中初始化所有变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    STEPS = 40  # 给出训练轮数
    for i in range(STEPS):  # 用for循环开始训练
        sess.run(train_step)
        learning_rate_val = sess.run(learning_rate)
        global_setp_val = sess.run(global_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print("After %s raining steps, global_setp is: %f,w is %f,learning_rate is %f,loss is %f" % (i,global_setp_val,w_val,learning_rate_val,loss_val))

C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/PycharmProjects/untitled4/TS4_5.py
2018-09-30 10:33:04.998480: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
After 0 raining steps, global_setp is: 1.000000,w is 3.800000,learning_rate is 0.099000,loss is 23.040001
After 1 raining steps, global_setp is: 2.000000,w is 2.849600,learning_rate is 0.098010,loss is 14.819419
After 2 raining steps, global_setp is: 3.000000,w is 2.095001,learning_rate is 0.097030,loss is 9.579033
After 3 raining steps, global_setp is: 4.000000,w is 1.494386,learning_rate is 0.096060,loss is 6.221961
After 4 raining steps, global_setp is: 5.000000,w is 1.015167,learning_rate is 0.095099,loss is 4.060896
After 5 raining steps, global_setp is: 6.000000,w is 0.631886,learning_rate is 0.094148,loss is 2.663051
After 6 raining steps, global_setp is: 7.000000,w is 0.324608,learning_rate is 0.093207,loss is 1.754587
After 7 raining steps, global_setp is: 8.000000,w is 0.077684,learning_rate is 0.092274,loss is 1.161403
After 8 raining steps, global_setp is: 9.000000,w is -0.121202,learning_rate is 0.091352,loss is 0.772287
After 9 raining steps, global_setp is: 10.000000,w is -0.281761,learning_rate is 0.090438,loss is 0.515867
After 10 raining steps, global_setp is: 11.000000,w is -0.411674,learning_rate is 0.089534,loss is 0.346128
After 11 raining steps, global_setp is: 12.000000,w is -0.517024,learning_rate is 0.088638,loss is 0.233266
After 12 raining steps, global_setp is: 13.000000,w is -0.602644,learning_rate is 0.087752,loss is 0.157891
After 13 raining steps, global_setp is: 14.000000,w is -0.672382,learning_rate is 0.086875,loss is 0.107334
After 14 raining steps, global_setp is: 15.000000,w is -0.729305,learning_rate is 0.086006,loss is 0.073276
After 15 raining steps, global_setp is: 16.000000,w is -0.775868,learning_rate is 0.085146,loss is 0.050235
After 16 raining steps, global_setp is: 17.000000,w is -0.814036,learning_rate is 0.084294,loss is 0.034583
After 17 raining steps, global_setp is: 18.000000,w is -0.845387,learning_rate is 0.083451,loss is 0.023905
After 18 raining steps, global_setp is: 19.000000,w is -0.871193,learning_rate is 0.082617,loss is 0.016591
After 19 raining steps, global_setp is: 20.000000,w is -0.892476,learning_rate is 0.081791,loss is 0.011561
After 20 raining steps, global_setp is: 21.000000,w is -0.910065,learning_rate is 0.080973,loss is 0.008088
After 21 raining steps, global_setp is: 22.000000,w is -0.924629,learning_rate is 0.080163,loss is 0.005681
After 22 raining steps, global_setp is: 23.000000,w is -0.936713,learning_rate is 0.079361,loss is 0.004005
After 23 raining steps, global_setp is: 24.000000,w is -0.946758,learning_rate is 0.078568,loss is 0.002835
After 24 raining steps, global_setp is: 25.000000,w is -0.955125,learning_rate is 0.077782,loss is 0.002014
After 25 raining steps, global_setp is: 26.000000,w is -0.962106,learning_rate is 0.077004,loss is 0.001436
After 26 raining steps, global_setp is: 27.000000,w is -0.967942,learning_rate is 0.076234,loss is 0.001028
After 27 raining steps, global_setp is: 28.000000,w is -0.972830,learning_rate is 0.075472,loss is 0.000738
After 28 raining steps, global_setp is: 29.000000,w is -0.976931,learning_rate is 0.074717,loss is 0.000532
After 29 raining steps, global_setp is: 30.000000,w is -0.980378,learning_rate is 0.073970,loss is 0.000385
After 30 raining steps, global_setp is: 31.000000,w is -0.983281,learning_rate is 0.073230,loss is 0.000280
After 31 raining steps, global_setp is: 32.000000,w is -0.985730,learning_rate is 0.072498,loss is 0.000204
After 32 raining steps, global_setp is: 33.000000,w is -0.987799,learning_rate is 0.071773,loss is 0.000149
After 33 raining steps, global_setp is: 34.000000,w is -0.989550,learning_rate is 0.071055,loss is 0.000109
After 34 raining steps, global_setp is: 35.000000,w is -0.991035,learning_rate is 0.070345,loss is 0.000080
After 35 raining steps, global_setp is: 36.000000,w is -0.992297,learning_rate is 0.069641,loss is 0.000059
After 36 raining steps, global_setp is: 37.000000,w is -0.993369,learning_rate is 0.068945,loss is 0.000044
After 37 raining steps, global_setp is: 38.000000,w is -0.994284,learning_rate is 0.068255,loss is 0.000033
After 38 raining steps, global_setp is: 39.000000,w is -0.995064,learning_rate is 0.067573,loss is 0.000024
After 39 raining steps, global_setp is: 40.000000,w is -0.995731,learning_rate is 0.066897,loss is 0.000018

Process finished with exit code 0

北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化 北京大学曹健——Tensorflow笔记 04 神经网络优化

 北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化 北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化 北京大学曹健——Tensorflow笔记 04 神经网络优化

#coding:utf-8
#0 导入模块,生成模拟数据集
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
BATCH_SIZE = 30
seed = 2
#基于seed产生随机数
rdm = np.random.RandomState(seed)
#随机数返回300行2列的矩阵,表示300组坐标点(x0,x1)作为输入数据集
X = rdm.randn(300,2)
# 从这个300行2列的矩阵中取出1行,判断如果两个坐标的平方和小于2,给Y赋值1,其余赋值0
# 作为输入数据集的标签(正确答案)
Y_ = [int(x0*x0 + x1*x1 <2) for (x0,x1) in X]
# 遍历Y中的每个元素,1赋值‘red’,其余赋值‘blue’,这样可视化显示时人可以直观区分
Y_c = [[ 'red' if y else 'blue'] for y in Y_]
# 对数据集X和标签Y进行shape整理,第一个元素用-1表示,随第二个参数计算得到,第二个元素表示多少列,
# 把X整形为n行2列,把Y整形为n行1列
X = np.vstack(X).reshape(-1,2)    #-1 表示为n行
Y_ = np.vstack(Y_).reshape(-1,1)
print(X)
print(Y_)
print(Y_c)
# 利用plot.scatter画出数据集X各行中第0列元素与第1列元素的点,即各行的(x0,x1),用各行Y_c对应的
# 值表示颜色(c是color的缩写)
plt.scatter(X[:,0],X[:,1],c=np.squeeze(Y_c))
plt.show()

# 定义神经网络的输入,参数和输出,定义前向传播过程
def get_weight(shape,regularizer):
    w = tf.Variable(tf.random_normal(shape),dtype=tf.float32)
    tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(regularizer)(w))
    return w

def get_bias(shape):
    b = tf.Variable(tf.constant(0.01,shape=shape))
    return

x = tf.placeholder(tf.float32,shape=(None,2))
y_ = tf.placeholder(tf.float32,shape=(None,1))

w1 = get_weight([2,11],0.01)
b1 = get_bias([11])   #b1为11个
y1 = tf.nn.relu(tf.matmul(x,w1)+b1)

w2 = get_weight([11,1],0.01)
b1 = get_bias([1])
y1 = tf.matmul(y1,w2)+b2 #输出层不过**函数

# 定义损失函数
lose_mse = tf.reduce_mean(tf.square(y-y_))
loss_total = lose_mse + tf.add_n(tf.get_collection('losses'))

# 定义反向传播方法,不含正则化
with tf.Session() as sess:  # with结构中初始化所有变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    STEPS = 40000  # 给出训练轮数
    for i in range(STEPS):  # 用for循环开始训练
        start = (i * BATCH_SIZE) % 300
        end = start + BATCH_SIZE
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
        # 每轮从X的数据集,以及标准答案Y_抽取相应的从start开始到end结束,Y是特征,喂入神经网络
        # 对train_step进行运算
        if i % 2000 == 0:  # 计算第一层神经网络的参数,每2000轮打印一次第一层神经网络参数的值w1
            loss_mse_v = sess.run(lose_mse,feed_dict={x:X,y_:Y_})
            print("After %d raining steps, loss is:%f" % (i,loss_mse_v))
    # xx在-3到3之间以步长为0.01,yy在-3到3之间以步长为0.01,生成二维网络坐标点
    xx,yy = np.mgrid[-3:3:0.01,-3:3:0.01]
    # 将xx,yy拉直,并合并为一个2列的矩阵,得到一个网格坐标点的集合
    grid = np.c_[xx.ravel(),yy.ravel()]
    # 将网格坐标点喂入神经网络,probs为输出
    probs = sess.run(y,feed_dict={x:grid})
    #probs的shape调整为xx的样子
    probs = probs.reshapte(xx.shape)
    print("w1:\n",sess.run(w1))
    print("b1:\n", sess.run(b1))
    print("w2:\n", sess.run(w2))
    print("b2:\n", sess.run(b2))

plt.scatter(X[:0],X[:1],c=np.squeeze(Y_c))
plt.contour(xx,yy,probs,levels=[.5])
plt.show()

# 定义方向传播方法
train_step = tf.train.AdamOptimizer(0.0001).minimize(loss_total)
with tf.Session() as sess:  # with结构中初始化所有变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    STEPS = 40000  # 给出训练轮数
    for i in range(STEPS):  # 用for循环开始训练
        start = (i * BATCH_SIZE) % 300
        end = start + BATCH_SIZE
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})
        # 每轮从X的数据集,以及标准答案Y_抽取相应的从start开始到end结束,Y是特征,喂入神经网络
        # 对train_step进行运算
        if i % 2000 == 0:  # 计算第一层神经网络的参数,每2000轮打印一次第一层神经网络参数的值w1
            loss_v = sess.run(lose_total,feed_dict={x:X,y_:Y_})
            print("After %d raining steps, loss is:%f" % (i,loss_v))
    # xx在-3到3之间以步长为0.01,yy在-3到3之间以步长为0.01,生成二维网络坐标点
    xx,yy = np.mgrid[-3:3:0.01,-3:3:0.01]
    # 将xx,yy拉直,并合并为一个2列的矩阵,得到一个网格坐标点的集合
    grid = np.c_[xx.ravel(),yy.ravel()]
    # 将网格坐标点喂入神经网络,probs为输出
    probs = sess.run(y,feed_dict={x:grid})
    #probs的shape调整为xx的样子
    probs = probs.reshapte(xx.shape)
    print("w1:\n",sess.run(w1))
    print("b1:\n", sess.run(b1))
    print("w2:\n", sess.run(w2))
    print("b2:\n", sess.run(b2))

plt.scatter(X[:0],X[:1],c=np.squeeze(Y_c))
plt.contour(xx,yy,probs,levels=[.5])
plt.show()

 

北京大学曹健——Tensorflow笔记 04 神经网络优化 北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化 北京大学曹健——Tensorflow笔记 04 神经网络优化

北京大学曹健——Tensorflow笔记 04 神经网络优化