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. k8s-yaml

    apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...

  2. CF19D Points 平衡树

    题意:支持插入/删除点 $(x,y)$,查询一个点右上方横坐标与之最接近的点坐标. 我们可以对于每一个操作过的横坐标都开一个 $set$,然后再开一个平衡树,维护每个横坐标上最大的纵坐标. 然后查询点 ...

  3. Qt智能指针QPointer, QSharedDataPointer ,QSharedPointer,QWeakPointer和QScopedPointer

    QPointer (4.0) 已经过时,可以被QWeakPointer所替代,它不是线程安全的. QSharedDataPointer (4.0) -- 提供对数据的COPY-ON-WRITE以及浅拷 ...

  4. Filebeat在windows下安装使用

    一.windows下安装Filebeat 官网下载安装包 解压到指定目录,打开解压后的目录,打开filebeat.yml进行配置. 1.配置为输出到ElasticSearch ①:配置 Filebea ...

  5. flutter报错:NoSuchMethodError: The method '>' was called on null.

    写了个list,发现出不来,报错 flutter: Another exception was thrown: RenderBox was not laid out: _RenderScrollSem ...

  6. 洛谷P1038 神经网络题解

    注意如果是 \(if(c[i])\) 这条语句并没有说明c[i]不为负数,所以说最好老老实实的写 #include<cstdio> #define _ 0 using namespace ...

  7. 交互设计算法基础(2) - Selection Sort

    int[] selection_sort(int[] arr) { int i, j, min, temp, len=arr.length; for (i=0; i<len-1; i++) { ...

  8. ArcGIS Enterprise 10.7.1新特性:批量发布服务

    ArcGIS Enterprise 10.7.1提供了批量发布GIS服务的功能,能大大简化GIS系统管理员的工作量. 作为发布人员和管理人员,支持向Portal for ArcGIS添加云存储.文件共 ...

  9. Matlab画图的输出格式

    利用Matlab命令,可以输出.eps, .pdf格式的图形.有时候,在图形窗口直接保存会导致图形不完整,这时,可以用如下命令代替: saveas(p1, 't1.eps'); saveas(p1, ...

  10. js off 缓动动画

    动画也有很多种,一起来学习,缓动动画吧 缓动动画 1.缓动动画原理=盒子位置+(目标盒子位置-现在盒子位置)/10 2.步长越来越小 3.让步长越来越小的公式      步长=(目标位置-本身位置)/ ...