<h1 align=center>实验二实验报告</h1>
<h1 align=center>BP神经网络和卷积神经网络CNN</h1>
## 一. 实验要求
> 构造一个三层的 BP 神经网络和一个卷积神经网络,完成手写 0-9 数字的识别:
>
> 1. 设计网络的结构,比如层数,每层的神经元数,单个神经元的输入输出函数;
> 2. 根据数字识别的任务,设计网络的输入和输出;
> 3. 实现 BP 网络的错误反传算法,完成神经网络的训练和测试,最终识别率达到 70% 以上;
> 4. 数字识别训练集可以自己手工制作,也可以网上下载,要求具有可视化图形界面,能够输入输出。
>
> 5.进一步的,用**卷积神经网络**实现以上任务,对比深度学习与浅层模型。
## 二. 算法原理
### 1. BP 神经网络
神经元是构成神经网络的基本单元,其主要是模拟生物神经元的结构和特性,接收一组输入信号并产生输出。一个典型的神经元如下图所示
<img src="assert/神经元.png" alt="神经元" style="zoom:60%;" />
其中激活函数是一个连续并可导的非线性函数,给神经网络带来非线性部分。
在这次实验里,我选择了两种激活函数,一个是 Sigmoid 类函数中的 Logistic 函数,另外一个是 ReLU 函数。
Logistic 函数方程和导函数方程如下:
![](https://www.writebug.com/myres/static/uploads/2022/4/27/72cd6971210828afbf94222c911c54e4.writebug)
当输入值在 0 附近时,Sigmoid 型函数近似为线性函数;当输入值靠近两端时,对输入进行抑制. 输入越小,越接近于 0;输入越大,越接近于 1。函数图像如下:
<img src="assert/Logistic.png" alt="Logistic" style="zoom:60%;" />
ReLU 函数方程和导函数方程如下:
![](https://www.writebug.com/myres/static/uploads/2022/4/27/f5b5bc481e4458b535a6dc9333ebe145.writebug)
采用 ReLU 的神经元只需要进行加、乘和比较的操作,计算上更加高效。ReLU 函数被认为有生物上的解释性,比如单侧抑制、宽兴奋边界。同时采用 ReLU 的神经网络也有很好的稀疏性。
在这次实验中,我实现了一个三层的 BP 神经网络,网络架构大致如下:
<img src="assert/BP.jpg" alt="BP" style="zoom: 50%;" />
更新参数的时候我们使用反向传播(即 BP 算法)。假设损失函数为$\partial \mathcal{L}(\boldsymbol{y}, \hat{\boldsymbol{y}})$,根据链式法则,损失函数相对于神经元权重和偏置的偏导公式为:
![](https://www.writebug.com/myres/static/uploads/2022/4/27/94d56f45c0cf1086417524a163164614.writebug)
由链式法则,令$\boldsymbol{a}^{(l)}$代表每一层的激活值,$\delta^{(l)}$为每一层的误差项,可以得到偏导计算公式如下:
![](https://www.writebug.com/myres/static/uploads/2022/4/27/9db4ea4485fce312edd93be4361632aa.writebug)
激活值和误差项都是可以在前向传播的时候算出来的。更新参数时使用梯度下降优化即可。
在本次实验中我的损失函数为
![](https://www.writebug.com/myres/static/uploads/2022/4/27/c33df0f2f839934b14cf78bab7858b74.writebug)
它的导函数十分简单,就是:
![](https://www.writebug.com/myres/static/uploads/2022/4/27/75a1b9e31eaa24e65fc4f5d63aa6c9fd.writebug)
最后算法伪代码如下:
```
repeat
for n in 样本数:
获取样本 x_n, y_n
前馈计算每一层的激活值a
反向传播计算误差项delta
//神经网络的权重为W,偏置为b,学习率为k
W = W - k (delta * a)
b = b - k * delta
end
untill 训练次数结束
```
### 2. CNN 卷积神经网络
二维卷积的公式为:
![](https://www.writebug.com/myres/static/uploads/2022/4/27/89594876a5cfca12a5f0bddb9205c331.writebug)
在图片上的操作如下图所示:
<img src="assert/jj.png" alt="jj" style="zoom: 67%;" />
不过这次实验我们直接使用 `Pytorch` 框架,这些数学式子不需要我实现。
在 CNN 神经网络里,我们用卷积来代替全连接,这样神经网络会有局部连接和权重共享的特性。此外,我们还会在卷积层之间加上汇聚层,最后还会有全连接层再到输出层。典型的 CNN 网络模型如下:
<img src="assert/CNNstru.png" alt="CNNstru" />
## 三、 实现过程
本次实验采用 MNIST 数据集,像素为 28\*28。数据的输入输出就不赘述了,源代码里面有。
### 1. BP 神经网络
由于输入图片的像素为 28\*28,所以神经网络的输入层有 28\*28 = 784 个节点。而输出层有 10 个节点,代表该图片对应 10 个数字的概率。
首先是激活函数的实现,`active_f` 是激活函数,`active_f_g` 是对应的导函数。
Sigmoid 激活函数:
```python
def active_f(x):
return 1 / (1 + np.exp(-x))
def active_f_g(x):
return active_f(x) * (1 - active_f(x))
```
ReLU 激活函数:
```python
def active_f(x):
return np.maximum(0, x)
def active_f_g(x):
return (x >= 0).astype(int)
```
BP 神经网络实现如下,包含了前向传播和反向传播过程。
```python
class BP:
def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
#初始化参数
self.__weight1 = np.random.normal(0.0, pow(hidden_nodes, -0.5), (hidden_nodes, input_nodes))
self.__weight2 = np.random.normal(0.0, pow(output_nodes, -0.5), (output_nodes, hidden_nodes))
self.__learning_rate = learning_rate
def train(self, t_input, t_target):
#前向过程
inputs = np.array(t_input, ndmin=2).T
targets = np.array(t_target, ndmin=2).T
hidden_inputs = np.dot(self.__weight1, inputs)
hidden_outputs = active_f(hidden_inputs)
final_inputs = np.dot(self.__weight2, hidden_outputs)
final_outputs = active_f(final_inputs)
#计算误差并反向传播
output_errors = targets - final_outputs
hidden_errors = np.dot(self.__weight2.T, output_errors)
self.__weight2 += self.__learning_rate * np.dot((output_errors * active_f_g(final_inputs)),
np.transpose(hidden_outputs))
self.__weight1 += self.__learning_rate * np.dot((hidden_errors * active_f_g(hidden_inputs)),
(np.transpose(inputs)))
def predict(self, inputs_list):
#预测过程只需要进行一个前向传播即可
inputs = np.array(inputs_list, ndmin=2).T
hidden_inputs = np.dot(self.__weight1, inputs)
hidden_outputs = active_f(hidden_inputs)
final_inputs = np.dot(self.__weight2, hidden_outputs)
final_outputs = active_f(final_inputs)
return final_outputs
```
主函数训练与预测过程如下:
```python
my_net = BP(784, num_hidden, 10, 0.1)
for _ in range(10):
for i in range(len(train_images)):
#调用train进行训练
my_net.train(train_images[i].reshape(784) / 255 * 0.99 + 0.01, train_labels[i])
acc = []
for i in range(len(test_images)):
label = np.argmax(my_net.predict(test_images[i].reshape(784) / 255 * 0.99 + 0.01))
#选取概率最大的数字为输出,若和标签相同则预测成功
if test_labels[i][label] == 0.99:
acc.append(1)
else:
acc.append(0)
# 计算平均准确率
accs.append(np.array(acc).mean())
print("acc is ", np.array(acc).mean(), "when hidden num is ", num_hidden)
```
### 2. CNN 卷积神经网络
这次实验我使用的是 `Pytorh` 的框架,CNN 网络结构如图:
```python
class my_CNN(torch.nn.Module):
def __init__(self):
super(my_CNN, self).__init__()
self.conv1 = torch.nn.Sequential(
# 1通道进 32出 卷积核大小为3*3 卷积核步长1*1
torch.nn.Conv2d(1,
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
构造一个三层的 BP 神经网络和一个卷积神经网络,完成手写 0-9 数字的识别: 设计网络的结构,比如层数,每层的神经元数,单个神经元的输入输出函数; 根据数字识别的任务,设计网络的输入和输出; 实现 BP 网络的错误反传算法,完成神经网络的训练和测试,最终识别率达到 70% 以上; 数字识别训练集可以自己手工制作,也可以网上下载,要求具有可视化图形界面,能够输入输出。 5.进一步的,用卷积神经网络实现以上任务,对比深度学习与浅层模型。 详细介绍参考:https://blog.csdn.net/sheziqiong/article/details/132654308
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PDF.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GZ.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GZ.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GZ.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GZ.png)
共 34 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
shejizuopin
- 粉丝: 1w+
- 资源: 1294
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)