搭建模塊化的神經網絡八股
前向傳播就是搭建網絡,設計網絡結構(forward.py)
1.def forward(x, regularizer):#完成網絡結構的設計,給出從輸入到輸出的數據通路。
w =
b =
y =
return y
2.def get_weight(shape, regularizer): #w的shape,以及正則化權重
w = tf.Variable() #給w賦初值
tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(regularizer)(w))
return w
3.def get_bias(shape):
b = tf.Variabel() #賦初值
return b
反向傳播就是訓練網絡,優化網絡參數(backward.py)
def backward():
x = tf.placeholder () #占位
y_ = tf.placeholder()
y = forward.forward(x,REGULARIZER) #復現前向傳播結構,求y
global_step = tf.Variable(0,trainable = False)
loss =
#損失函數,正則化
#loss可以是:
y與y_的差距(loss_mse) = tf.reduce_mean(tf.square(y-y_))
#也可以是:
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = y,labels = tf.argmax(y_,1))
y與y_的差距(cem) = tf.reduce_mean(ce)
#加入正則化后:
loss = y于y_的差距+tf.add_n(tf.get_collection("losses"))
#指數衰減學習率
learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,數據集總樣本數/BATCH_SIZE,LEARNING_RATE_DECAY,staircase = True)
train_step =tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step)
#滑動平均
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([train_step,ema_op]):
trian_op = tf.no_op(name = "train")
with tf.Session() as sess:
init.run(init_op)
for i in range(STEPS):
sess.run(train_step,feed_dict = {x:, y:})
if i % 輪數 == 0 :
print()
if __name__ = "__main__": #判斷Python運行的文件是否是主文件,如果是主文件,就執行backward這個函數
backward()
案例
#opt4_8_generateds.py
#0導入模塊 ,生成模擬數據集
import numpy as np
import matplotlib.pyplot as plt
seed = 2
def generateds():
#基于seed產生隨機數
rdm = np.random.RandomState(seed)
#隨機數返回300行2列的矩陣,表示300組坐標點(x0,x1)作為輸入數據集
X = rdm.randn(300,2)
#從X這個300行2列的矩陣中取出一行,判斷如果兩個坐標的平方和小于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進行形狀整理,第一個元素為-1表示跟隨第二列計算,第二個元素表示多少列,可見X為兩列,Y為1列
X = np.vstack(X).reshape(-1,2)
Y_ = np.vstack(Y_).reshape(-1,1)
return X, Y_, Y_c
#print X
#print Y_
#print Y_c
#用plt.scatter畫出數據集X各行中第0列元素和第1列元素的點即各行的(x0,x1),用各行Y_c對應的值表示顏色(c是color的縮寫)
#plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c))
#plt.show()
#opt4_8_forward.py
#0導入模塊 ,生成模擬數據集
import tensorflow as tf
#定義神經網絡的輸入、參數和輸出,定義前向傳播過程
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 b
def forward(x, regularizer):
w1 = get_weight([2,11], regularizer)
b1 = get_bias([11])
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
w2 = get_weight([11,1], regularizer)
b2 = get_bias([1])
y = tf.matmul(y1, w2) + b2
return y
#
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import opt4_8_generateds #兩個py文件放在\Anaconda3\Lib文件夾下
import opt4_8_forward
STEPS = 40000
BATCH_SIZE = 30
LEARNING_RATE_BASE = 0.001
LEARNING_RATE_DECAY = 0.999
REGULARIZER = 0.01
def backward():
x = tf.placeholder(tf.float32, shape=(None, 2))
y_ = tf.placeholder(tf.float32, shape=(None, 1))
X, Y_, Y_c = opt4_8_generateds.generateds()
y = opt4_8_forward.forward(x, REGULARIZER) #復現前向傳播求y
global_step = tf.Variable(0,trainable=False)
#指數學習衰減率
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
300/BATCH_SIZE,
LEARNING_RATE_DECAY,
staircase=True)
#定義損失函數
#損失函數為均方誤差
loss_mse = tf.reduce_mean(tf.square(y-y_))
loss_total = loss_mse + tf.add_n(tf.get_collection('losses'))
#定義反向傳播方法:包含正則化
train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total)
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
for i in range(STEPS):
start = (i*BATCH_SIZE) % 300
end = start + BATCH_SIZE
sess.run(train_step, feed_dict={x: X[start:end], y_:Y_[start:end]})
if i % 2000 == 0: #每2000輪打印一次參數
loss_v = sess.run(loss_total, feed_dict={x:X,y_:Y_})
print("After %d steps, loss is: %f" %(i, loss_v))
xx, yy = np.mgrid[-3:3:.01, -3:3:.01]
grid = np.c_[xx.ravel(), yy.ravel()]
probs = sess.run(y, feed_dict={x:grid})
probs = probs.reshape(xx.shape)
plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c))
plt.contour(xx, yy, probs, levels=[.5])
plt.show()
if __name__=='__main__':
backward()