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)