[Python人工智能] 二.theano实现回归神经网络分析
從本篇文章開始,作者正式開始研究Python深度學習、神經網絡及人工智能相關知識。前一篇文章主要講解神經網絡基礎概念,同時講解Theano庫的安裝過程及基礎用法,這篇文章主要講解theano實現回歸神經網絡,主要是學習"莫煩大神" 網易云視頻的在線筆記,后面隨著深入會講解具體的項目及應用。基礎性文章,希望對您有所幫助,也建議大家一步步跟著學習,同時文章中存在錯誤或不足之處,還請海涵~
"莫煩大神" 網易云視頻地址:http://study.163.com/provider/1111519/course.html
從2014年開始,作者主要寫了三個Python系列文章,分別是基礎知識、網絡爬蟲和數據分析。
- Python基礎知識系列:Pythonj基礎知識學習與提升
- Python網絡爬蟲系列:Python爬蟲之Selenium+Phantomjs+CasperJS
- Python數據分析系列:知識圖譜、web數據挖掘及NLP
??
一. 定義神經網絡Layer類
神經網絡激勵函數參考:
http://deeplearning.net/software/theano/library/tensor/nnet/nnet.html
激勵函數相當于一個過濾器或激勵器,它把特有的信息或特征激活,常見的激活函數包括softplus、sigmoid、relu、softmax、elu、tanh等。對于隱藏層,我們可以使用relu、tanh、softplus等非線性關系;對于分類問題,我們可以使用sigmoid(值越小越接近于0,值越大越接近于1)、softmax函數,對每個類求概率,最后以最大的概率作為結果;對于回歸問題,可以使用線性函數(linear function)來實驗。
神經網絡首先需要添加神經層,將層(Layer)定義成類,通過類來添加神經層。神經層是相互鏈接,并且是全連接,從第一層輸入層傳入到隱藏層,最后傳輸至輸出層。假設接下來需要定義兩層內容:
? ? L1 = Layer(inputs, in_size=1, out_size=10, activation_function)
? ??參數包括輸入值,輸入節點數,輸出節點數和激勵函數
? ? L2 = Layer(L1.outputs, 10, 1, None)
? ? 參數中L1的輸出作為輸入值,L1的輸出10個節點作為輸入節點,輸出節點1個,激勵函數為None。
定義類的代碼如下,包括權重和bias,其中參數為隨機變量更有利于我們后面的更新,亂序更能促進神經網絡的學習。
class Layer(object):def __init__(self, inputs, in_size, out_size, activation_function=None):#權重: 平均值為0 方差為1 行數為in_size 列數為out_sizeself.W = theano.shared(np.random.normal(0,1,(in_size,out_size)))#biasself.b = theano.shared(np.zeros((out_size,) ) + 0.1)#乘法加biasself.Wx_plus_b = T.dot(inputs, self.W) + self.b #dot乘法#激勵函數self.activation_function = activation_function#默認為None,否則進行激活if activation_function is None: self.outputs = self.Wx_plus_belse: self.outputs = self.activation_function(self.Wx_plus_b)二. 回歸神經網絡實現
接下來開始跟著莫煩大聲實現了第一個神經網絡代碼,步驟如下:
1.制造虛擬數據
通過numpy.linspace生成300個隨機點進行訓練,形成y=x^2-0.5的虛擬數據。代碼如下:
#coding:utf-8 import numpy as np import theano.tensor as T import theano from theano import functionclass Layer(object):def __init__(self, inputs, in_size, out_size, activation_function=None):#權重: 平均值為0 方差為1 行數為in_size 列數為out_sizeself.W = theano.shared(np.random.normal(0,1,(in_size,out_size)))#biasself.b = theano.shared(np.zeros((out_size,) ) + 0.1)#乘法加biasself.Wx_plus_b = T.dot(inputs, self.W) + self.b #dot乘法#激勵函數self.activation_function = activation_function#默認為None,否則進行激活if activation_function is None: self.outputs = self.Wx_plus_belse: self.outputs = self.activation_function(self.Wx_plus_b)#回歸神經網絡 Regression#1.制造虛擬數據 import matplotlib.pyplot as plt #Make up some fake data x_data = np.linspace(-1,1,300)[:, np.newaxis] #300個點進行訓練 noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) - 0.5 + noise #y = x^2 - 0.5 #一元二次散點圖 plt.scatter(x_data, y_data) plt.show()生產的一元二次隨機散點圖如下圖所示:
圖1 散點圖結果
2.增加神經層
定義輸入x和y,注意其dtype類型為64位浮點型,整個代碼前后類型需要保持一致。同時定義了L1層和L2層:
? ? L1 = Layer(x, 1, 10, T.nnet.relu)
? ? 輸入為x,1維的data,神經元10個,relu非線性激勵函數
? ? L2 = Layer(L1.outputs, 10, 1, None)
? ? 輸入為L1輸出值,?L2的in_size為L1的神經元10,假設L2輸出為最終output
3.計算誤差與梯度下降更新
定義cost變量計算誤差,即預測值與真實值的差別;再定義梯度下降變量,其誤差越大,降低趨勢越大,通過梯度下降讓預測值更接近真實值。代碼中通過theano.function()函數更新神經網絡的四個參數,計算公式如下啊:
L1.W, L1.W-learnging_rate*gW1:
(原始的權重-學習效率*下降幅度)并且更新為L1.W,通過該方法將L1.W、L1.b、L2.W、L2.b更新。
4.預測結果
最后是預測結果,訓練時會給出x和y求cost,而預測時只給出輸入x,用來做預測。最后每隔50步輸出err,如果err不斷減小,說明神經網絡在學到東西,因為預測值與真實值誤差在不斷減小。
#coding:utf-8 import numpy as np import theano.tensor as T import theano from theano import functionclass Layer(object):def __init__(self, inputs, in_size, out_size, activation_function=None):#權重: 平均值為0 方差為1 行數為in_size 列數為out_sizeself.W = theano.shared(np.random.normal(0,1,(in_size,out_size)))#biasself.b = theano.shared(np.zeros((out_size,) ) + 0.1)#乘法加biasself.Wx_plus_b = T.dot(inputs, self.W) + self.b #dot乘法#激勵函數self.activation_function = activation_function#默認為None,否則進行激活if activation_function is None: self.outputs = self.Wx_plus_belse: self.outputs = self.activation_function(self.Wx_plus_b)#回歸神經網絡 Regression#1.制造虛擬數據 import matplotlib.pyplot as plt #Make up some fake data x_data = np.linspace(-1,1,300)[:, np.newaxis] #300個點進行訓練 noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) - 0.5 + noise #y = x^2 - 0.5 #一元二次散點圖 plt.scatter(x_data, y_data) plt.show()#2.定義輸入和神經元 #determine the inputs dytpe x = T.dmatrix('x') #d代表64位float類型 y = T.dmatrix('y') #add layers L1 = Layer(x, 1, 10, T.nnet.relu) L2 = Layer(L1.outputs, 10, 1, None)#3.計算誤差與梯度下降更新 #誤差為預測值與真實值差別 cost = T.mean(T.square(L2.outputs - y)) #mean()求平均誤差 #compute the gradients #梯度下降,讓預測值更接近真實值,誤差越大,降低趨勢越大 gW1, gb1, gW2, gb2 = T.grad(cost, [L1.W, L1.b, L2.W, L2.b]) #權重和bias #apply gradient descent #學習效率: 神經網絡中learning_rate通常小于1的數字,0.05保證神經網絡學習比較精細 learning_rate = 0.05#更新四個神經網絡的參數 train = theano.function(inputs = [x,y],outputs = cost,updates = [(L1.W, L1.W-learning_rate*gW1),(L1.b, L1.b-learning_rate*gb1),(L2.W, L2.W-learning_rate*gW2),(L2.b, L2.b-learning_rate*gb2)] ) #(L1.W, L1.W-learnging_rate*gW1) #(原始的權重-學習的效率*下降的幅度)更新為L1.W#4.預測結果 #訓練時會給y求cost, 而預測輸入只給出x用來做預測 predict = theano.function(inputs=[x], outputs=L2.outputs)for i in range(1000):#training 把x_data和y_data放到函數x、y中err = train(x_data, y_data) #誤差#每隔50步輸出err, 如果err不斷減小說明神經網絡在學到東西, 因為預測值與真實值誤差在不斷減小if i % 50 == 0: print(err)最后輸出誤差結果,可以看到在不斷減小,通過不斷更新權重和bias,神經網絡在不斷學習。
2.079139917311914 0.019342171772016286 0.010080951605219858 0.007202397774306516 0.005702041299886045 0.004946926023254156 0.004565940080184372 0.0043433009094413985 0.00421325480276665 0.0041214880336559725 0.004046461715568916 0.003989685842213987 0.003934933552824453 0.003886586291155118 0.003843283475886867 0.0038068442317786316 0.0037721487639369986 0.0037432478192238835 0.003712756532212612 0.00368813308403329三. 回歸神經網絡可視化
最后補充神經網絡不斷學習的擬合圖形,從最早不合理的圖形到后面基本擬合,同時cost誤差也在不斷減小,說明神經網絡的真實值和預測值在不斷更新接近,神經網絡正常運行。結果如下:
圖2 第一次擬合結果
圖3 第二次擬合結果
圖4 最后一次擬合結果
完整代碼及注釋如下所示:
#coding:utf-8 import numpy as np import theano.tensor as T import theano from theano import functionclass Layer(object):def __init__(self, inputs, in_size, out_size, activation_function=None):#權重: 平均值為0 方差為1 行數為in_size 列數為out_sizeself.W = theano.shared(np.random.normal(0,1,(in_size,out_size)))#biasself.b = theano.shared(np.zeros((out_size,) ) + 0.1)#乘法加biasself.Wx_plus_b = T.dot(inputs, self.W) + self.b #dot乘法#激勵函數self.activation_function = activation_function#默認為None,否則進行激活if activation_function is None: self.outputs = self.Wx_plus_belse: self.outputs = self.activation_function(self.Wx_plus_b)#回歸神經網絡 Regression#1.制造虛擬數據 import matplotlib.pyplot as plt #Make up some fake data x_data = np.linspace(-1,1,300)[:, np.newaxis] #300個點進行訓練 noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) - 0.5 + noise #y = x^2 - 0.5 #一元二次散點圖 plt.scatter(x_data, y_data) plt.show()#2.定義輸入和神經元 #determine the inputs dytpe x = T.dmatrix('x') #d代表64位float類型 y = T.dmatrix('y') #add layers L1 = Layer(x, 1, 10, T.nnet.relu) L2 = Layer(L1.outputs, 10, 1, None)#3.計算誤差與梯度下降更新 #誤差為預測值與真實值差別 cost = T.mean(T.square(L2.outputs - y)) #mean()求平均誤差 #compute the gradients #梯度下降,讓預測值更接近真實值,誤差越大,降低趨勢越大 gW1, gb1, gW2, gb2 = T.grad(cost, [L1.W, L1.b, L2.W, L2.b]) #權重和bias #apply gradient descent #學習效率: 神經網絡中learning_rate通常小于1的數字,0.05保證神經網絡學習比較精細 learning_rate = 0.05#更新四個神經網絡的參數 train = theano.function(inputs = [x,y],outputs = cost,updates = [(L1.W, L1.W-learning_rate*gW1),(L1.b, L1.b-learning_rate*gb1),(L2.W, L2.W-learning_rate*gW2),(L2.b, L2.b-learning_rate*gb2)] ) #(L1.W, L1.W-learnging_rate*gW1) #(原始的權重-學習的效率*下降的幅度)更新為L1.W#4.預測結果 #訓練時會給y求cost, 而預測輸入只給出x用來做預測 predict = theano.function(inputs=[x], outputs=L2.outputs)#可視化分析#plot the fake data fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(x_data,y_data) #散點圖效果 #輸入紅線并且連續不斷更新 plt.ion() plt.show() #參數block=True: 只顯示這張圖,關掉圖后才運行后面程序#每隔50步輸出err并繪制擬合曲線 #cost值不斷減小說明預測值與真實值誤差在不斷減小,擬合直線越來越接近 for i in range(1000):#誤差: training 把x_data和y_data放到函數x、y中err = train(x_data, y_data)pre = predict(x_data)#to visualize the result and improvement#查看神經網絡結果和擬合提升過程if i % 50 == 0: #消除紅線, 否則每次的紅線繪制在一起#因為第一步沒有紅線, 故使用try忽略第一步try:ax.lines.remove(lines[0])except Exception:passpredicted_value = predict(x_data)#plot the predictionlines = ax.plot(x_data, predicted_value, 'r-', lw=5) #x和預測y lw線寬度plt.pause(1) #暫定1秒#輸出誤差print(err)#print(pre)基礎性文章,希望對您有所幫助,推薦大家閱讀莫煩大神的學習視頻,也建議大家一步步跟著學習,同時文章中存在錯誤或不足之處,還請海涵~
(By:Eastmount 2018-05-21 下午4點 ? ? http://blog.csdn.net/eastmount/ ? )
總結
以上是生活随笔為你收集整理的[Python人工智能] 二.theano实现回归神经网络分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【python数据挖掘课程】二十三.时间
- 下一篇: [Python人工智能] 三.thean