import os
import lr as lr
import tensorflow as tf
from pyspark.sql.functions import stddev
from tensorflow.keras import datasets
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' #只列印error的資訊
(x,y),_=datasets.mnist.load_data()
#x: [60k,28,28]
#y: [60k]
x=tf.convert_to_tensor(x,dtype=tf.float32)/255 #使x的值從0~255降到0~1
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))
train_db=tf.data.Dataset.from_tensor_slices((x,y)).batch(100) #每次從60k中取100張
train_iter=iter(train_db) #迭代器
sample=next(train_iter)
print('batch:',sample[0].shape,sample[1].shape)
#[b,784]=>[b,256]=>[b,128]=>[b,10]
#[dim_in,dim_out],[dim_out]
w1=tf.Variable(tf.random.truncated_normal([784,256],stddev=0.1)) #防止梯度爆炸,需要設定均值和方差的范圍,原來是均值為0,方差為1,現在設定方差為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]))
#h1=x@w1+b1 x指的是之前的一個batch,100個28*28的圖片
for epoch in range(10): #對整個資料集進行10次迭代
for step,(x,y) in enumerate(train_db): # x:[100,28,28] y:[100] 對每個batch進行,整體進度
x=tf.reshape(x,[-1,28*28]) #[b,28,28]=>[b,28*28] 維度變換
with tf.GradientTape() as tape: #tf.Variable
h1 = x @ w1 + b1 # [b,784]@[784,256]+[256]=>[b,256]
h1 = tf.nn.relu(h1) # 加入非線性因素
h2 = h1 @ w2 + b2 # [b,256]@[256,128]+[128]=>[b,128]
h2 = tf.nn.relu(h2)
out = h2 @ w3 + b3 # [b,128]@[128,10]+[10]=>[b,10] 前項計算結束
# compute loss
# out:[b,10]
# y:[b]=>[b,10]
y_onehot = tf.one_hot(y, depth=10) #將y one_hot編碼為長度為10的一維陣列,好與x*w+b的[b,10]進行相減誤差運算
# mes=mean(sum(y_onehot-out)^2)
loss = tf.square(y_onehot - out)
# mean:scalar
loss = tf.reduce_mean(loss) #求均值,就是計算100張圖片的平均誤差
#compute gradient
grads=tape.gradient(loss,[w1,b1,w2,b2,w3,b3]) #loss函式中隊w1,b1,w2,b2,w3,b3求導
# print(grads)
#w1=w1-lr*w1_grad 求下一個w1,梯度下降演算法
# w1 = w1 - lr * grads[0] #tf.Variable相減之后還是tf.tensor,需要原地更新
# b1 = b1 - lr * grads[1]
# w2 = w2 - lr * grads[2]
# b2 = b2 - lr * grads[3]
# w3 = w3 - lr * grads[4]
# b3 = b3 - lr * grads[5]
lr = 1e-3 #0.001
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])
# print(isinstance(b3, tf.Variable))
# print(isinstance(b3, tf.Tensor))
if step%100==0: #每進行100個batch輸出一次
print(epoch,step,loss,float(loss))
#本次學習也算是繼續理解線性回歸模型,mnist影像識別的學習,識訓還是很不錯的,不過還有一些知識希望在之后的學習中進行計算理解,還挺開心的學這個東西,挺有意思的哈哈,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/528811.html
標籤:其他
上一篇:基于docker和cri-dockerd部署kubernetes
下一篇:回溯
