MINST手写数字识别

MINST手写数字识别

import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib
import os
LS =[]
lr = 0.001
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2'
(x,y),(t_x,t_y)= tf.keras.datasets.mnist.load_data()
x = tf.convert_to_tensor(x,dtype=tf.float32)/255
x = tf.reshape(x,shape=(-1,28*28))
y = tf.one_hot(y,depth=10)
slice = tf.data.Dataset.from_tensor_slices((x,y)).shuffle(x.shape[0]).batch(128)
w1 = tf.Variable(tf.random.truncated_normal(shape=(784,256),stddev=0.1,dtype=tf.float32))
b1 = tf.Variable(tf.zeros(shape=(256),dtype=tf.float32))
w2 = tf.Variable(tf.random.truncated_normal(shape=(256,128),stddev=0.1,dtype=tf.float32))
b2 = tf.Variable(tf.zeros(shape=(128),dtype=tf.float32))
w3 = tf.Variable(tf.random.truncated_normal(shape=(128,10),stddev=0.1,dtype=tf.float32))
b3 = tf.Variable(tf.zeros(shape=(10),dtype=tf.float32))
for step in range(20):
    for (x,y) in slice:
        with tf.GradientTape() as Tape:
            h1 = x@w1+b1
            h1 = tf.nn.relu(h1)
            h2 = h1@w2+b2
            h2 = tf.nn.relu(h2)
            out = h2@w3+b3
            # 使用交叉熵
            loss = tf.losses.categorical_crossentropy(y,out,from_logits=True)
            # 使用均方差
            # loss = tf.square(y - out)
            # loss = tf.reduce_mean(loss)
        gradient = Tape.gradient(loss,[w1,b1,w2,b2,w3,b3])
        loss = tf.reduce_mean(loss)
        w1.assign_sub(lr*gradient[0])
        b1.assign_sub(lr*gradient[1])
        w2.assign_sub(lr*gradient[2])
        b2.assign_sub(lr*gradient[3])
        w3.assign_sub(lr*gradient[4])
        b3.assign_sub(lr*gradient[5])
        LS.append(loss)
    print(step,"    Loss:",loss)
Y = tf.range(len(LS))
plt.plot(Y,LS,'g^')
plt.ylabel("Loss")
plt.xlabel('Times')
plt.show()
def test(X):
    h1 = X @ w1 + b1
    h1 = tf.nn.relu(h1)
    h2 = h1 @ w2 + b2
    h2 = tf.nn.relu(h2)
    out = h2 @ w3 + b3
    return out
n=5
figure,ax = plt.subplots(n,8)
for i in range(n):
    for u in range(0,8,2):
        ax[i][u].imshow(t_x[i*5+u])
        F = t_x[i*5+u]
        F = F.reshape((-1,28*28))/255
        F = test(F)
        print(F)
        F = tf.one_hot(tf.argmax(F,axis=-1),depth=10)[0]
        ax[i][u+1].bar(['0','1','2','3','4','5','6','7','8','9'],F)
good=0
plt.show()
t_x = tf.convert_to_tensor(t_x,dtype=tf.float32)/255
t_x = tf.reshape(t_x,shape=(-1,28*28))
t_y = tf.one_hot(t_y,depth=10)
T = tf.data.Dataset.from_tensor_slices((t_x,t_y)).shuffle(t_x.shape[0]).batch(1)
for (x,y) in T:
    if(tf.argmax(test(x),axis=-1)[0].numpy()==tf.argmax(y,axis=-1)[0].numpy()):
        good+=1
print("准确率:",good/t_x.shape[0]*100)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值