Python使用numpy实现BP神经网络


Posted in Python onMarch 10, 2018

本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x)=x。BP神经网络的具体原理此处不再介绍。

import numpy as np 
 
class NeuralNetwork(object): 
  def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate): 
    # Set number of nodes in input, hidden and output layers.设定输入层、隐藏层和输出层的node数目 
    self.input_nodes = input_nodes 
    self.hidden_nodes = hidden_nodes 
    self.output_nodes = output_nodes 
 
    # Initialize weights,初始化权重和学习速率 
    self.weights_input_to_hidden = np.random.normal(0.0, self.hidden_nodes**-0.5,  
                    ( self.hidden_nodes, self.input_nodes)) 
 
    self.weights_hidden_to_output = np.random.normal(0.0, self.output_nodes**-0.5,  
                    (self.output_nodes, self.hidden_nodes)) 
    self.lr = learning_rate 
     
    # 隐藏层的激励函数为sigmoid函数,Activation function is the sigmoid function 
    self.activation_function = (lambda x: 1/(1 + np.exp(-x))) 
   
  def train(self, inputs_list, targets_list): 
    # Convert inputs list to 2d array 
    inputs = np.array(inputs_list, ndmin=2).T  # 输入向量的shape为 [feature_diemension, 1] 
    targets = np.array(targets_list, ndmin=2).T  
 
    # 向前传播,Forward pass 
    # TODO: Hidden layer 
    hidden_inputs = np.dot(self.weights_input_to_hidden, inputs) # signals into hidden layer 
    hidden_outputs = self.activation_function(hidden_inputs) # signals from hidden layer 
 
     
    # 输出层,输出层的激励函数就是 y = x 
    final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs) # signals into final output layer 
    final_outputs = final_inputs # signals from final output layer 
     
    ### 反向传播 Backward pass,使用梯度下降对权重进行更新 ### 
     
    # 输出误差 
    # Output layer error is the difference between desired target and actual output. 
    output_errors = (targets_list-final_outputs) 
 
    # 反向传播误差 Backpropagated error 
    # errors propagated to the hidden layer 
    hidden_errors = np.dot(output_errors, self.weights_hidden_to_output)*(hidden_outputs*(1-hidden_outputs)).T 
 
    # 更新权重 Update the weights 
    # 更新隐藏层与输出层之间的权重 update hidden-to-output weights with gradient descent step 
    self.weights_hidden_to_output += output_errors * hidden_outputs.T * self.lr 
    # 更新输入层与隐藏层之间的权重 update input-to-hidden weights with gradient descent step 
    self.weights_input_to_hidden += (inputs * hidden_errors * self.lr).T 
  
  # 进行预测   
  def run(self, inputs_list): 
    # Run a forward pass through the network 
    inputs = np.array(inputs_list, ndmin=2).T 
     
    #### 实现向前传播 Implement the forward pass here #### 
    # 隐藏层 Hidden layer 
    hidden_inputs = np.dot(self.weights_input_to_hidden, inputs) # signals into hidden layer 
    hidden_outputs = self.activation_function(hidden_inputs) # signals from hidden layer 
     
    # 输出层 Output layer 
    final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs) # signals into final output layer 
    final_outputs = final_inputs # signals from final output layer  
     
    return final_outputs

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python批量修改文件后缀的方法
Jan 26 Python
从零学python系列之从文件读取和保存数据
May 23 Python
Python字符串和文件操作常用函数分析
Apr 08 Python
常见的python正则用法实例讲解
Jun 21 Python
Python探索之ModelForm代码详解
Oct 26 Python
通过Python 获取Android设备信息的轻量级框架
Dec 18 Python
python3.5绘制随机漫步图
Aug 27 Python
Python基于mysql实现学生管理系统
Feb 21 Python
Python获取当前脚本文件夹(Script)的绝对路径方法代码
Aug 27 Python
Python如何使用内置库matplotlib绘制折线图
Feb 24 Python
python实现与redis交互操作详解
Apr 21 Python
Python小白不正确的使用类变量实例
May 29 Python
python实现日常记账本小程序
Mar 10 #Python
python实现简单神经网络算法
Mar 10 #Python
TensorFlow saver指定变量的存取
Mar 10 #Python
TensorFLow用Saver保存和恢复变量
Mar 10 #Python
tensorflow创建变量以及根据名称查找变量
Mar 10 #Python
Python2中文处理纪要的实现方法
Mar 10 #Python
python实现冒泡排序算法的两种方法
Mar 10 #Python
You might like
解析thinkphp的左右值无限分类
2013/06/20 PHP
php中try catch捕获异常实例详解
2020/08/06 PHP
PHP获取类私有属性的3种方法
2020/09/10 PHP
利用404错误页面实现UrlRewrite的实现代码
2008/08/20 Javascript
下载网站打开页面后间隔多少时间才显示下载链接地址的代码
2010/04/25 Javascript
JavaScript 一道字符串分解的题目
2011/08/03 Javascript
JS小功能(onmouseover实现选择月份)实例代码
2013/11/28 Javascript
extJS中常用的4种Ajax异步提交方式
2014/03/07 Javascript
jQuery遍历DOM节点操作之filter()方法详解
2016/04/14 Javascript
jQuery+ajax实现滚动到页面底部自动加载图文列表效果(类似图片懒加载)
2016/06/07 Javascript
详解Vue-cli 创建的项目如何跨域请求
2017/05/18 Javascript
解决vue中虚拟dom,无法实时更新的问题
2018/09/15 Javascript
vue 翻页组件vue-flip-page效果
2020/02/05 Javascript
JS实现图片懒加载(lazyload)过程详解
2020/04/02 Javascript
VSCode写vue项目一键生成.vue模版,修改定义其他模板的方法
2020/04/17 Javascript
支付宝小程序实现省市区三级联动
2020/06/21 Javascript
Javascript新手入门之字符串拼接与变量的应用
2020/12/03 Javascript
[54:56]DOTA2上海特级锦标赛主赛事日 - 5 总决赛Liquid VS Secret第三局
2016/03/06 DOTA
[47:22]Mineski vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
举例详解Python中循环语句的嵌套使用
2015/05/14 Python
Python简单计算文件夹大小的方法
2015/07/14 Python
Python中文竖排显示的方法
2015/07/28 Python
Python实现二维有序数组查找的方法
2016/04/27 Python
详解Python3中setuptools、Pip安装教程
2019/06/18 Python
Python检查图片是否损坏及图片类型是否正确过程详解
2019/09/30 Python
Python3安装pip工具的详细步骤
2019/10/14 Python
Python对称的二叉树多种思路实现方法
2020/02/28 Python
使用Python和百度语音识别生成视频字幕的实现
2020/04/09 Python
Python-jenkins模块获取jobs的执行状态操作
2020/05/12 Python
基于Python绘制个人足迹地图
2020/06/01 Python
浅谈PyTorch中in-place operation的含义
2020/06/27 Python
使用CSS3来代替JS实现交互
2017/08/10 HTML / CSS
全国税务系统先进集体事迹材料
2014/05/19 职场文书
应届生求职信
2014/05/31 职场文书
重阳节活动总结
2014/08/27 职场文书
大学生活感想
2015/08/10 职场文书