Pybrain是一個比較有名的Python神經網路程式庫,今天我用它做了一個實驗,參考了這篇部落格,感謝原作者,給出了具體的實現,代碼可以直接拷貝運行。
我們的問題主要如下:
首先我們給出構造產生這個題目要求的資料集的函數
def generate_data(): """generate original data of u and y""" u = np.random.uniform(-1,1,200) y=[] former_y_value = 0 for i in np.arange(0,200): y.append(former_y_value) next_y_value = (29 / 40) * np.sin( (16 * u[i] + 8 * former_y_value) / (3 + 4 * (u[i] ** 2) + 4 * (former_y_value ** 2))) \ + (2 / 10) * u[i] + (2 / 10) * former_y_value former_y_value = next_y_value return u,y
這個題目的函數畫出來是這樣子的:
我們的例子,就是用前100個點訓練,後100個點作為預測。 構建Pybrain神經網路的基本步驟: 構建神經網路 構造資料集 訓練神經網路 結果可視化 驗證和分析 構造神經網路
構建神經網路的過程非常清晰,設定幾個層次,幾個節點,都簡單明了,看一次就會了。
import numpy as npimport matplotlib.pyplot as pltfrom pybrain.structure import *from pybrain.datasets import SupervisedDataSetfrom pybrain.supervised.trainers import BackpropTrainer# createa neural networkfnn = FeedForwardNetwork()# create three layers, input layer:2 input unit; hidden layer: 10 units; output layer: 1 outputinLayer = LinearLayer(2, name='inLayer')hiddenLayer0 = SigmoidLayer(10, name='hiddenLayer0')outLayer = LinearLayer(1, name='outLayer')# add three layers to the neural networkfnn.addInputModule(inLayer)fnn.addModule(hiddenLayer0)fnn.addOutputModule(outLayer)# link three layersin_to_hidden0 = FullConnection(inLayer,hiddenLayer0)hidden0_to_out = FullConnection(hiddenLayer0, outLayer)# add the links to neural networkfnn.addConnection(in_to_hidden0)fnn.addConnection(hidden0_to_out)# make neural network come into effectfnn.sortModules()
構建資料集
我們選擇2輸入1輸出,80%用於訓練,20%用於預測
# definite the dataset as two input , one outputDS = SupervisedDataSet(2,1)# add data element to the datasetfor i in np.arange(199): DS.addSample([u[i],y[i]],[y[i+1]])# you can get your input/output this wayX = DS['input']Y = DS['target']# split the dataset into train dataset and test datasetdataTrain, dataTest = DS.splitWithProportion(0.8)xTrain, yTrain = dataTrain['input'],dataTrain['target']xTest, yTest = dataTest['input'], dataTest['target']
訓練神經網路
我們暫且讓他迭代1000次
# train the NN# we use BP Algorithm# verbose = True means print th total errortrainer = BackpropTrainer(fnn, dataTrain, verbose=True,learningrate=0.01)# set the epoch times to make the NN fittrainer.trainUntilConvergence(maxEpochs=1000)
結果可視化
我們用matlibplot畫出來這個預測值和實際值
predict_resutl=[]for i in np.arange(len(xTest)): predict_resutl.append(fnn.activate(xTest[i])[0])print(predict_resutl)plt.figure()plt.plot(np.arange(0,len(xTest)), predict_resutl, 'ro--', label='predict number')plt.plot(np.arange(0,len(xTest)), yTest, 'ko-', label='true number')plt.legend()plt.xlabel("x")plt.ylabel("y")plt.show()
我們拿這個題目來做一下預測,畫出來的圖形如下
分析
for mod in fnn.modules: print ("Module:", mod.name) if mod.paramdim > 0: print ("--parameters:", mod.params) for conn in fnn.connections[mod]: print ("-connection to", conn.outmod.name) if conn.paramdim > 0: print ("- parameters", conn.params) if hasattr(fnn, "recurrentConns"): print ("Recurrent connections") for conn in fnn.recurrentConns: print ("-", conn.inmod.name, " to", conn.outmod.name) if conn.paramdim > 0: print ("- parameters", conn.params)
它可以列印出來神經網路的具體資訊,結果如下:
Module: hiddenLayer0-connection to outLayer- parameters [-0.48485978 1.94439991 -1.1686299 -1.01764515 -1.04221 -0.78088745 0.27321985 -1.76426041 2.0747614 1.98425053]Module: inLayer-connection to hiddenLayer0- parameters [ 1.48125364 -0.97942827 4.7258546 2.08059918 -1.96960441 -0.03098871 0.52430318 1.64983933 0.43738152 1.95122015 0.81952423 -0.24019787 -0.86026329 0.63505556 0.53870484 0.94078527 1.42263437 1.87720358 -1.12582038 0.70344489]Module: outLayer
完整的代碼
最後,我把完整的代碼貼出來,注意,你要先安裝pybrain才行
import numpy as npimport matplotlib.pyplot as pltfrom pybrain.structure import *from pybrain.datasets import SupervisedDataSetfrom pybrain.supervised.trainers import BackpropTrainerdef generate_data(): """generate original data of u and y""" u = np.random.uniform(-1,1,200) y=[] former_y_value = 0 for i in np.arange(0,200): y.append(former_y_value) next_y_value = (29 / 40) * np.sin( (16 * u[i] + 8 * former_y_value) / (3 + 4 * (u[i] ** 2) + 4 * (former_y_value ** 2))) \ + (2 / 10) * u[i] + (2 / 10) * former_y_value former_y_value = next_y_value return u,y# obtain the original datau,y = generate_data()# createa neural networkfnn = FeedForwardNetwork()# create three layers, input layer:2 input unit; hidden layer: 10 units; output layer: 1 outputinLayer = LinearLayer(2, name='inLayer')hiddenLayer0 = SigmoidLayer(10, name='hiddenLayer0')outLayer = LinearLayer(1, name='outLayer')# add three layers to the neural networkfnn.addInputModule(inLayer)fnn.addModule(hiddenLayer0)fnn.addOutputModule(outLayer)# link three layersin_to_hidden0 = FullConnection(inLayer,hiddenLayer0)hidden0_to_out = FullConnection(hiddenLayer0, outLayer)# add the links to neural networkfnn.addConnection(in_to_hidden0)fnn.addConnection(hidden0_to_out)# make neural network come into effectfnn.sortModules()# definite the dataset as two input , one outputDS = SupervisedDataSet(2,1)# add data element to the datasetfor i in np.arange(199): DS.addSample([u[i],y[i]],[y[i+1]])# you can get your input/output this wayX = DS['input']Y = DS['target']# split the dataset into train dataset and test datasetdataTrain, dataTest = DS.splitWithProportion(0.8)xTrain, yTrain = dataTrain['input'],dataTrain['target']xTest, yTest = dataTest['input'], dataTest['target']# train the NN# we use BP Algorithm# verbose = True means print th total errortrainer = BackpropTrainer(fnn, dataTrain, verbose=True,learningrate=0.01)# set the epoch times to make the NN fittrainer.trainUntilConvergence(maxEpochs=1000)# prediction = fnn.activate(xTest[1])# print("the prediction number is :",prediction," the real number is: ",yTest[1])predict_resutl=[]for i in np.arange(len(xTest)): predict_resutl.append(fnn.activate(xTest[i])[0])print(predict_resutl)plt.figure()plt.plot(np.arange(0,len(xTest)), predict_resutl, 'ro--', label='predict number')plt.plot(np.arange(0,len(xTest)), yTest, 'ko-', label='true number')plt.legend()plt.xlabel("x")plt.ylabel("y")plt.show()for mod in fnn.modules: print ("Module:", mod.name) if mod.paramdim > 0: print ("--parameters:", mod.params) for conn in fnn.connections[mod]: print ("-connection to", conn.outmod.name) if conn.paramdim > 0: print ("- parameters", conn.params) if hasattr(fnn, "recurrentConns"): print ("Recurrent connections") for conn in fnn.recurrentConns: print ("-", conn.inmod.name, " to", conn.outmod.name) if conn.paramdim > 0: print ("- parameters", conn.params)
文章引用:
[1]用Pybrain庫進行神經網路擬合