# coding: utf-8
import sys, os
sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定
import numpy as np
import pickle
from dataset.mnist import load_mnist ##导入mnist手写体识别
from common.functions import sigmoid, softmax
##获取手写数字识别的测试集合x_test.shape=1000*784,t_test.shape=10000
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
##加载pkl参数,是一个已经训练好的,里面带w1,w2,w3,b1,b2,b3的一个dict
def init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network ##
def new_init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network ##return dict
##预测数字分类,核心在这个地方
##维度w1=784*50,w2=50*100,w3=100*10,b1=1*50,b2=1*100,b3=1*10
#也就是连个隐藏层,一个输出层,10表示输出类别是10,一层50个隐藏节点,二测100个
#预测的过程就是矩阵计算的过程激活函数是sigmoid
#为什么np.dot(x,w1),x要写在w1前面?因为要保证矩阵能够计算x=xxx行*784,w1必须是784行,隐藏节点个#数可以随便设置,这里默认给了50。a1 = np.dot(x, w1) + b1,加b1,有个numpy的广播运算,计算结果##的每一行都加b1,就这么一层套一层,总共3层(加上输出层)最后把y算出来了,也就是
##最后 return y ,输出的是一个10个概率的行向量,加起来等于一,最大概率就是输出的图片分的那个类
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()
print("x.shape:", x.shape)
print("t.shape", t.shape)
network = init_network()
print("network['W1'].shape:",(network['W1'].shape))
print("network['W2'].shape:",(network['W2'].shape))
print("network['W3'].shape:",(network['W3'].shape))
# print("network:",network)
batch_size = 100 # 批数量,一次去100张图片,每一行表示一张图片
accuracy_cnt = 0
for i in range(0, len(x), batch_size):
x_batch = x[i:i+batch_size]#100*784
#一次计算出来y_batch.shape = 100*784
y_batch = predict(network, x_batch) #[100*784] * [784 *50] * [50 * 100] * [100, 10]
#找到列中最大的那个只的索引维度是100*1,
p = np.argmax(y_batch, axis=1)
#与测试集中100个对比,正确的累加起来,巡皇10000/100=100次后得到正确的累计
accuracy_cnt += np.sum(p == t[i:i+batch_size])
#最后正确的累加/10000就是正确率了
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
# coding: utf-8
import sys, os
sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定
import numpy as np
import pickle
from dataset.mnist import load_mnist ##导入mnist手写体识别
from common.functions import sigmoid, softmax
##获取手写数字识别的测试集合x_test.shape=1000*784,t_test.shape=10000
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
##加载pkl参数,是一个已经训练好的,里面带w1,w2,w3,b1,b2,b3的一个dict
def init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network ##
def new_init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network ##return dict
##预测数字分类,核心在这个地方
##维度w1=784*50,w2=50*100,w3=100*10,b1=1*50,b2=1*100,b3=1*10
#也就是连个隐藏层,一个输出层,10表示输出类别是10,一层50个隐藏节点,二测100个
#预测的过程就是矩阵计算的过程激活函数是sigmoid
#为什么np.dot(x,w1),x要写在w1前面?因为要保证矩阵能够计算x=xxx行*784,w1必须是784行,隐藏节点个#数可以随便设置,这里默认给了50。a1 = np.dot(x, w1) + b1,加b1,有个numpy的广播运算,计算结果##的每一行都加b1,就这么一层套一层,总共3层(加上输出层)最后把y算出来了,也就是
##最后 return y ,输出的是一个10个概率的行向量,加起来等于一,最大概率就是输出的图片分的那个类
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()
print("x.shape:", x.shape)
print("t.shape", t.shape)
network = init_network()
print("network['W1'].shape:",(network['W1'].shape))
print("network['W2'].shape:",(network['W2'].shape))
print("network['W3'].shape:",(network['W3'].shape))
# print("network:",network)
batch_size = 100 # 批数量,一次去100张图片,每一行表示一张图片
accuracy_cnt = 0
for i in range(0, len(x), batch_size):
x_batch = x[i:i+batch_size]#100*784
#一次计算出来y_batch.shape = 100*784
y_batch = predict(network, x_batch) #[100*784] * [784 *50] * [50 * 100] * [100, 10]
#找到列中最大的那个只的索引维度是100*1,
p = np.argmax(y_batch, axis=1)
#与测试集中100个对比,正确的累加起来,巡皇10000/100=100次后得到正确的累计
accuracy_cnt += np.sum(p == t[i:i+batch_size])
#最后正确的累加/10000就是正确率了
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
# coding: utf-8
import sys, os
sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定
import numpy as np
import pickle
from dataset.mnist import load_mnist ##导入mnist手写体识别
from common.functions import sigmoid, softmax
##获取手写数字识别的测试集合x_test.shape=1000*784,t_test.shape=10000
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
##加载pkl参数,是一个已经训练好的,里面带w1,w2,w3,b1,b2,b3的一个dict
def init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network ##
def new_init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network ##return dict
##预测数字分类,核心在这个地方
##维度w1=784*50,w2=50*100,w3=100*10,b1=1*50,b2=1*100,b3=1*10
#也就是连个隐藏层,一个输出层,10表示输出类别是10,一层50个隐藏节点,二测100个
#预测的过程就是矩阵计算的过程激活函数是sigmoid
#为什么np.dot(x,w1),x要写在w1前面?因为要保证矩阵能够计算x=xxx行*784,w1必须是784行,隐藏节点个#数可以随便设置,这里默认给了50。a1 = np.dot(x, w1) + b1,加b1,有个numpy的广播运算,计算结果##的每一行都加b1,就这么一层套一层,总共3层(加上输出层)最后把y算出来了,也就是
##最后 return y ,输出的是一个10个概率的行向量,加起来等于一,最大概率就是输出的图片分的那个类
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()
print("x.shape:", x.shape)
print("t.shape", t.shape)
network = init_network()
print("network['W1'].shape:",(network['W1'].shape))
print("network['W2'].shape:",(network['W2'].shape))
print("network['W3'].shape:",(network['W3'].shape))
# print("network:",network)
batch_size = 100 # 批数量,一次去100张图片,每一行表示一张图片
accuracy_cnt = 0
for i in range(0, len(x), batch_size):
x_batch = x[i:i+batch_size]#100*784
#一次计算出来y_batch.shape = 100*784
y_batch = predict(network, x_batch) #[100*784] * [784 *50] * [50 * 100] * [100, 10]
#找到列中最大的那个只的索引维度是100*1,
p = np.argmax(y_batch, axis=1)
#与测试集中100个对比,正确的累加起来,巡皇10000/100=100次后得到正确的累计
accuracy_cnt += np.sum(p == t[i:i+batch_size])
#最后正确的累加/10000就是正确率了
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))