import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets
import os
调整输出信息,只输出error
os.environ[‘TF_CPP_MIN_LOG_LEVEL’]=‘2’
获取数据
(x,y),_=datasets.mnist.load_data()
把x值从0-255调整为0-1 把np转换为tensor
x=tf.convert_to_tensor(x,dtype=tf.float32)/255.
y=tf.convert_to_tensor(y,dtype=tf.int32)
print(x.shape,y.shape,x.dtype,y.dtype)
print(tf.reduce_min(x),tf.reduce_max(x))
print(tf.reduce_min(y),tf.reduce_max(y))
设置一批数据为128
train_db=tf.data.Dataset.from_tensor_slices((x,y)).batch(128)
train_iter=iter(train_db)
sample=next(train_iter)
print(‘batch:’,sample[0].shape,sample[1].shape)
定义参数与学习率
w1=tf.Variable(tf.random.truncated_normal([784,256],stddev=0.1))
b1=tf.Variable(tf.zeros([256]))
w2=tf.Variable(tf.random.truncated_normal([256,128],stddev=0.1))
b2=tf.Variable(tf.zeros([128]))
w3=tf.Variable(tf.random.truncated_normal([128,10],stddev=0.1))
b3=tf.Variable(tf.zeros([10]))
lr=1e-3
循环数据集10次
for epoch in range(10):
#分批循环数据集
for step,(x,y) in enumerate(train_db):
#把X转换为[batch,784]
x=tf.reshape(x,[-1,28*28])
#tensor提供的自动求导
with tf.GradientTape() as tape:
#x[128,784]->[128,256]
h1=x@w1+tf.broadcast_to(b1,[x.shape[0],256])
#非线性激励
h1=tf.nn.relu(h1)
#x[128,256]->[128,128]
h2=h1@w2+b2
h2=tf.nn.relu(h2)
#输出[128,10]
out=h2@w3+b3
#对y进行one_hot转变为[128,10]
y_onehot=tf.one_hot(y,depth=10)
#损失函数:(y-y')**2/128
loss=tf.square(y_onehot-out)
loss=tf.reduce_mean(loss)
#传入损失函数,参数
grads=tape.gradient(loss,[w1,b1,w2,b2,w3,b3])
#更新参数
w1.assign_sub(lr*grads[0])
b1.assign_sub(lr*grads[1])
w2.assign_sub(lr*grads[2])
b2.assign_sub(lr*grads[3])
w3.assign_sub(lr*grads[4])
b3.assign_sub(lr*grads[5])
#输出损失值
if step%100==0:
print(epoch,step,'loss',float(loss))