反向傳播--->訓練模型參數,在所有參數上用梯度下降,使NN模型再訓練資料上的損失函數最小。
損失函數(loss):預測值(y)與已知答案(y_)的差距
均方誤差MSE:
loss=tf.reduce_mean(tf.square(y,y_))
反向傳播訓練方法:以減小loss值為優化目标
train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) 梯度下降
train_step=tf.train.MomentumOptimizer(learning_rate,momentum).minimize(loss)momentum優化器
train_step=tf.train.AdamOptimizer(learning_rate).minimize(loss) adam優化
learining_rate 學習率:決定參數每次更新的幅度
參考代碼:
#tf_3_3.py
#建立一個兩層網絡,輸入層2,中間層3,輸出層1
import tensorflow as tf
import numpy as np
BATCH_SIZE=10
seed=23455
#虛拟樣本,基于seed生成随機數
rng=np.random.RandomState(seed)
#随機生成
X=rng.rand(80,2)
#生成标簽 0、1
Y=[[int(x0+x1<1)] for (x0,x1) in X]
print("X:",X)
print("Y:",Y)
x=tf.placeholder(tf.float32)
y_=tf.placeholder(tf.float32)
w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2=tf.Variable(tf.random_normal([3,3],stddev=1,seed=1))
w3=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
a=tf.matmul(x,w1)
b=tf.matmul(a,w2)
y=tf.matmul(b,w3)
#定義loss和反向傳播方法
learning_rate=0.001
loss=tf.reduce_mean(tf.square(y-y_))
train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
#train_step=tf.train.MomentumOptimizer(learning_rate,momentum=0.1).minimize(loss)
#train_step=tf.train.AdamOptimizer(learning_rate,beta1=0.9,beta2=0.999).minimize(loss)
with tf.Session() as sess:
init_op=tf.global_variables_initializer()
sess.run(init_op)
STEPS=3000
for i in range(STEPS):
start=(i*BATCH_SIZE)%80
end=start+BATCH_SIZE
sess.run(train_step,feed_dict={x:X[start:end],y_:Y[start:end]})
if i%300 == 0:
total_loss=sess.run(loss,feed_dict={x:X,y_:Y})
print("%d:loss:%g",i,total_loss)
總結:
搭建神經網絡的八股:準備,前傳,反傳,疊代
1.準備: import
常量定義
生成資料集
2.前傳:定義輸入、參數、和輸出
x=
y_=
w1=
w2=
w3
a
b
y
3.反向傳播:定義損失函數、反向傳播方法
loss=
train_step=
4.生成會話、訓練steps輪
with tf.Session() as sess:
........