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应用程序在windows下不出现cmd窗口的办法
May 29 Python
python实现读取命令行参数的方法
May 22 Python
Python基于递归实现电话号码映射功能示例
Apr 13 Python
使用Python进行QQ批量登录的实例代码
Jun 11 Python
python按时间排序目录下的文件实现方法
Oct 17 Python
关于Django ForeignKey 反向查询中filter和_set的效率对比详解
Dec 15 Python
Django框架登录加上验证码校验实现验证功能示例
May 23 Python
Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]
Feb 13 Python
python用什么编辑器进行项目开发
Jun 17 Python
在django中实现choices字段获取对应字段值
Jul 12 Python
Python pymsql模块的使用
Sep 07 Python
一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃
Nov 02 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
PHP文件缓存内容保存格式实例分析
2014/08/20 PHP
php中实现xml与mysql数据相互转换的方法
2014/12/25 PHP
javascript+php实现根据用户时区显示当地时间的方法
2015/03/11 PHP
yii2.0数据库迁移教程【多个数据库同时同步数据】
2016/10/08 PHP
javascript Discuz代码中的msn聊天小功能
2008/05/25 Javascript
jQuery 性能优化指南(2)
2009/05/21 Javascript
JavaScript浏览器选项卡效果
2010/08/25 Javascript
javascript textContent与innerText的异同分析
2010/10/22 Javascript
深入理解JavaScript 闭包究竟是什么
2013/04/12 Javascript
node.js中的path.dirname方法使用说明
2014/12/09 Javascript
js判断文本框输入的内容是否为数字
2015/12/23 Javascript
一个用jquery写的判断div滚动条到底部的方法【推荐】
2016/04/29 Javascript
深入理解Node.js 事件循环和回调函数
2016/11/02 Javascript
JavaScript使用delete删除数组元素用法示例【数组长度不变】
2017/01/17 Javascript
vue.js实例todoList项目
2017/07/07 Javascript
基于Vue的ajax公共方法(详解)
2018/01/20 Javascript
node实现生成带参数的小程序二维码并保存到本地功能示例
2018/12/05 Javascript
详解Vue This$Store总结
2018/12/17 Javascript
js中值引用和地址引用实例分析
2019/06/21 Javascript
详细教你微信公众号正文页SVG交互开发技巧
2019/07/25 Javascript
简单了解JS打开url的方法
2020/02/21 Javascript
vue实现商品列表的添加删除实例讲解
2020/05/14 Javascript
jQuery 隐藏/显示效果函数用法实例分析
2020/05/20 jQuery
js动态生成表格(节点操作)
2021/01/12 Javascript
python进程类subprocess的一些操作方法例子
2014/11/22 Python
Python探索之爬取电商售卖信息代码示例
2017/10/27 Python
numpy和pandas中数组的合并、拉直和重塑实例
2019/06/28 Python
Python逐行读取文件内容的方法总结
2020/02/14 Python
python怎么删除缓存文件
2020/07/19 Python
全面解析CSS Media媒体查询使用操作(推荐)
2017/08/15 HTML / CSS
ZWILLING双立人英国网上商店:德国刀具锅具厨具品牌
2018/05/15 全球购物
关于赌博的检讨书
2014/01/24 职场文书
活动宣传策划方案
2014/05/23 职场文书
学前班幼儿评语大全
2014/12/29 职场文书
正规借条模板
2015/05/26 职场文书
pytorch中的 .view()函数的用法介绍
2022/03/17 Python