点击查看完整代码http://www.daimapi.com/neuralnetwork2_1/
径向基神经网络(Radial Basis Function Network),简写为RBF,该代码不使用任何第三方深度学习工具包实现,利用Python3实现。
# -*- coding: utf-8 -*-
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
self.centers = [random.uniform(-1, 1, indim) for i in range(numCenters)]
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):
# 计算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
def train(self, X, Y):
""" X: n x indim维的矩阵
y: n x 1维的列向量"""
# 从训练集随机选择中心向量
rnd_idx = random.permutation(X.shape[0])[:self.numCenters]
self.centers = [X[i,:] for i in rnd_idx]
print("center", self.centers)
# 计算RBFs的激活函数值
G = self._calcAct(X)
print(G)
# 计算输出层的权值
self.W = dot(pinv(G), Y)
def test(self, X):
""" X: n x indim维的矩阵 """
G = self._calcAct(X)
Y = dot(G, self.W)
return Y