Minst初探_计算准确度


import numpy as np
import tensorflow as tf
import sys,os
sys.path.append('E:\\Deeplearning\\MnistDemo')
import pickle
mnist = tf.keras.datasets.mnist

def init_network():#打开权重文件,获取神经网络
with open("E:\\Deeplearning\\MnistDemo\\sample_weight.pkl",'rb') as f:
network=pickle.load(f)
return network
def Sigmod(x):#设定sigmod激活函数
return 1/(1+np.exp(-x))
def forward(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=Sigmod(a1)
a2=np.dot(z1,W2)+b2
z2=Sigmod(a2)
a3=np.dot(z2,W3)+b3
y=softmax(a3)
return y
def softmax(a):#输出端激活函数
exp_a=np.exp(a)
sum_exp_a=np.sum(exp_a)
y=exp_a/sum_exp_a
return y

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x,t=np.reshape(x_test,(10000,784)),y_test
network=init_network()
accuracy_cnt=0
for i in range(len(x)):
y=forward(network,x[i])
p=np.argmax(y)
if p==t[i]:accuracy_cnt+=1
else:
print("你他娘的算错了,实际是%d,你算了%d"%(t[i],p))
print("准确度:"+str(float(accuracy_cnt)/len(x)))

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注