#囚徒 mnist应用
import sys, os
sys.path.append(r"E:\my tools\vscode_subject\vscode\python\cnn\deep_network") # 为了导入父目录的文件而进行的设定
import numpy as np
import pickle
from dataset.mnist import load_mnist
from common.functions import sigmoid, softmax
sample_w_path=r"E:\my tools\vscode_subject\vscode\python\cnn\deep_network\ch03\sample_weight.pkl"
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_w_path, 'rb') as f: #读取权重参数 sample_weight.pkl
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, t = get_data()
network = init_network()
batch_stemp=100 #批处理大小
accuracy_cnt = 0
for i in range(0,len(x),batch_stemp):
x_batch=x[i:i+batch_stemp]
t_batch=t[i:i+batch_stemp]
y = predict(network, x_batch)
p= np.argmax(y,axis=1) # 获取概率最高的元素的索引 按照行来寻找(每行的第几个,其实就是他的类别)
accuracy_cnt+=np.sum(p==t_batch) #累加每次批处理后的正确值个数
print("x {}".format(t[0:10]))
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
标签:network,batch,stemp,囚徒,np,import,mnist,4.0
From: https://www.cnblogs.com/qt-pyq/p/17840991.html