Python使用numpy实现BP神经网络

本文完全利用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使用numpy实现BP神经网络的更多相关文章

  1. BP神经网络原理及python实现

    [废话外传]:终于要讲神经网络了,这个让我踏进机器学习大门,让我读研,改变我人生命运的四个字!话说那么一天,我在乱点百度,看到了这样的内容: 看到这么高大上,这么牛逼的定义,怎么能不让我这个技术宅男心 ...

  2. Python实现bp神经网络识别MNIST数据集

    title: "Python实现bp神经网络识别MNIST数据集" date: 2018-06-18T14:01:49+08:00 tags: [""] cat ...

  3. BP神经网络在python下的自主搭建梳理

    本实验使用mnist数据集完成手写数字识别的测试.识别正确率认为是95% 完整代码如下: #!/usr/bin/env python # coding: utf-8 # In[1]: import n ...

  4. 三层BP神经网络的python实现

    这是一个非常漂亮的三层反向传播神经网络的python实现,下一步我准备试着将其修改为多层BP神经网络. 下面是运行演示函数的截图,你会发现预测的结果很惊人! 提示:运行演示函数的时候,可以尝试改变隐藏 ...

  5. python手写bp神经网络实现人脸性别识别1.0

    写在前面:本实验用到的图片均来自google图片,侵删! 实验介绍 用python手写一个简单bp神经网络,实现人脸的性别识别.由于本人的机器配置比较差,所以无法使用网上很红的人脸大数据数据集(如lf ...

  6. python对BP神经网络实现

    python对BP神经网络实现 一.概念理解 开始之前首先了解一下BP神经网络,BP的英文是back propagationd的意思,它是一种按误差反向传播(简称误差反传)训练的多层前馈网络,其算法称 ...

  7. BP神经网络与Python实现

    人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善. 联想大家熟悉的回归问题, 神经网络模型实际上是根据训练样本创造出一个多维输入多维输出的函数, 并使用该函数进行预测, 网 ...

  8. python构建bp神经网络_曲线拟合(一个隐藏层)__2.代码实现

    IDE:jupyter 抽象程度可能不是那么高,以后再优化. 理论和代码实现的差距还是挺大的 数据集请查看 python构建bp神经网络(一个隐藏层)__1.数据可视化 部分代码预览 git上传.ip ...

  9. 机器学习:python使用BP神经网络示例

    1.简介(只是简单介绍下理论内容帮助理解下面的代码,如果自己写代码实现此理论不够) 1) BP神经网络是一种多层网络算法,其核心是反向传播误差,即: 使用梯度下降法(或其他算法),通过反向传播来不断调 ...

随机推荐

  1. pandas数据类型判断(三)数据判断

    1.函数:空值判断 1)判断数值是否为空用 pd.isna,pd.isnull,np.isnan2)判断字符串是否为空用 pd.isna,pd.isnull:3)判断时间是否为空用 pd.isna,p ...

  2. PAT1059Prime Factors

    1059 Prime Factors (25分)   Given any positive integer N, you are supposed to find all of its prime f ...

  3. 洛谷 AT2434 JOI 公園 (JOI Park) 题解

    人生第一次AC黑题,我太感动了. 每日一题 day31 打卡 Analysis 先跑遍DJ,求出1到 i的最短路.得到每个点到 1号点的距离后,从小到大排序一遍,这时便可以枚举每个点到 1号点的距离修 ...

  4. Kubernetes 学习26 基于kubernetes的Paas概述

    一.概述 1.通过以往的学习应该可以了解到k8s 和以往提到的devops概念更容易落地了.比如我们说的CI,CD,CD a.CI(Continuous Integration):持续集成 b.CD( ...

  5. learning java Runtime类中的exec

    var rt = Runtime.getRuntime(); // 类c语言当中的system()函数. rt.exec("notepad.exe");

  6. JMX类型监控

    zabbix服务器配置 zabbix_server.conf: JavaGateway=10.42.239.219 #JavaGateway的IP JavaGatewayPort=10052 #Jav ...

  7. C 指针常量 和常量指针 指向常量的指针常量的使用

    #include <stdio.h> /* 指针常量 和常量指针 指向常量的指针常量 */ int main() { int a = 100; int b =200; int* const ...

  8. CODE FESTIVAL 2017 qual C 题解

    传送门 \(A\) 咕咕 const int N=15; char s[N];int n; int main(){ scanf("%s",s+1),n=strlen(s+1); f ...

  9. Day12:H5

    掌握HTML+CSS+JavaScript相关知识 了解HTML5的结构标签: 掌握新增和删去的标签及相关属性 运用HTML5相关知识进行实际开发 下面哪种语法中是对大小写进行区分的? XHTML H ...

  10. SSM ehcache 配置 mapper 文件出错

    异常 十二月 26, 2017 1:44:49 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertie ...