径向基(RBF)神经网络
生活随笔
收集整理的這篇文章主要介紹了
径向基(RBF)神经网络
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
RBF網絡能夠逼近任意非線性的函數。可以處理系統內難以解析的規律性,具有很好的泛化能力,并且具有較快的學
習速度。當網絡的一個或多個可調參數(權值或閾值)對任何一個輸出都有影響時,這樣的網絡稱為全局逼近網絡。
由于對于每次輸入,網絡上的每一個權值都要調整,從而導致全局逼近網絡的學習速度很慢,比如BP網絡。如果對于
輸入空間的某個局部區域只有少數幾個連接權值影響輸出,則該網絡稱為局部逼近網絡,比如RBF網絡。接下來重點
先介紹RBF網絡的原理,然后給出其實現。先看如下圖
? ?
?正則化的RBF網絡參考這里。下面是網上找的一個比較好的Python的RBF網絡實現。
代碼:
from scipy import * from scipy.linalg import norm, pinvfrom matplotlib import pyplot as pltclass RBF:def __init__(self, indim, numCenters, outdim):self.indim = indimself.outdim = outdimself.numCenters = numCentersself.centers = [random.uniform(-1, 1, indim) for i in xrange(numCenters)]self.beta = 8self.W = random.random((self.numCenters, self.outdim))def _basisfunc(self, c, d):assert len(d) == self.indimreturn exp(-self.beta * norm(c-d)**2)def _calcAct(self, X):# calculate activations of RBFsG = zeros((X.shape[0], self.numCenters), float)for ci, c in enumerate(self.centers):for xi, x in enumerate(X):G[xi,ci] = self._basisfunc(c, x)return Gdef train(self, X, Y):""" X: matrix of dimensions n x indim y: column vector of dimension n x 1 """# choose random center vectors from training setrnd_idx = random.permutation(X.shape[0])[:self.numCenters]self.centers = [X[i,:] for i in rnd_idx]print "center", self.centers# calculate activations of RBFsG = self._calcAct(X)print G# calculate output weights (pseudoinverse)self.W = dot(pinv(G), Y)def test(self, X):""" X: matrix of dimensions n x indim """G = self._calcAct(X)Y = dot(G, self.W)return Yif __name__ == '__main__':n = 100x = mgrid[-1:1:complex(0,n)].reshape(n, 1)# set y and add random noisey = sin(3*(x+0.5)**3 - 1)# y += random.normal(0, 0.1, y.shape)# rbf regressionrbf = RBF(1, 10, 1)rbf.train(x, y)z = rbf.test(x)# plot original dataplt.figure(figsize=(12, 8))plt.plot(x, y, 'k-')# plot learned modelplt.plot(x, z, 'r-', linewidth=2)# plot rbfsplt.plot(rbf.centers, zeros(rbf.numCenters), 'gs')for c in rbf.centers:# RF prediction linescx = arange(c-0.7, c+0.7, 0.01)cy = [rbf._basisfunc(array([cx_]), array([c])) for cx_ in cx]plt.plot(cx, cy, '-', color='gray', linewidth=0.2)plt.xlim(-1.2, 1.2)plt.show()
最后提供Github上的一個C++實現的RBF,供日后參考。
總結
以上是生活随笔為你收集整理的径向基(RBF)神经网络的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三种重要哈希介绍
- 下一篇: SlopOne推荐算法