class WNN(object):
def __init__(self, eta=0.008, epoch_max=5000, Ni=1, Nh=40, Ns=1,checkV=0):
### Inijalizacja parametrów
self.eta = eta
self.epoch_max = epoch_max
self.Ni = Ni
self.Nh = Nh
self.Ns = Ns
self.Aini = 0.01
if(eta<=checkV):
messagebox.showerror(title="Wrong value!",message="You choose the wrong learning rate!Must be more then 0!")
if(epoch_max<=checkV):
messagebox.showerror(title="Wrong value!",message="You choose the wrong epochs!Must be more then 0!")
if(Nh<=checkV):
messagebox.showerror(title="Wrong value!",message="You choose the wrong hidden neurons!Must be more then 0!")
def load_first_function(self,d,check):
self.d=d
x = np.arange(-6, 6, 0.15)##
self.N = x.shape[0]
xmax = np.max(x)
self.X_train = x / xmax
if(check=="2*(1 / (1 + exp(-x)))**3 - 3*(1 / (1 + exp(-x)))**2 + (1 / (1 + exp(-x)))"):
self.d=2*(1 / (1 + np.exp(-x)))**3 - 3*(1 / (1 + np.exp(-x)))**2 + (1 / (1 + np.exp(-x)))#done
elif(check=="1 / (1 + exp(-1 * x))*(cos(x) -sin(x))"):
self.d=1 / (1 + np.exp(-1 * x))*(np.cos(x) - np.sin(x))#done
elif(check=="sin(x)"):
self.d=np.sin(x)#done
elif(check=="cos(x)"):
self.d=np.cos(x)#done epochs=6000 or more
elif(check=="cos(x)*sin(x)"):
self.d=np.cos(x)*np.sin(x)#done epochs=6000 or more
elif(check=="1 / (1 + exp(-1 * x))"):
self.d=1 / (1 + np.exp(-1 * x))#done
elif(check=="cos(x) - sin(x)"):
self.d=(np.cos(x) - np.sin(x))#done
def sig_dev2(self, theta):
return 2*(1 / (1 + np.exp(-theta)))**3 - 3*(1 / (1 + np.exp(-theta)))**2 + (1 / (1 + np.exp(-theta)))
def sig_dev3(self, theta):
return -6*(1 / (1 + np.exp(-theta)))**4 + 12*(1 / (1 + np.exp(-theta)))**3 - 7*(1 / (1 + np.exp(-theta)))**2 + (1 / (1 + np.exp(-theta)))
def train(self):
### Inicjalizacja wag
self.A = np.random.rand(self.Ns, self.Nh) * self.Aini
### Inicjalizacja centrów
self.t = np.zeros((1, self.Nh))
idx = np.random.permutation(self.Nh)
for j in range(self.Nh):
self.t[0,j] = self.d[idx[j]]
### Szerokość wczytywania
self.R = abs(np.max(self.t) - np.min(self.t)) / 2
MSE = np.zeros(self.epoch_max)
plt.ion()
for epoca in range(self.epoch_max):
z = np.zeros(self.N)
E = np.zeros(self.N)
index = np.random.permutation(self.N)
for i in index:
xi = self.X_train[i]#np.array([self.X_train[i]]).reshape(1, -1)
theta = (xi - self.t) / self.R
yj = self.sig_dev2(theta)
z[i] = np.dot(self.A, yj.T)[0][0]
e = self.d[i] - z[i]
self.A = self.A + (self.eta * e * yj)
self.t = self.t - (self.eta * e * self.A / self.R * self.sig_dev3(theta))
self.R = self.R - (((self.eta * e * self.A * (xi - self.t)) / self.R**2) * self.sig_dev3(theta))
E[i] = 0.5 * e**2
MSE[epoca] = np.sum(E) / self.N
if (epoca % 200 == 0 or epoca == self.epoch_max - 1):
if (epoca != 0):
plt.cla()
plt.clf()
self.plot(z, epoca) `введите сюда код`