我正在尝试实现RBF网络以估计square(2x)的功能。不幸的是,我无法达到分配要求所要求的错误0.01以下,这使我相信自己已经错过了一些基本知识。对于我的训练集,我使用(0,2 * pi)的间隔,并且从(0.05,2 * pi)生成测试集。我目前在这两个上都没有错误,但是我仍然认为我得到了一个相当大的错误。对于rbf网络,我为每个节点分配了(2 * pi / nr_of_nodes)。有人可以向我提供一些有关我的实现缺少的信息吗?
import numpy as np
from scipy import signal
from numpy.linalg import pinv, norm
import matplotlib.pyplot as plt
#global variables
mean_error = 0
mean_variance = 0.1
nr_nodes = 6
mean = np.linspace(0, 2*np.pi, num = nr_nodes)
mean = mean.reshape((nr_nodes, 1))
eta = 0.00009
epochs = 500
alpha = 0.3
def reshuffle(X, t):
permute = np.random.permutation(X.shape[0])
X = X[permute,:]
t = t[permute,:]
return X, t
def generate_data():
x = np.arange(0, 2*np.pi, 0.1)
x = x.reshape((x.shape[0], 1))
x_test = np.arange(0.05, 2 * np.pi, 0.1)
x_test = x_test.reshape((x_test.shape[0], 1))
#generating training set
x_sine_train = np.sin(x) #+ np.random.normal(mean_error, mean_variance, (x.shape[0], 1))
x_square_train = signal.square(2*x) #+ np.random.normal(mean_error, mean_variance, (x.shape[0], 1))
#generating test set
x_sine_test = np.sin(x_test) #+ np.random.normal(mean_error, mean_variance, (x_test.shape[0], 1))
x_square_test = signal.square(2*x_test) #+ np.random.normal(mean_error, mean_variance, (x_test.shape[0], 1))
return x, x_test, x_sine_train, x_square_train, x_sine_test, x_square_test
x, x_test, x_sine_train, x_square_train, x_sine_test, x_square_test = generate_data()
def rbf(x):
variance = 1.0 * np.pi/(nr_nodes)
phi = np.exp(-np.subtract(x, mean.T)**2 / (2*variance)**2)
return phi
#print(np.amin(rbf(x)))
"""def rbf(x):
variance = 2 * np.pi / (nr_nodes)
random_numbers = []
for i in range(nr_nodes):
rand_int = np.random.randint(0, 629) / 100
random_numbers.append(rand_int)
random_numbers = np.array(random_numbers)
random_numbers.reshape(nr_nodes, 1)
phi = np.exp(-np.subtract(x, random_numbers.T) ** 2 / (2 * variance) ** 2)
return phi"""
def update_weights():
# generate needed variables
x, x_test, x_sine_train, x_square_train, x_sine_test, x_square_test = generate_data()
#for i in range(epochs):
w = np.dot(pinv(np.dot(rbf(x).T, rbf(x))), np.dot(rbf(x).T, x_square_train))
#w = np.linalg.lstsq(rbf(x), x_square_train, rcond=None)[0]
f = np.dot(rbf(x_test), w)
tot_error = np.sum((np.abs(f - x_square_test)))/62
return f, tot_error
f, tot_error = update_weights()
print(tot_error)