Python实现的径向基(RBF)神经网络示例


Posted in Python onFebruary 06, 2018

本文实例讲述了Python实现的径向基(RBF)神经网络。分享给大家供大家参考,具体如下:

from numpy import array, append, vstack, transpose, reshape, \
         dot, true_divide, mean, exp, sqrt, log, \
         loadtxt, savetxt, zeros, frombuffer
from numpy.linalg import norm, lstsq
from multiprocessing import Process, Array
from random import sample
from time import time
from sys import stdout
from ctypes import c_double
from h5py import File
def metrics(a, b):
  return norm(a - b)
def gaussian (x, mu, sigma):
  return exp(- metrics(mu, x)**2 / (2 * sigma**2))
def multiQuadric (x, mu, sigma):
  return pow(metrics(mu,x)**2 + sigma**2, 0.5)
def invMultiQuadric (x, mu, sigma):
  return pow(metrics(mu,x)**2 + sigma**2, -0.5)
def plateSpine (x,mu):
  r = metrics(mu,x)
  return (r**2) * log(r)
class Rbf:
  def __init__(self, prefix = 'rbf', workers = 4, extra_neurons = 0, from_files = None):
    self.prefix = prefix
    self.workers = workers
    self.extra_neurons = extra_neurons
    # Import partial model
    if from_files is not None:
      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: Make reusable
    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))
    return mu
  def _calculate_sigmas(self):
    neurons = self.neurons
    mu = self.mu
    sigmas = zeros((neurons, ))
    for i in xrange(neurons):
      dists = [0 for _ in xrange(neurons)]
      for j in xrange(neurons):
        if i != j:
          dists[j] = metrics(mu[i], mu[j])
      sigmas[i] = mean(dists)* 2
           # max(dists) / sqrt(neurons * 2))
    return sigmas
  def _calculate_phi(self, x):
    C = self.workers
    neurons = self.neurons
    mu = self.mu
    sigmas = self.sigmas
    phi = self.phi = None
    n = self.n
    def heavy_lifting(c, phi):
      s = jobs[c][1] - jobs[c][0]
      for k, i in enumerate(xrange(jobs[c][0], jobs[c][1])):
        for j in xrange(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]))
        if k % 1000 == 0:
          percent = true_divide(k, s)*100
          print(c, ': {:2.2f}%'.format(percent))
      print(c, ': Done')
    # distributing the work between 4 workers
    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
    for c in range(C):
      jobs.append((c*p, (c+1)*p + (m if c == C-1 else 0)))
      worker = Process(target = heavy_lifting, args = (c, phi))
      workers.append(worker)
      worker.start()
    for worker in workers:
      worker.join()
    return phi
  def _do_algebra(self, y):
    phi = self.phi
    w = lstsq(phi, y)[0]
    os = dot(w, transpose(phi))
    return w, os
    # Saving to HDF5
    os_h5 = os_handle.create_dataset('os', data = os)
  def train(self, x, y):
    self.n = x.shape[0]
    ## Initialize HDF5 caches
    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')
    ## Mu generation
    mu = self.mu = self._generate_mu(x)
    self.neurons = mu.shape[0]
    print('({} neurons)'.format(self.neurons))
    # Save to HDF5
    mu_h5 = mu_handle.create_dataset('mu', data = mu)
    ## Sigma calculation
    print('Calculating Sigma...')
    sigmas = self.sigmas = self._calculate_sigmas()
    # Save to HDF5
    sigmas_h5 = sigma_handle.create_dataset('sigmas', data = sigmas)
    print('Done')
    ## Phi calculation
    print('Calculating Phi...')
    phi = self.phi = self._calculate_phi(x)
    print('Done')
    # Saving to HDF5
    print('Serializing...')
    phi_h5 = phi_handle.create_dataset('phi', data = phi)
    del phi
    self.phi = phi_h5
    print('Done')
    ## Algebra
    print('Doing final algebra...')
    w, os = self.w, _ = self._do_algebra(y)
    # Saving to HDF5
    w_h5 = w_handle.create_dataset('w', data = w)
    os_h5 = os_handle.create_dataset('os', data = os)
    ## Calculate error
    self._calculate_error(y)
    print('Done')
  def predict(self, test_data):
    mu = self.mu = self.mu.value
    sigmas = self.sigmas = self.sigmas.value
    w = self.w = self.w.value
    print('Calculating phi for test data...')
    phi = self._calculate_phi(test_data)
    os = dot(w, transpose(phi))
    savetxt('iok3834.txt', os, delimiter='\n')
    return os
  @property
  def summary(self):
    return '\n'.join( \
      ['-----------------',
      'Training set size: {}'.format(self.n),
      'Hidden layer size: {}'.format(self.neurons),
      '-----------------',
      'Absolute error  : {:02.2f}'.format(self.error),
      'Relative error  : {:02.2f}%'.format(self.relative_error * 100)])
