致敬:https://blog.youkuaiyun.com/Snoopy_Yuan/article/details/71024046
RBF首先是确定神经元中心点ci,对于异或,训练集和验证集构造0 1取值的二维数据,四个0-1点即为中心点。
然后根据103页公式计算w和β,代入径向基函数作为输出
训练10次后错误率已经是0,毕竟RBF是能以任意精度逼近任意连续函数的,异或麻麻地啦
主函数XOR_RBF.py
import numpy as np
# train set
X_trn = np.random.randint(0, 2, (100, 2)) #生成int随机数 >=0且<2
y_trn = np.logical_xor(X_trn[:, 0], X_trn[:, 1])
# test set
X_tst = np.random.randint(0, 2, (100, 2))
y_tst = np.logical_xor(X_tst[:, 0], X_tst[:, 1])
'''
implementation of RBF network
'''
from RBF_BP import *
# 神经元中心ci,因为随机数在0和1取值,随机采样的中心点应该是如下四个
centers = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# construct the network
rbf_nn = RBP_network() # initial a BP network class
rbf_nn.CreateNN(4, centers, learningrate=0.05) # build the network structure
# parameter training
e = []
for i in range(10):
err, err_k = rbf_nn.TrainRBF(X_trn, y_trn)
e.append(err)
'''
model te