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神经网络是一种多层网络算法,其核心是反向传播误差,即: 使用梯度下降法(或其他算法),通过反向传播来不断调 ...
随机推荐
- 常见的C语言编程规范
头文件: 1.头文件中适合放置接口的声明,不适合放置实现. 2.头文件应向稳定的方向包含,产品依赖于平台,平台依赖于标准库. 3. .c/.h文件禁止包含用不到的头文件. 4.每一个.c文件应有一个同 ...
- k8s-yaml
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...
- learning java Cloneable
class Address{ String Detail; public Address(String detail){ this.Detail = detail; } } class User im ...
- svg坐标转换
本文内容转自 how-to-translate-from-dom-to-svg-coordinates-and-back-again svg coordinate system Why we need ...
- 如何快速把ps序列图层建立帧动画?
工具ps 1.将序列帧图片载入ps 新建->脚本->将文件载入堆栈 2.制作序列帧动画 窗口->时间轴->时间轴面板右上角菜单->从图层建立帧 3.去除多余的透明画布 全 ...
- mac中强大的快捷键
用mac本不过一年左右, 但是越用越感觉到mac的强大. 只是从快捷键这个方面去说吧. 与 windows 系统的比较 从接触电脑开始, 就是与windows为伍, 最初的window98, xp 等 ...
- CF1205题解
B 最高有\(64\)位,当\(n\le 128\)时,最坏情况形成不了三元环,\(floyed\)暴力做 否则直接输出\(3\) C 题意的\(n\)均为奇数,设\((i,j)\),把\(i+j\) ...
- 【Python 代码】CS231n中Softmax线性分类器、非线性分类器对比举例(含python绘图显示结果)
1 #CS231n中线性.非线性分类器举例(Softmax) #注意其中反向传播的计算 # -*- coding: utf-8 -*- import numpy as np import matplo ...
- CRMEB中因为重写规则导致的服务器异常和404之解决办法
问题描述:安装CRMEB后,只能通过https://域名//index.php/admin访问到后台,而不能直接通过https://域名/admin访问到后台,以至于导致进入系统后台出现有的功能界面可 ...
- Perl快速查找素数
查找N内的所有素数,首先想到的就是: 对整数N从2开始到sqrt(N),进行整除计算,能整除则计算N+1,然后循环.方法简单,但效率低下.1000,000内的素数个数: #!/usr/bin/perl ...