Python实现的径向基(RBF)神经网络示例
本文实例讲述了Python实现的径向基(RBF)神经网络。分享给大家供大家参考,具体如下:
fromnumpyimportarray,append,vstack,transpose,reshape,\ dot,true_divide,mean,exp,sqrt,log,\ loadtxt,savetxt,zeros,frombuffer fromnumpy.linalgimportnorm,lstsq frommultiprocessingimportProcess,Array fromrandomimportsample fromtimeimporttime fromsysimportstdout fromctypesimportc_double fromh5pyimportFile defmetrics(a,b): returnnorm(a-b) defgaussian(x,mu,sigma): returnexp(-metrics(mu,x)**2/(2*sigma**2)) defmultiQuadric(x,mu,sigma): returnpow(metrics(mu,x)**2+sigma**2,0.5) definvMultiQuadric(x,mu,sigma): returnpow(metrics(mu,x)**2+sigma**2,-0.5) defplateSpine(x,mu): r=metrics(mu,x) return(r**2)*log(r) classRbf: def__init__(self,prefix='rbf',workers=4,extra_neurons=0,from_files=None): self.prefix=prefix self.workers=workers self.extra_neurons=extra_neurons #Importpartialmodel iffrom_filesisnotNone: w_handle=self.w_handle=File(from_files['w'],'r') mu_handle=self.mu_handle=File(from_files['mu'],'r') sigma_handle=self.sigma_handle=File(from_files['sigma'],'r') self.w=w_handle['w'] self.mu=mu_handle['mu'] self.sigmas=sigma_handle['sigmas'] self.neurons=self.sigmas.shape[0] def_calculate_error(self,y): self.error=mean(abs(self.os-y)) self.relative_error=true_divide(self.error,mean(y)) def_generate_mu(self,x): n=self.n extra_neurons=self.extra_neurons #TODO:Makereusable mu_clusters=loadtxt('clusters100.txt',delimiter='\t') mu_indices=sample(range(n),extra_neurons) mu_new=x[mu_indices,:] mu=vstack((mu_clusters,mu_new)) returnmu def_calculate_sigmas(self): neurons=self.neurons mu=self.mu sigmas=zeros((neurons,)) foriinxrange(neurons): dists=[0for_inxrange(neurons)] forjinxrange(neurons): ifi!=j: dists[j]=metrics(mu[i],mu[j]) sigmas[i]=mean(dists)*2 #max(dists)/sqrt(neurons*2)) returnsigmas def_calculate_phi(self,x): C=self.workers neurons=self.neurons mu=self.mu sigmas=self.sigmas phi=self.phi=None n=self.n defheavy_lifting(c,phi): s=jobs[c][1]-jobs[c][0] fork,iinenumerate(xrange(jobs[c][0],jobs[c][1])): forjinxrange(neurons): #phi[i,j]=metrics(x[i,:],mu[j])**3) #phi[i,j]=plateSpine(x[i,:],mu[j])) #phi[i,j]=invMultiQuadric(x[i,:],mu[j],sigmas[j])) phi[i,j]=multiQuadric(x[i,:],mu[j],sigmas[j]) #phi[i,j]=gaussian(x[i,:],mu[j],sigmas[j])) ifk%1000==0: percent=true_divide(k,s)*100 print(c,':{:2.2f}%'.format(percent)) print(c,':Done') #distributingtheworkbetween4workers shared_array=Array(c_double,n*neurons) phi=frombuffer(shared_array.get_obj()) phi=phi.reshape((n,neurons)) jobs=[] workers=[] p=n/C m=n%C forcinrange(C): jobs.append((c*p,(c+1)*p+(mifc==C-1else0))) worker=Process(target=heavy_lifting,args=(c,phi)) workers.append(worker) worker.start() forworkerinworkers: worker.join() returnphi def_do_algebra(self,y): phi=self.phi w=lstsq(phi,y)[0] os=dot(w,transpose(phi)) returnw,os #SavingtoHDF5 os_h5=os_handle.create_dataset('os',data=os) deftrain(self,x,y): self.n=x.shape[0] ##InitializeHDF5caches prefix=self.prefix postfix=str(self.n)+'-'+str(self.extra_neurons)+'.hdf5' name_template=prefix+'-{}-'+postfix phi_handle=self.phi_handle=File(name_template.format('phi'),'w') os_handle=self.w_handle=File(name_template.format('os'),'w') w_handle=self.w_handle=File(name_template.format('w'),'w') mu_handle=self.mu_handle=File(name_template.format('mu'),'w') sigma_handle=self.sigma_handle=File(name_template.format('sigma'),'w') ##Mugeneration mu=self.mu=self._generate_mu(x) self.neurons=mu.shape[0] print('({}neurons)'.format(self.neurons)) #SavetoHDF5 mu_h5=mu_handle.create_dataset('mu',data=mu) ##Sigmacalculation print('CalculatingSigma...') sigmas=self.sigmas=self._calculate_sigmas() #SavetoHDF5 sigmas_h5=sigma_handle.create_dataset('sigmas',data=sigmas) print('Done') ##Phicalculation print('CalculatingPhi...') phi=self.phi=self._calculate_phi(x) print('Done') #SavingtoHDF5 print('Serializing...') phi_h5=phi_handle.create_dataset('phi',data=phi) delphi self.phi=phi_h5 print('Done') ##Algebra print('Doingfinalalgebra...') w,os=self.w,_=self._do_algebra(y) #SavingtoHDF5 w_h5=w_handle.create_dataset('w',data=w) os_h5=os_handle.create_dataset('os',data=os) ##Calculateerror self._calculate_error(y) print('Done') defpredict(self,test_data): mu=self.mu=self.mu.value sigmas=self.sigmas=self.sigmas.value w=self.w=self.w.value print('Calculatingphifortestdata...') phi=self._calculate_phi(test_data) os=dot(w,transpose(phi)) savetxt('iok3834.txt',os,delimiter='\n') returnos @property defsummary(self): return'\n'.join(\ ['-----------------', 'Trainingsetsize:{}'.format(self.n), 'Hiddenlayersize:{}'.format(self.neurons), '-----------------', 'Absoluteerror:{:02.2f}'.format(self.error), 'Relativeerror:{:02.2f}%'.format(self.relative_error*100)]) defpredict(test_data): mu=File('rbf-mu-212243-2400.hdf5','r')['mu'].value sigmas=File('rbf-sigma-212243-2400.hdf5','r')['sigmas'].value w=File('rbf-w-212243-2400.hdf5','r')['w'].value n=test_data.shape[0] neur=mu.shape[0] mu=transpose(mu) mu.reshape((n,neur)) phi=zeros((n,neur)) foriinrange(n): forjinrange(neur): phi[i,j]=multiQuadric(test_data[i,:],mu[j],sigmas[j]) os=dot(w,transpose(phi)) savetxt('iok3834.txt',os,delimiter='\n') returnos
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。