照着书上敲的
import os, sys
sys.path.append(os.pardir)
""""
sys.path.append
sys.path.append(os.pardir)
python程序中使用 import XXX 时,python解析器会在当前目录、
已安装和第三方模块中搜索 xxx,如果都搜索不到就会报错。
mnist_show.py文件的当前工作目录为ch03,
但是load_mnist()函数的mnist.py文件在dataset目录下。
因此,mnist_show.py文件不可以跨文件直接导入mnist.py文件。
sys.path.append(os.pardir)语句实际上是把
父目录deep-learning-from-scatch加入到
sys.path(Python 的搜索目录模块的路径集中),
从而可以导入deep-learning-from-scatch下的任何目录(包括dataset目录)中的任何文件。
"""
from dataset.mnist import load_mnist
(x_train,t_train),(x_test,t_test) = load_mnist(flatten=True,normalize=False) #flatten是展开成一维数组,原图像28X28 展开后是 一维数组(784)个数据
#训练图像 ,训练标签 ,测试图像,测试标签
print(x_train.shape) #矩阵是 (60000, 784) 60000张图片,每一行是一张图片的信息。
print(t_train.shape)
print(x_test.shape)
print(t_test.shape)
"""
(60000, 784)
(60000,)
(10000, 784)
(10000,)
"""
完整版:
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)
print(img.shape)
img = img.reshape(28,28)
print(img.shape)
img_show(img)
利用神经网络测试正确率:
get_data 是获取数据
init_network 是用现成的(训练好的)网络模型
predict是统计成功率
import os,sys
sys.path.append(os.pardir)
import numpy as np
from PIL import Image
import pickle
from dataset.mnist import load_mnist
from common.functions import sigmoid,softmax
def get_data():
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=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 = sigmoid(a3)
return y # y可以认为是概率矩阵
x,t = get_data()
network = init_network()
accuracy_cnt = 0
for i in range(len(x)):
y = predict(network,x[i])
p = np.argmax(y)
if p == t[i]:
accuracy_cnt +=1
print("Accuracy:"+str(float(accuracy_cnt)/len(x)))
上个for是一个个的检测,有点慢,采用批处理的形式会快一些。可以改成下面的形式。
batch_size = 100
for i in range(0,len(x),batch_size):
x_batch = x[i:i+batch_size]
y_batch = predict(network,x_batch)
p = np.argmax(y_batch,axis=1)
accuracy_cnt += np.sum( p == t[i:i+batch_size])
'''
>>> x = np.array([[0.1, 0.8, 0.1], [0.3, 0.1, 0.6],
... [0.2, 0.5, 0.3], [0.8, 0.1, 0.1]])
>>> y = np.argmax(x, axis=1)
>>> print(y)
[1 2 1 0]
'''
print("Accuracy:"+str(float(accuracy_cnt)/len(x)))
替换for循环之后的完整代码:
import os,sys
sys.path.append(os.pardir)
import numpy as np
from PIL import Image
import pickle
from dataset.mnist import load_mnist
from common.functions import sigmoid,softmax
def get_data():
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=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): # x是要训练的数据,network是现成的网络 把现成的参数(偏执和权重)取过来
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 = sigmoid(a3) # 做一次非线性变换
return y # y可以认为是概率矩阵
x,t = get_data()
network = init_network()
accuracy_cnt = 0
'''
for i in range(len(x)):
y = predict(network,x[i])
p = np.argmax(y)
if p == t[i]:
accuracy_cnt +=1
'''
batch_size = 100 #batch 是批处理的意思
for i in range(0,len(x),batch_size):
x_batch = x[i:i+batch_size]
y_batch = predict(network,x_batch)
p = np.argmax(y_batch,axis=1)
accuracy_cnt += np.sum( p == t[i:i+batch_size])
'''
>>> x = np.array([[0.1, 0.8, 0.1], [0.3, 0.1, 0.6],
... [0.2, 0.5, 0.3], [0.8, 0.1, 0.1]])
>>> y = np.argmax(x, axis=1)
>>> print(y)
[1 2 1 0]
'''
print("Accuracy:"+str(float(accuracy_cnt)/len(x)))