# BP 算法
> Author: Lijuan Wang
> E-mail: 2015014368@buct.edu.cnmailto:2015014368@buct.edu.cn
> Date: 2018-05-27
Description: 纯 numpy 实现的 Bp 神经网络,实现了全连接层,MSEloss,Sigmoid、Relu 激活函数,以及优化器。前端可实时监控训练集 loss 与测试集 loss 以及训练测试集拟合曲线
## 结果展示
### 网络设置
可设置每层神经元个数
![](https://www.writebug.com/myres/static/uploads/2022/1/12/ad582ed8cecf28d300e2f9a6ae66dfc2.writebug)
### 2d 可视化训练
拟合任意一元函数,下面展示 sin(x),x^2
![](https://www.writebug.com/myres/static/uploads/2022/1/12/c9cb0645b7ad5eb59dfd8b6bb5d3b81f.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/1/12/68fe105c6a3924e798f9202d06763bb3.writebug)
### 3d 可视化训练
可拟合常用二元曲面
![](https://www.writebug.com/myres/static/uploads/2022/1/12/e6529e1f290cea27bdc1aa950f83aa6e.writebug)
## 代码解析
### 构建计算图
构造了计算图,在前向传播和反向传播中根据相应函数求导。
```python
class Net:
def __init__(self):
self.graph=[] #用来存放计算图
self.InitInput=None
self.LR=0.00001
self.target=None
self._loss=[]
self.momentum=1
```
### 全连接层
继承基类,实现了基类的方法。将该层参数与梯度封装进 Variable,供优化器更新。
```python
def LinearLayer(self,input,output,bias=None,weights=None):#0
l=LLayer(input,output,bias,weights)
self.graph.append((0,l))
def LinearLayerForward(self,input,i):
self.graph[i][1].output=np.dot(input,self.graph[i][1].weight)+self.graph[i][1].bias
return self.graph[i][1].output
def LinearLayerBackward(self,input,i,gra,flag=None):
grad=gra.copy()
input=np.array(input)
grad=np.array(grad)
self.graph[i][1].weight=np.array(self.graph[i][1].weight)
self.graph[i][1].wgrad=np.dot(input.T,grad)
self.graph[i][1].bgrad=grad
self.graph[i][1].grad=np.dot(grad,self.graph[i][1].weight.T)
return self.graph[i][1].grad
```
### 激活函数
Sigmoid 激活函数存在梯度消失的问题,建议使用 Relu 激活函数
```python
def Relu(self):#1
l=Layer()
self.graph.append((1,l))
def ReluForward(self,input,i):
input=np.array(input)
self.graph[i][1].output=np.maximum(0,input)
self.graph[i][1].grad = np.zeros(input.shape[0])
return self.graph[i][1].output
def ReluBackward(self,input,i,grad):
mask=(input==self.graph[i][1].output)
newgrad=np.zeros(mask.shape)
newgrad[mask]=1
self.graph[i][1].grad=grad*newgrad
return self.graph[i][1].grad
```
```python
def Sigmoid(self):#4
l=Layer()
self.graph.append((4,l))
def SigmoidForward(self,input,i):
input=np.array(input)
self.graph[i][1].output=1/(np.exp(-input)+1)
self.graph[i][1].grad=np.zeros(input.shape[0])
return self.graph[i][1].output
def SigmoidBackward(self,input,i,grad):
temp=self.graph[i][1].output*(1-self.graph[i][1].output)
self.graph[i][1].grad=grad*temp
return self.graph[i][1].grad
```
### 损失函数
损失函数实现了 MSE(均方差损失)
```python
def MSELoss(self):#3
l=Layer()
self.graph.append((3,l))
def MSELossForward(self,input,i,label):
self.graph[i][1].output = (input-label)**2/2
return self.graph[i][1].output
def MSELossBackward(self,input,i,grad):
if self.graph[i][1].output is not None:
if len(input.shape)<=0:
input=np.expand_dims(input,axis=0)
self.graph[i][1].grad=(input-self.target)
return self.graph[i][1].grad
```
## 局限与不足
本课设由于时间仓促,存在以下局限,供参考者改进:
* 没有实现 batch 的参数更新
* 只有基础网络结构
## 代码结构
```
BP
│
├── readmeDisplay # 存放本说明文件使用的图片数据
│
├── static # 存放网页展示所需的外部js
│
├── templates # 存放网页展示所需的html
│ ├── square.html # 拟合平方函数的html与js
│ ├── sin.html # 拟合正弦函数的html与js
│ └── two.html # 拟合二元函数曲面的html与js
│
├── BPFlask.py # 后端代码
│
├── Network.py # 给后端代码写好调用接口的网络代码
│
└── readme.md # 此说明文件
```
## 环境
* `Python 3.6 `
* 依赖:`flask` `plotly.js`
* 系统支持:`ubuntu`
* 编程环境:`pycharm`
没有合适的资源?快使用搜索试试~ 我知道了~
BPAr-master.zipBPAr-master.zip
共47个文件
js:11个
css:8个
html:7个
需积分: 9 0 下载量 129 浏览量
2023-01-27
13:27:27
上传
评论
收藏 420KB ZIP 举报
温馨提示
BPAr-master.zipBPAr-master.zipBPAr-master.zip
资源推荐
资源详情
资源评论
收起资源包目录
BPAr-master.zip (47个子文件)
bpar
readmeDisplay
4.JPG 64KB
3.JPG 61KB
1.JPG 75KB
2.JPG 60KB
LICENSE 1KB
BPFlask.py 9KB
templates
two.html 9KB
sin.html 9KB
index2.html 123B
square.html 9KB
embed.html 1KB
index.html 6KB
test.html 2KB
.idea
other.xml 193B
workspace.xml 39KB
misc.xml 192B
Bptest.iml 324B
modules.xml 264B
static
js
html5shiv.min.js 3KB
bootstrap.js 59KB
bootstrap.min.js 31KB
easypiechart-data.js 534B
chart-data.js 3KB
bootstrap-table.js 49KB
respond.min.js 4KB
chart.min.js 50KB
easypiechart.js 9KB
jquery-1.11.1.min.js 94KB
bootstrap-datepicker.js 46KB
css
bootstrap-table.css 4KB
bootstrap.min.css 107KB
bootstrap-theme.css.map 23KB
datepicker3.css 33KB
bootstrap-theme.css 21KB
bootstrap.css.map 216KB
bootstrap.css 128KB
bootstrap-theme.min.css 18KB
styles.css 15KB
datepicker.css 18KB
tables
data1.json 2KB
data2.json 548B
fonts
glyphicons-halflings-regular.svg 61KB
glyphicons-halflings-regular.ttf 40KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.eot 20KB
README.md 5KB
GetNum.py 14KB
共 47 条
- 1
资源评论
计算机毕设论文
- 粉丝: 9878
- 资源: 398
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功