深度学习入门(鱼书)学习笔记:第3章 神经网络

目录导航

第3章 神经网络

3.1 从感知机到神经网络

3.2 激活函数

sigmoid函数:

\[h(x)=\frac{1}{1+\exp(-x)} \]

ReLU(Rectified Linear Unit)函数:

\[h(x)= \begin{cases} x, & (x>0) \\ 0, &(x \le 0) \end{cases} \]

3.3 多维数组运算

3.4 3层神经网络的实现

3.5 输出层设计

softmax函数:

\[y_k=\frac{\exp (a_k)}{\sum_{i=1}^n \exp (a_i)} \]

3.6 手写数字识别

ch03/mnist_show.py的实现:

点击mnist_show.py代码
# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 为了导入父目录的文件而进行的设定
import numpy as np
from dataset_all.mnist import load_mnist
from PIL import Image


def img_show(img):
    pil_img = Image.fromarray(np.uint8(img))
    pil_img.show()


(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)

index = 0
img = x_train[index]
label = t_train[index]
print(label)  # 5

print(img.shape)  # (784,)
img = img.reshape(28, 28)  # 把图像的形状变为原来的尺寸
print(img.shape)  # (28, 28)


if __name__ == '__main__':
    img_show(img)

运行ch03/mnist_show.py的结果(此处使用Python3.7版本)

点击查看运行输出
"D:\Program Files\Python\Python37\python.exe" D:/Code/CodePython/02_dl_from_scratch_demo/ch03/mnist_show.py
Converting train-images-idx3-ubyte.gz to NumPy Array ...
Done
Converting train-labels-idx1-ubyte.gz to NumPy Array ...
Done
Converting t10k-images-idx3-ubyte.gz to NumPy Array ...
Done
Converting t10k-labels-idx1-ubyte.gz to NumPy Array ...
Done
Creating pickle file ...
Done!
0
(784,)
(28, 28)

Process finished with exit code 0

image

目录dataset_all里会保存一个mnist.pkl文件。注意保存和读取pkl文件的Python版本必须保持一致,否则会报错。比如,在一台电脑上使用Python3.7生成并保存pkl文件,而在另一台电脑上使用Python3.8读取pkl文件就会报错。一个解决办法就是不把pkl文件添加到git中。
image

3.7 小结

本章介绍了神经网络的前向传播。本章介绍的神经网络和上一章的感知机在信号的按层传递这一点上是相同的,但是,向下一个神经元发送信号时,改变信号的激活函数有很大差异。神经网络中使用的是平滑变化的sigmoid函数,而感知机中使用的是信号急剧变化的阶跃函数。

posted @ 2022-06-01 21:20  萧驭  阅读(381)  评论(0)    收藏  举报