def predict(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))
  for i in range(n):
    for j in range(neur):
      phi[i, j] = multiQuadric(test_data[i,:], mu[j], sigmas[j])
  os = dot(w, transpose(phi))
  savetxt('iok3834.txt', os, delimiter='\n')
  return os

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
讲解Python中fileno()方法的使用
May 24 Python
Python操作MySQL数据库的三种方法总结
Jan 30 Python
简单实现python聊天程序
Apr 01 Python
Python实现的端口扫描功能示例
Apr 08 Python
用Python PIL实现几个简单的图片特效
Jan 18 Python
浅谈Python 递归算法指归
Aug 22 Python
Python的缺点和劣势分析
Nov 19 Python
python列表生成器迭代器实例解析
Dec 19 Python
django 取消csrf限制的实例
Mar 13 Python
Python中常见的数制转换有哪些
May 27 Python
Python如何进行时间处理
Aug 06 Python
PyCharm 2020.1版安装破解注册码永久激活(激活到2089年)
Sep 24 Python
python实现淘宝秒杀聚划算抢购自动提醒源码
Jun 23 #Python
初探TensorFLow从文件读取图片的四种方式
Feb 06 #Python
用十张图详解TensorFlow数据读取机制(附代码)
Feb 06 #Python
Python实现matplotlib显示中文的方法详解
Feb 06 #Python
Python实现自动上京东抢手机
Feb 06 #Python
Python获取指定文件夹下的文件名的方法
Feb 06 #Python
TensorFlow如何实现反向传播
Feb 06 #Python
You might like
9个PHP开发常用功能函数小结
2011/07/15 PHP
php之Memcache学习笔记
2013/06/17 PHP
PHP数字字符串左侧补0、字符串填充和自动补齐的几种方法
2014/05/10 PHP
JavaScript 面向对象之命名空间
2010/05/04 Javascript
JavaScript自定义日期格式化函数详细解析
2014/01/14 Javascript
Jquery 获取指定标签的对象及属性的设置与移除
2014/05/29 Javascript
浅析js预加载/延迟加载
2014/09/25 Javascript
nodejs教程之异步I/O
2014/11/21 NodeJs
简介JavaScript中setUTCSeconds()方法的使用
2015/06/12 Javascript
js控制文本框输入的字符类型方法汇总
2015/06/19 Javascript
用JavaScript判断CSS浏览器类型前缀的两种方法
2015/10/08 Javascript
jQuery实现的购物车物品数量加减功能代码
2016/11/16 Javascript
JS如何判断浏览器类型和详细区分IE各版本浏览器
2017/03/04 Javascript
js处理包含中文的字符串实例
2017/10/11 Javascript
vue 设置路由的登录权限的方法
2018/07/03 Javascript
ng-events类似ionic中Events的angular全局事件
2018/09/05 Javascript
vue项目设置scrollTop不起作用(总结)
2018/12/21 Javascript
小程序实现新用户判断并跳转激活的方法
2019/05/20 Javascript
Nodejs技巧之Exceljs表格操作用法示例
2019/11/06 NodeJs
如何基于原生javaScript生成带图片的二维码
2019/11/21 Javascript
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
2008/09/06 Python
py2exe 编译ico图标的代码
2013/03/08 Python
使用Python制作获取网站目录的图形化程序
2015/05/04 Python
Python正则表达式常用函数总结
2017/06/24 Python
Python中单例模式总结
2018/02/20 Python
python实现朴素贝叶斯算法
2018/11/19 Python
python贪吃蛇游戏代码
2020/04/18 Python
python实现关闭第三方窗口的方法
2019/06/28 Python
python中sort和sorted排序的实例方法
2019/08/26 Python
解决pytorch-yolov3 train 报错的问题
2020/02/18 Python
Python基础类继承重写实现原理解析
2020/04/03 Python
精美的手工家居和生活用品:Nkuku
2019/11/01 全球购物
销售主管的自我评价分享
2014/01/03 职场文书
师德师风承诺书
2014/05/23 职场文书
Pytorch 统计模型参数量的操作 param.numel()
2021/05/13 Python
SQL Server作业失败:无法确定所有者是否有服务器访问权限的解决方法
2021/06/30 SQL Server