感知器學習算法步驟以下: |
圖1-10 感知器的分類例子算法 |
2.輸入同樣本X=(X1 ,X2 ,…,Xn+1 )以及它的指望輸出d。函數 指望輸出值d在樣本的類屬不一樣時取值不一樣。若是x是A類,則取d=1,若是x是B類,則取-1。指望輸出d也便是教師信號。學習 3.計算實際輸出值Y測試 4.根據實際輸出求偏差espa e=d-Y(t) (1-21)code 5.用偏差e去修改權係數blog i=1,2,…,n,n+1 (1-22)ip 其中,η稱爲權重變化率,0<η≤1ci |
# -*- coding: cp936 -*- import numpy import pylab import sys class neuralNetwork: b = 1 learnRaito = 0.5 trainData = numpy.array([[b,1,3],[b,2,3],[b,1,8],[b,2,15],[b,3,7],[b,4,29],[b,4,8],[b,4,20]]) #訓練數據 能夠訓練不一樣的方程 模型 trainResult = numpy.array([1,1,-1,-1,1,-1,1,-1]) weight = numpy.array([b,0,0]) error = 0.001 def Out(self,v): """求值的取向""" if v>=0: return 1 else: return -1 def exceptSignal(self,oldw,inx): #a bug here #print '-'*20 #print oldw #print inx #print numpy.dot(oldw.T,inx) #print '+'*20 #return 1 ans = numpy.dot(oldw.T,inx) return self.Out(ans) def trainOnce(self,oldw,inx,correctResult): """one training""" error = correctResult - self.exceptSignal(oldw,inx) newWeight = oldw + self.learnRaito*error*inx self.weight = newWeight return error def getAbs(self,x): if x<0: return -x else: return x def trainWeight(self): """traing the weight of data""" error = 1 while error > self.error: i = 0 error = 0 for inx in self.trainData: error += self.getAbs(self.trainOnce(self.weight,inx,self.trainResult[i])) i = i+1 def drawTrainResult(self): """ draw graph of Result""" xor = self.trainData[:,1]#切片,獲取第一列,x座標 yor = self.trainData[:,2]#切片,獲取第二列,y座標 pylab.subplot(111) xMax = numpy.max(xor)+15 xMin = numpy.min(xor)-5 yMax = numpy.max(yor)+50 yMin = numpy.min(yor)-5 pylab.xlabel(u'xor') pylab.ylabel(u'yor') pylab.xlim(xMin,xMax) pylab.ylim(yMin,yMax) #draw point for i in range(0,len(self.trainResult)): if self.trainResult[i] == 1: pylab.plot(xor[i],yor[i],'r*') else: pylab.plot(xor[i],yor[i],'ro') def drawTestResult(self,data): test = data#numpy.array(data) if self.exceptSignal(self.weight,test)>0: pylab.plot(test[1],test[2],'b*') else: pylab.plot(test[1],test[2],'bo') def drawTrueLine(self): """真實函數分界線""" xtest = numpy.array(range(0,20)) ytest = xtest*2+1.68 pylab.plot(xtest,ytest,'g--') def showGraph(self): pylab.show() testData = [[1,5,11],[1,5,12],[1,4,16],[1,6,7],[1,3,12],[1,2,22]] neural = neuralNetwork() print neural.Out(124.32423) neural.trainWeight() neural.drawTrainResult() neural.drawTrueLine() #neural.showGraph() for test in testData: neural.drawTestResult(test) print neural.weight neural.showGraph()
紅色是訓練數據,藍色是測試數據,圓點表明是在線上方,*表明在線下方,由圖可知這個算法還不錯