开发者社区> 罗兵> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

径向基(RBF)神经网络python实现

简介: 1 from numpy import array, append, vstack, transpose, reshape, \ 2 dot, true_divide, mean, exp, sqrt, log, \ 3 ...
+关注继续查看

 

  1 from numpy import array, append, vstack, transpose, reshape, \
  2                   dot, true_divide, mean, exp, sqrt, log, \
  3                   loadtxt, savetxt, zeros, frombuffer
  4 from numpy.linalg import norm, lstsq
  5 from multiprocessing import Process, Array
  6 from random import sample
  7 from time import time
  8 from sys import stdout
  9 from ctypes import c_double
 10 from h5py import File
 11 
 12 
 13 def metrics(a, b): 
 14     return norm(a - b)
 15 
 16 
 17 def gaussian (x, mu, sigma): 
 18     return exp(- metrics(mu, x)**2 / (2 * sigma**2))
 19 
 20 
 21 def multiQuadric (x, mu, sigma):
 22     return pow(metrics(mu,x)**2 + sigma**2, 0.5)
 23 
 24 
 25 def invMultiQuadric (x, mu, sigma):
 26     return pow(metrics(mu,x)**2 + sigma**2, -0.5)
 27 
 28 
 29 def plateSpine (x,mu):
 30     r = metrics(mu,x)
 31     return (r**2) * log(r)
 32 
 33 
 34 class Rbf:
 35     def __init__(self, prefix = 'rbf', workers = 4, extra_neurons = 0, from_files = None):
 36         self.prefix = prefix
 37         self.workers = workers
 38         self.extra_neurons = extra_neurons
 39 
 40         # Import partial model
 41         if from_files is not None:            
 42             w_handle = self.w_handle = File(from_files['w'], 'r')
 43             mu_handle = self.mu_handle = File(from_files['mu'], 'r')
 44             sigma_handle = self.sigma_handle = File(from_files['sigma'], 'r')
 45             
 46             self.w = w_handle['w']
 47             self.mu = mu_handle['mu']
 48             self.sigmas = sigma_handle['sigmas']
 49             
 50             self.neurons = self.sigmas.shape[0]
 51 
 52     def _calculate_error(self, y):
 53         self.error = mean(abs(self.os - y))
 54         self.relative_error = true_divide(self.error, mean(y))
 55 
 56     def _generate_mu(self, x):
 57         n = self.n
 58         extra_neurons = self.extra_neurons
 59 
 60         # TODO: Make reusable
 61         mu_clusters = loadtxt('clusters100.txt', delimiter='\t')
 62 
 63         mu_indices = sample(range(n), extra_neurons)
 64         mu_new = x[mu_indices, :]
 65         mu = vstack((mu_clusters, mu_new))
 66 
 67         return mu
 68 
 69     def _calculate_sigmas(self):
 70         neurons = self.neurons
 71         mu = self.mu
 72 
 73         sigmas = zeros((neurons, ))
 74         for i in xrange(neurons):
 75             dists = [0 for _ in xrange(neurons)]
 76             for j in xrange(neurons):
 77                 if i != j:
 78                     dists[j] = metrics(mu[i], mu[j])
 79             sigmas[i] = mean(dists)* 2
 80                       # max(dists) / sqrt(neurons * 2))
 81         return sigmas
 82 
 83     def _calculate_phi(self, x):
 84         C = self.workers
 85         neurons = self.neurons
 86         mu = self.mu
 87         sigmas = self.sigmas
 88         phi = self.phi = None
 89         n = self.n
 90 
 91 
 92         def heavy_lifting(c, phi):
 93             s = jobs[c][1] - jobs[c][0]
 94             for k, i in enumerate(xrange(jobs[c][0], jobs[c][1])):
 95                 for j in xrange(neurons):
 96                     # phi[i, j] = metrics(x[i,:], mu[j])**3)
 97                     # phi[i, j] = plateSpine(x[i,:], mu[j]))
 98                     # phi[i, j] = invMultiQuadric(x[i,:], mu[j], sigmas[j]))
 99                     phi[i, j] = multiQuadric(x[i,:], mu[j], sigmas[j])
