您的位置:首页 > 理论基础 > 计算机网络

径向基(RBF)神经网络

2018-04-04 16:33 369 查看
径向基(RBF)神经网络是一种单隐层前馈神经网络,它使用径向基函数作为隐藏层神经元的激活函数,而输出层则是对隐层的神经元的输出的线性组合




上图就是径向基神经网络的结构展示,可以分析出,权值W仅在隐藏层到输出层才存在,这样的神经网络的优势在于:它是一种局部逼近网络,即在空间的某个局部区域上只有少数的几个连接权值会影响其输出,这就比BP神经网络的全局逼近网络具有较大的优势.
RBF神经网络因为具有上述的特点,因此具有较快的学习速度,而且它能够逼近任意的非线性函数,具有较强的泛化能力.
下面说两个关于RBF神经网络特别的地方
1.它的隐藏层的激活函数使用的是径向基函数.
2.在具体实现这个神经网络时,我们主要确定两个过程来训练:
1>确定神经元的中心,常用的方式包括随机采样与聚类,(下面的代码采用的是随机采样的方式进行实现)。
2>利用BP算法等来确定RBF神经网络的权值W与β..(这个值在程序的代码上是beta)
下面附上Python实现RBF神经网络的一个实例,主要用在函数的拟合上from scipy import *
from scipy.linalg import norm, pinv

from matplotlib import pyplot as plt

class RBF:

def __init__(self, indim, numCenters, outdim):
self.indim = indim
self.outdim = outdim
self.numCenters = numCenters
#这里我们初始化RBF的隐含神经元所对应的中心
self.centers = [random.uniform(-1, 1, indim) for i in range(numCenters)]
#这里我们是定义RBF网络的两个重要的参数..
#第一个参数代表β,第二个表示的是连接权值
self.beta = 8
self.W = random.random((self.numCenters, self.outdim))

def _basisfunc(self, c, d):
assert len(d) == self.indim
return exp(-self.beta * norm(c-d)**2)

def _calcAct(self, X):
# calculate activations of RBFs
G = 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 G
#将x,y值传入进行训练
def 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 set
rnd_idx = random.permutation(X.shape[0])[:self.numCenters]
self.centers = [X[i,:] for i in rnd_idx]
#运行的中心为:[76 21 58 61 2 1 64 77 34 33]
print ("center", self.centers)
# calculate activations of RBFs
G = 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 Y

if __name__ == '__main__':
n = 100
x = mgrid[-1:1:complex(0,n)].reshape(n, 1)
#print(x)
# set y and add random noise
y = sin(3*(x+0.5)**3 - 1)
y += random.normal(0, 0.1, y.shape)
# rbf regression
rbf = RBF(1, 10, 1)
rbf.train(x, y)
z = rbf.test(x)

# plot original data
plt.figure(figsize=(12, 8))
plt.plot(x, y, 'k-')

# plot learned model
plt.plot(x, z, 'r-', linewidth=2)
plt.show()



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: