1. 程式人生 > >TensorFlow神經網路:模組化的神經網路八股

TensorFlow神經網路:模組化的神經網路八股

1、前向傳播:

  • 搭建從輸入到輸出的網路結構
  • forward.py:
# 定義前向傳播過程
def forward(x, regularizer):
	w = 
	b = 
	y = 
	return y

# 給w賦初值,並把w的正則化損失加到總損失中
def get_weight(shape, regularizer):
	w = tf.Variable()
	tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
	return w

def get_bias
(shape) b = tf.Variable() return b

2、反向傳播

  • 訓練網路,優化網路引數,提高模型準確性
  • backward.py:
# 定義反向傳播
def backward():
	# 對資料集x和標準答案y_佔位
	x = tf.placeholder()
	y_ = tf.placeholder(
		)
	# 利用forward模組復現前向傳播網路的結構,計算得到y
	y = forward.forward(x, REGULARIZER)

	# 定義輪數計數器
	global_step = tf.Variable(0, trainable =
False) # 定義損失函式 loss = ''' # 均方誤差 loss = tf.reduce_mean(tf.square(y - y_)) # 交叉熵 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = y, lables = tf.argmax(y_, 1)) loss = tf.reduce_mean(ce) ''' # 在訓練網路模型時 # 常常將1正則化、2指數衰減學習率、3滑動平均這三個方法作為優化模型的方法 ''' # 使用正則化時的損失函式 loss = 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]): train_op = tf.no_op(name = 'train') ''' # 訓練過程 with tf.Session() as sess: # 初始化所有引數 init_op = tf.global_variables_initializer() sess.run(init_op) # 迴圈迭代 for i in range(STEPS): # 每輪呼叫sess.run執行訓練過程train_step sess.run(train_step, feed_dict = {x: , y_: }) # 每執行一定輪數,打印出當前的loss資訊 if i % 輪數==0 print

3、判斷主檔案

# 判斷python執行檔案是否為主檔案,如果是,則執行
if __name__ == '__main__':
	backward()

4、例項模組化展示

  • 加入指數衰減學習率–優化效率
  • 加入正則化–提高泛化效能
  • 模組化設計
    generateds.py
# modelNN_generateds.py
# 資料匯入模組,生成模擬資料集
# coding: utf-8

import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'  #hide warnings

seed = 2

def generateds():
	# 基於seed產生隨機數
	rdm = np.random.RandomState(seed)

	# 隨機數返回300行2列的矩陣,表示300組座標點,作為輸入資料集
	X = rdm.randn(300, 2)

	# 手工標註資料分類
	Y_ = [int(x0*x0 + x1*x1 < 2)for (x0, x1) in X]

	# Y_為1,標記紅色,否則藍色
	Y_c = [['red' if y else 'blue'] for y in Y_]

	# 對資料集和標籤進行reshape, X整理為n行2列,Y為n行1列,第一個元素-1表示n行
	X = np.vstack(X).reshape(-1, 2)
	Y_ = np.vstack(Y_).reshape(-1, 1) 

	return X, Y_, Y_c

	print("X:\n")
	print(X)
	print("Y_:\n")
	print(Y_)
	print("Y_c:\n")
	print(Y_c)

forward.py

# modelNN_generateds.py
# 前向傳播模組
# 定義神經網路的輸入、引數和輸出,定義前向傳播過程
# coding: utf-8

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'  #hide warnings

# 給w賦初值,並把w的正則化損失加到總損失中
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

# 給b賦初值
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


backward.py

# modelNN_generateds.py
# 反向傳播模組
# 定義神經網路的反向傳播過程
# coding: utf-8

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import modelNN_generateds
import modelNN_forward
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'  #hide warnings

# 定義超引數
STEPS = 40000 #訓練輪數
BATCH_SIZE = 30 
LEARNING_RATE_BASE = 0.001 #初始學習率
LEARNING_RATE_DECAY = 0.999 # 學習率衰減率
REGULARIZER = 0.01 # 正則化引數

def backward():
	# placeholder佔位
	x = tf.placeholder(tf.float32, shape = (None, 2))
	y_ = tf.placeholder(tf.float32, shape = (None, 1))

	# 生成資料集
	X, Y_, Y_c = modelNN_generateds.generateds()

	# 前向傳播推測輸出y
	y = modelNN_forward.forward(x, REGULARIZER)

	# 定義global_step
	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:
				loss_v = sess.run(loss_total, feed_dict = {x: X, y_: Y_})
				print("after %d steps, loss for total 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))
	# 給probs值為0.5的所有點(xx, yy)上色
	plt.contour(xx, yy, probs, levels = [.5])
	plt.show()

# 判斷python執行檔案是否為主檔案,如果是,則執行
if __name__ == '__main__':
	backward()


在這裡插入圖片描述