100                     # phi[i, j] = gaussian(x[i,:], mu[j], sigmas[j]))
101                 if k % 1000 == 0:
102                     percent = true_divide(k, s)*100
103                     print(c, ': {:2.2f}%'.format(percent))
104             print(c, ': Done')
105         
106         # distributing the work between 4 workers
107         shared_array = Array(c_double, n * neurons)
108         phi = frombuffer(shared_array.get_obj())
109         phi = phi.reshape((n, neurons))
110 
111         jobs = []
112         workers = []
113 
114         p = n / C
115         m = n % C
116         for c in range(C):
117             jobs.append((c*p, (c+1)*p + (m if c == C-1 else 0)))
118             worker = Process(target = heavy_lifting, args = (c, phi))
119             workers.append(worker)
120             worker.start()
121 
122         for worker in workers:
123             worker.join()
124 
125         return phi
126 
127     def _do_algebra(self, y):
128         phi = self.phi
129 
130         w = lstsq(phi, y)[0]
131         os = dot(w, transpose(phi))
132         return w, os
133         # Saving to HDF5
134         os_h5 = os_handle.create_dataset('os', data = os)
135 
136     def train(self, x, y):
137         self.n = x.shape[0]
138 
139         ## Initialize HDF5 caches
140         prefix = self.prefix
141         postfix = str(self.n) + '-' + str(self.extra_neurons) + '.hdf5'
142         name_template = prefix + '-{}-' + postfix
143         phi_handle = self.phi_handle = File(name_template.format('phi'), 'w')
144         os_handle = self.w_handle = File(name_template.format('os'), 'w')
145         w_handle = self.w_handle = File(name_template.format('w'), 'w')
146         mu_handle = self.mu_handle = File(name_template.format('mu'), 'w')
147         sigma_handle = self.sigma_handle = File(name_template.format('sigma'), 'w')
148 
149         ## Mu generation
150         mu = self.mu = self._generate_mu(x)
151         self.neurons = mu.shape[0]
152         print('({} neurons)'.format(self.neurons))
153         # Save to HDF5
154         mu_h5 = mu_handle.create_dataset('mu', data = mu)
155 
156         ## Sigma calculation
157         print('Calculating Sigma...')
158         sigmas = self.sigmas = self._calculate_sigmas()
159         # Save to HDF5
160         sigmas_h5 = sigma_handle.create_dataset('sigmas', data = sigmas)
161         print('Done')
162 
163         ## Phi calculation
164         print('Calculating Phi...')
165         phi = self.phi = self._calculate_phi(x)
166         print('Done')
167         # Saving to HDF5
168         print('Serializing...')
169         phi_h5 = phi_handle.create_dataset('phi', data = phi)
170         del phi
171         self.phi = phi_h5
172         print('Done')
173 
174         ## Algebra
175         print('Doing final algebra...')
176         w, os = self.w, _ = self._do_algebra(y)
177         # Saving to HDF5
178         w_h5 = w_handle.create_dataset('w', data = w)
179         os_h5 = os_handle.create_dataset('os', data = os)
180 
181         ## Calculate error
182         self._calculate_error(y)
183         print('Done')
184 
185     def predict(self, test_data):
186         mu = self.mu = self.mu.value
187         sigmas = self.sigmas = self.sigmas.value
188         w = self.w = self.w.value
189 
190         print('Calculating phi for test data...')
191         phi = self._calculate_phi(test_data)
192         os = dot(w, transpose(phi))
193         savetxt('iok3834.txt', os, delimiter='\n')
194         return os
195 
196     @property
197     def summary(self):
198         return '\n'.join( \
199             ['-----------------',
200             'Training set size: {}'.format(self.n),
201             'Hidden layer size: {}'.format(self.neurons),
202             '-----------------',
203             'Absolute error   : {:02.2f}'.format(self.error),
204             'Relative error   : {:02.2f}%'.format(self.relative_error * 100)])
205 
206 
207 def predict(test_data):
208     mu = File('rbf-mu-212243-2400.hdf5', 'r')['mu'].value
209     sigmas = File('rbf-sigma-212243-2400.hdf5', 'r')['sigmas'].value
210     w = File('rbf-w-212243-2400.hdf5', 'r')['w'].value
211 
212     n = test_data.shape[0]  
213     neur = mu.shape[0]  
214     
215     mu = transpose(mu)
216     mu.reshape((n, neur))   
217 
218     phi = zeros((n, neur)) 
219     for i in range(n):
220         for j in range(neur):
221             phi[i, j] = multiQuadric(test_data[i,:], mu[j], sigmas[j])
222 
223     os = dot(w, transpose(phi))
224     savetxt('iok3834.txt', os, delimiter='\n')
225     return os

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
三层BP神经网络的python实现
这是一个非常漂亮的三层反向传播神经网络的python实现,下一步我准备试着将其修改为多层BP神经网络。   下面是运行演示函数的截图,你会发现预测的结果很惊人!         提示:运行演示函数的时候,可以尝试改变隐藏层的节点数,看节点数增加了,预测的精度会否提升   ...
1138 0
图解爬虫,用几个最简单的例子带你入门Python爬虫
爬虫一直是Python的一大应用场景,差不多每门语言都可以写爬虫,但是程序员们却独爱Python。之所以偏爱Python就是因为她简洁的语法,我们使用Python可以很简单的写出一个爬虫程序。本篇博客将以Python语言,用几个非常简单的例子带大家入门Python爬虫。
62 0
python爬虫实战实现XPath和lxml | python爬虫实战之六
本节介绍了用XPath类似于路径的设计遍历或者查询其中的某些元素或者元素的属性。
1064 0
如何开始写你的第一个python脚本——简单爬虫入门!
好多朋友在入门python的时候都是以爬虫入手,而网络爬虫是近几年比较流行的概念,特别是在大数据分析热门起来以后,学习网络爬虫的人越来越多,哦对,现在叫数据挖掘了! 其实,一般的爬虫具有2个功能:取数据和存数据!好像说了句废话。
1432 0
Python爬虫入门一之综述
大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验。 Python版本:2.7,Python 3请另寻其他博文。 首先爬虫是什么? 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动的抓
3172 0
+关注
罗兵
数学专业。擅数据分析,涉stock、lotto。了解随机过程分析、神经网络。涉web前端、后端。了解vba、js,稍擅python
251
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载