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神经网络的更多相关文章
- BP神经网络原理及python实现
[废话外传]:终于要讲神经网络了,这个让我踏进机器学习大门,让我读研,改变我人生命运的四个字!话说那么一天,我在乱点百度,看到了这样的内容: 看到这么高大上,这么牛逼的定义,怎么能不让我这个技术宅男心 ...
- Python实现bp神经网络识别MNIST数据集
title: "Python实现bp神经网络识别MNIST数据集" date: 2018-06-18T14:01:49+08:00 tags: [""] cat ...
- BP神经网络在python下的自主搭建梳理
本实验使用mnist数据集完成手写数字识别的测试.识别正确率认为是95% 完整代码如下: #!/usr/bin/env python # coding: utf-8 # In[1]: import n ...
- 三层BP神经网络的python实现
这是一个非常漂亮的三层反向传播神经网络的python实现,下一步我准备试着将其修改为多层BP神经网络. 下面是运行演示函数的截图,你会发现预测的结果很惊人! 提示:运行演示函数的时候,可以尝试改变隐藏 ...
- python手写bp神经网络实现人脸性别识别1.0
写在前面:本实验用到的图片均来自google图片,侵删! 实验介绍 用python手写一个简单bp神经网络,实现人脸的性别识别.由于本人的机器配置比较差,所以无法使用网上很红的人脸大数据数据集(如lf ...
- python对BP神经网络实现
python对BP神经网络实现 一.概念理解 开始之前首先了解一下BP神经网络,BP的英文是back propagationd的意思,它是一种按误差反向传播(简称误差反传)训练的多层前馈网络,其算法称 ...
- BP神经网络与Python实现
人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善. 联想大家熟悉的回归问题, 神经网络模型实际上是根据训练样本创造出一个多维输入多维输出的函数, 并使用该函数进行预测, 网 ...
- python构建bp神经网络_曲线拟合(一个隐藏层)__2.代码实现
IDE:jupyter 抽象程度可能不是那么高,以后再优化. 理论和代码实现的差距还是挺大的 数据集请查看 python构建bp神经网络(一个隐藏层)__1.数据可视化 部分代码预览 git上传.ip ...
- 机器学习:python使用BP神经网络示例
1.简介(只是简单介绍下理论内容帮助理解下面的代码,如果自己写代码实现此理论不够) 1) BP神经网络是一种多层网络算法,其核心是反向传播误差,即: 使用梯度下降法(或其他算法),通过反向传播来不断调 ...
随机推荐
- Lock锁和synchronized的不同点
- 安装node.js 和 npm 的完整步骤
vue 生命周期 1,beforeCreate 组件刚刚被创建 2,created 组件创建完成 3,beforeMount 挂载之前 4,mounted 挂载之后 5,beforeDestory 组 ...
- b/s实现大文件上传分片上传断点续传
关键部分 前端用file.slice()分块 前端用FileReader获取每一分块的md5值 后端用MultipartFile接受分块文件 后端用FileOutputStream拼装分块文件 话不多 ...
- 博客系统的使用(typecho、WordPress等等)
一.下载,解压,安装 二.Apache配置虚拟主机,(host文件修改) 三.开启php.ini中pdo类型的扩展适配数据库 四.按照指示页面配置 五.操作控制面板和blog前台
- python3 结束子线程
最近公司内部网络经常出问题,奇慢无比,导致人脸检测程序在下载图片时经常卡住,为了不影响数据的核对, 决定在网络不佳图片下载超时后放弃下载,继续执行后续程序. 于是整理出解决思路如下: 1.在线程中完成 ...
- Linux工具[转]
ref: https://github.com/linw7/Skill-Tree/blob/master/Linux%E5%B7%A5%E5%85%B7.md Linux工具 Linux下还是有很多超 ...
- 笔记-读官方Git教程(1)~认识Git
小书匠版本管理 教程内容基本来自git官方教程,认真都了系列的文章,然后对一些重点的记录下来,做了简单的归纳并写上自己的思考. 目录: 1.Git介绍 2.Git版本控制原理 3.Git特点 4.Gi ...
- navicat 链接阿里云服务器数据库报80070007 的错误
navicat用ssh跳转登录mysql连接时报: 80070007: SSH Tunnel: Server does not support diffie-hellman-group1-sha1 f ...
- P3719 [AHOI2017初中组]rexp——递归模拟
P3719 [AHOI2017初中组]rexp 没有什么算法的题做起来真不适应,这道题深深讽刺了我想用栈维护匹配括号个数的想法: 递归解决就行了: 时刻注意函数返回值是什么,边界条件是什么: #inc ...
- Django重定向你是如何实现的?用的什么状态码?
使用HttpResponseRedirect redirect和reverse 状态码:302,301