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神经网络是一种多层网络算法,其核心是反向传播误差,即: 使用梯度下降法(或其他算法),通过反向传播来不断调 ...
随机推荐
- redis详解(包含使用场景)
本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...
- (尚015)Vue过滤器(对显示的数据进行格式化)
现在日期为:当前时间-1970年1月1日0时0分0秒的时间差 日期格式化:百度搜索moment 1.test015.html <!DOCTYPE html><html lang=&q ...
- java对接微信小程序
https://www.cnblogs.com/lyn20141231/p/11210372.html https://blog.csdn.net/sinat_29039125/article/det ...
- javascript练习题
function Vertex(city, x) { this.name = city; this.num = x; } var node0 = new Vertex("邯郸", ...
- Pro自定义数据源原理
1. 概念 Connector:定义连接到一个数据源的连接信息,用于创建datastore. Datastore:代表一个数据源的实例,用于打开一个或多个tables或feature class. ...
- jquery.nicescroll.js Unable to preventDefault inside passive event listener due to target being treated as passive.
解决办法就是:https://github.com/bestjhh/Plugin 下载替换. 参考: https://github.com/bestjhh/Plugin https://blog.cs ...
- [Shell]MySql慢查询日志GetShell
通过开启慢查询日志,配置可解析日志文件GETSHELL. MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句. long_query_time的默认值 ...
- Redis恢复数据
对于单点或者集群,都可以用 cat data.txt | redis-cli --pipe方式进行冷恢复. 对于大数据量会很慢,但不会出错.
- Coupled和segregated【转载】
转载自:http://blog.sina.com.cn/s/blog_67873f6c0100ltq6.html 问题1: 我看中文帮组里说是'分离'的意思?我绝对翻译不太好,请问有更好的翻译吗? 和 ...
- Nginx:fastcgi_param详解
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param QUERY_STRI ...