一、机器学习问题的求解步骤
学习
- 使用训练数据进行权重参数的学习
推理 - 使用学习到的参数对输入的数据进行分类
二、MNIST数据集
2.1 load_mnist(flatten=True, normalize=False)
flatten=true
读入的图像一维numpy数组的形式保存
2.2 函数学习
def fromarray(obj, mode=None):
Then this can be used to convert it to a Pillow image::
im = Image.fromarray(a)
使用代码读入MINIST数据集
# coding: utf-8
import sys, os
sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定
import numpy as np
from dataset.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)
img = x_train[0]
label = t_train[0]
print(label) # 5
print(img.shape) # (784,)
img = img.reshape(28, 28) # 把图像的形状变为原来的尺寸
print(img.shape) # (28, 28)
img_show(img)
三、关于MNIST图像数据
28*28像素的灰度图像(1像素)
- 每个像素的取值都是0-255
- 图像数据都标有7,2,1标签
3.1 神经网络的推理处理
输入层的神经元数量?
- 28*28=784个神经元
输出层的神经元数量?
- 神经网络识别的结果0-9,共10种类别,所以需要10个神经元
2个隐藏层
- 每个隐藏层中的神经元的数量可以任意设置
我们用下面这3个函数来实现神经网络的推理处理
def get_data():
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
return x_test, t_test
def init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network
def predict(network, x):
W1, W2, W3 = network['W1'], network['W2'], network['W3']
b1, b2, b3 = network['b1'], network['b2'], network['b3']
a1 = np.dot(x, W1) + b1
z1 = sigmoid(a1)
a2 = np.dot(z1, W2) + b2
z2 = sigmoid(a2)
a3 = np.dot(z2, W3) + b3
y = softmax(a3)
return y
x表示权重矩阵
- predict 表示前向传播
3.2 评价指标
识别精度Accuracy
能在多大程度上正确分类
3.3 具体函数意义解释
该函数用来读入保存在pickle文件中学习到的权重参数
在这个文件中以字典变量的形式保存了权重和偏置参数
def init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network
predict()函数用来进行分类
- 以numpy数组的形式输出各个标签对应的概率
3.4 关于函数参数
normalize
- 正规化(把数据限定到某个范围)
函数内部会进行转换,将图像的各个像素值除以255
- 使得数据的值在0到1之内
五、预处理
对神经网络中的输入数据进行某种既定的转换
代码执行结果
D:\ANACONDA\envs\pytorch\python.exe C:/Users/Administrator/Desktop/DeepLearning/ch03/neuralnet_mnist.py
Accuracy:0.9352
Process finished with exit code 0
这里表示有93%的数据被正确分类了
- 我们打算把精度提高到99%以上才算好
六、专业英语
neural 神经元