学习
load_mnist(flatten=True, normalize=False)flatten=true
读入的图像一维numpy数组的形式保存
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)

28*28像素的灰度图像(1像素)
输入层的神经元数量?
输出层的神经元数量?
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表示权重矩阵
识别精度Accuracy
能在多大程度上正确分类
该函数用来读入保存在pickle文件中学习到的权重参数
在这个文件中以字典变量的形式保存了权重和偏置参数
def init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network
predict()函数用来进行分类
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%的数据被正确分类了
neural 神经元