机器学习(六)——优化器
生活随笔
收集整理的這篇文章主要介紹了
机器学习(六)——优化器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
優(yōu)化器是引導(dǎo)神經(jīng)網(wǎng)絡(luò)更新參數(shù)的工具
鳶尾花分類的各種優(yōu)化器實(shí)現(xiàn)(只有優(yōu)化器,更新參數(shù)的部分不同)
1、SGD優(yōu)化器
from sklearn import datasets import tensorflow as tf import numpy as np from matplotlib import pyplot as plt import time ##記錄時(shí)間 #步驟 ###準(zhǔn)備數(shù)據(jù) # 數(shù)據(jù)讀入 # 這個(gè)就是sgd優(yōu)化器的案例 x_data = datasets.load_iris().data##加載數(shù)據(jù)集所有特征 y_data = datasets.load_iris().target##加載數(shù)據(jù)集所有標(biāo)簽 # 數(shù)據(jù)集亂序 np.random.seed(116)#使用相同的seed,使輸入特征/標(biāo)簽一一對(duì)應(yīng) np.random.shuffle(x_data) np.random.seed(116) np.random.shuffle(y_data) tf.random.set_seed(116) # 生成訓(xùn)練集和測(cè)試集 數(shù)據(jù)總量150,訓(xùn)練:測(cè)試一般 4:1 x_train = x_data[:-30] y_train = y_data[:-30] x_test = x_data[-30:] y_test = y_data[-30:] # 配成(輸入特征,標(biāo)簽)對(duì),每次讀入一小批(batch) train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32) test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) ##tf.data.Dataset.from_tensor_slices()的作用是使輸入標(biāo)簽配對(duì)打包###搭建神經(jīng)網(wǎng)絡(luò) # 定義神經(jīng)網(wǎng)絡(luò)中的可訓(xùn)練參數(shù) #生成神經(jīng)網(wǎng)絡(luò)參數(shù),4個(gè)輸入特征,所以輸入層為4個(gè)輸入節(jié)點(diǎn),因?yàn)槭欠殖?類,所以輸出層為3個(gè)神經(jīng)元 w1 = tf.Variable(tf.random.truncated_normal([4,3], stddev=0.1, seed=1)) b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))lr = 0.1#學(xué)習(xí)率,不能太大或太小 train_loss_results = []#記錄每輪loss test_acc = []#將每輪的acc記錄下來(lái) epoch = 500#循環(huán)次數(shù) loss_all = 0#記錄每輪四個(gè)step的loss和 ###參數(shù)優(yōu)化 # 嵌套循環(huán)迭代,with結(jié)構(gòu)更新參數(shù),顯示當(dāng)前l(fā)oss now_time = time.time()###記錄起始時(shí)間 for epoch in range(epoch):for step, (x_train, y_train) in enumerate(train_db):with tf.GradientTape() as tape:x_train = tf.cast(x_train, dtype=w1.dtype)y = tf.matmul(x_train, w1) + b1#神經(jīng)網(wǎng)絡(luò)乘加運(yùn)算y = tf.nn.softmax(y)#使y結(jié)果符合概率分布 與獨(dú)熱碼求lossy_ = tf.one_hot(y_train, depth = 3)#將標(biāo)簽轉(zhuǎn)為獨(dú)熱碼,方便計(jì)算lossy_ = tf.cast(y_, dtype=y.dtype)loss = tf.reduce_mean(tf.square(y_-y))#采用均方誤差損失函數(shù)loss_all += loss.numpy() #加loss累加,后面求均值grads = tape.gradient(loss, [w1, b1])#實(shí)現(xiàn)梯度更新 w1 = w1 - lr * w1_grad b = b1 - lr * b_gradw1.assign_sub(lr * grads[0])#參數(shù)w1自更新b1.assign_sub(lr * grads[1])#參數(shù)b自更新#每個(gè)epoech打印loss信息print("Epoch{},loss:{}".format(epoch, loss_all/4))train_loss_results.append(loss_all / 4)#記錄四個(gè)step的loss平均值loss_all = 0#歸0,為下一次做準(zhǔn)備 ###測(cè)試效果 # 計(jì)算當(dāng)前參數(shù)向后傳播的準(zhǔn)確率,顯示當(dāng)前的acctotal_correct, total_number = 0, 0for x_test, y_test in test_db:x_test = tf.cast(x_test, dtype=w1.dtype)#使用訓(xùn)練得到的參數(shù)進(jìn)行預(yù)測(cè)y = tf.matmul(x_test, w1) + b1y = tf.nn.softmax(y)pred = tf.argmax(y, axis=1)#返回最大值,即預(yù)測(cè)到的值#將pred轉(zhuǎn)換為y_test類型pred = tf.cast(pred, dtype=y_test.dtype)#將比較結(jié)果的布爾型轉(zhuǎn)換為int型correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)correct = tf.reduce_sum(correct)#如果分類正確則+1total_correct += int(correct)#累加,方便后面求正確率total_number += x_test.shape[0]#測(cè)試總樣本數(shù)acc = total_correct / total_numbertest_acc.append(acc)print("Test_acc:",acc)print("----------------------------") total_time = time.time() - now_time print("total_time", total_time)##記錄訓(xùn)練時(shí)間 ##繪制loss曲線方便觀察 plt.title("Loss Function Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('loss')#y軸變量名 plt.plot(train_loss_results, label="$Loss$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像##繪制acc曲線方便觀察 plt.title("Acc Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('Acc')#y軸變量名 plt.plot(test_acc, label="$Accuracy$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像2、SGDM優(yōu)化器
from sklearn import datasets import tensorflow as tf import numpy as np from matplotlib import pyplot as plt import time ##記錄時(shí)間 #步驟 ###準(zhǔn)備數(shù)據(jù) # 數(shù)據(jù)讀入 # 這個(gè)就是sgd優(yōu)化器的案例 x_data = datasets.load_iris().data##加載數(shù)據(jù)集所有特征 y_data = datasets.load_iris().target##加載數(shù)據(jù)集所有標(biāo)簽 # 數(shù)據(jù)集亂序 np.random.seed(116)#使用相同的seed,使輸入特征/標(biāo)簽一一對(duì)應(yīng) np.random.shuffle(x_data) np.random.seed(116) np.random.shuffle(y_data) tf.random.set_seed(116) # 生成訓(xùn)練集和測(cè)試集 數(shù)據(jù)總量150,訓(xùn)練:測(cè)試一般 4:1 x_train = x_data[:-30] y_train = y_data[:-30] x_test = x_data[-30:] y_test = y_data[-30:] # 配成(輸入特征,標(biāo)簽)對(duì),每次讀入一小批(batch) train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32) test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) ##tf.data.Dataset.from_tensor_slices()的作用是使輸入標(biāo)簽配對(duì)打包###搭建神經(jīng)網(wǎng)絡(luò) # 定義神經(jīng)網(wǎng)絡(luò)中的可訓(xùn)練參數(shù) #生成神經(jīng)網(wǎng)絡(luò)參數(shù),4個(gè)輸入特征,所以輸入層為4個(gè)輸入節(jié)點(diǎn),因?yàn)槭欠殖?類,所以輸出層為3個(gè)神經(jīng)元 w1 = tf.Variable(tf.random.truncated_normal([4,3], stddev=0.1, seed=1)) b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))lr = 0.1#學(xué)習(xí)率,不能太大或太小 train_loss_results = []#記錄每輪loss test_acc = []#將每輪的acc記錄下來(lái) epoch = 500#循環(huán)次數(shù) loss_all = 0#記錄每輪四個(gè)step的loss和 ###參數(shù)優(yōu)化 # 嵌套循環(huán)迭代,with結(jié)構(gòu)更新參數(shù),顯示當(dāng)前l(fā)oss #######################################################m_w, m_b = 0, 0 beta = 0.9#0.9憑經(jīng)驗(yàn)設(shè)定 #######################################################now_time = time.time()###記錄起始時(shí)間 for epoch in range(epoch):for step, (x_train, y_train) in enumerate(train_db):with tf.GradientTape() as tape:x_train = tf.cast(x_train, dtype=w1.dtype)y = tf.matmul(x_train, w1) + b1#神經(jīng)網(wǎng)絡(luò)乘加運(yùn)算y = tf.nn.softmax(y)#使y結(jié)果符合概率分布 與獨(dú)熱碼求lossy_ = tf.one_hot(y_train, depth = 3)#將標(biāo)簽轉(zhuǎn)為獨(dú)熱碼,方便計(jì)算lossy_ = tf.cast(y_, dtype=y.dtype)loss = tf.reduce_mean(tf.square(y_-y))#采用均方誤差損失函數(shù)loss_all += loss.numpy() #加loss累加,后面求均值grads = tape.gradient(loss, [w1, b1])#實(shí)現(xiàn)梯度更新 w1 = w1 - lr * w1_grad b = b1 - lr * b_grad################################################################sgd-momentun更新優(yōu)化器m_w = beta * m_w + (1 - beta) * grads[0]m_b = beta * m_b + (1 - beta) * grads[1]################################################################w1.assign_sub(lr * m_w)#參數(shù)w1自更新b1.assign_sub(lr * m_b)#參數(shù)b自更新#每個(gè)epoech打印loss信息print("Epoch{},loss:{}".format(epoch, loss_all/4))train_loss_results.append(loss_all / 4)#記錄四個(gè)step的loss平均值loss_all = 0#歸0,為下一次做準(zhǔn)備 ###測(cè)試效果 # 計(jì)算當(dāng)前參數(shù)向后傳播的準(zhǔn)確率,顯示當(dāng)前的acctotal_correct, total_number = 0, 0for x_test, y_test in test_db:x_test = tf.cast(x_test, dtype=w1.dtype)#使用訓(xùn)練得到的參數(shù)進(jìn)行預(yù)測(cè)y = tf.matmul(x_test, w1) + b1y = tf.nn.softmax(y)pred = tf.argmax(y, axis=1)#返回最大值,即預(yù)測(cè)到的值#將pred轉(zhuǎn)換為y_test類型pred = tf.cast(pred, dtype=y_test.dtype)#將比較結(jié)果的布爾型轉(zhuǎn)換為int型correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)correct = tf.reduce_sum(correct)#如果分類正確則+1total_correct += int(correct)#累加,方便后面求正確率total_number += x_test.shape[0]#測(cè)試總樣本數(shù)acc = total_correct / total_numbertest_acc.append(acc)print("Test_acc:",acc)print("----------------------------") total_time = time.time() - now_time print("total_time", total_time)##記錄訓(xùn)練時(shí)間 ##繪制loss曲線方便觀察 plt.title("Loss Function Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('loss')#y軸變量名 plt.plot(train_loss_results, label="$Loss$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像##繪制acc曲線方便觀察 plt.title("Acc Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('Acc')#y軸變量名 plt.plot(test_acc, label="$Accuracy$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像3、Adagrad優(yōu)化器
from sklearn import datasets import tensorflow as tf import numpy as np from matplotlib import pyplot as plt import time ##記錄時(shí)間 #步驟 ###準(zhǔn)備數(shù)據(jù) # 數(shù)據(jù)讀入 # 這個(gè)就是sgd優(yōu)化器的案例 x_data = datasets.load_iris().data##加載數(shù)據(jù)集所有特征 y_data = datasets.load_iris().target##加載數(shù)據(jù)集所有標(biāo)簽 # 數(shù)據(jù)集亂序 np.random.seed(116)#使用相同的seed,使輸入特征/標(biāo)簽一一對(duì)應(yīng) np.random.shuffle(x_data) np.random.seed(116) np.random.shuffle(y_data) tf.random.set_seed(116) # 生成訓(xùn)練集和測(cè)試集 數(shù)據(jù)總量150,訓(xùn)練:測(cè)試一般 4:1 x_train = x_data[:-30] y_train = y_data[:-30] x_test = x_data[-30:] y_test = y_data[-30:] # 配成(輸入特征,標(biāo)簽)對(duì),每次讀入一小批(batch) train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32) test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) ##tf.data.Dataset.from_tensor_slices()的作用是使輸入標(biāo)簽配對(duì)打包###搭建神經(jīng)網(wǎng)絡(luò) # 定義神經(jīng)網(wǎng)絡(luò)中的可訓(xùn)練參數(shù) #生成神經(jīng)網(wǎng)絡(luò)參數(shù),4個(gè)輸入特征,所以輸入層為4個(gè)輸入節(jié)點(diǎn),因?yàn)槭欠殖?類,所以輸出層為3個(gè)神經(jīng)元 w1 = tf.Variable(tf.random.truncated_normal([4,3], stddev=0.1, seed=1)) b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))lr = 0.1#學(xué)習(xí)率,不能太大或太小 train_loss_results = []#記錄每輪loss test_acc = []#將每輪的acc記錄下來(lái) epoch = 500#循環(huán)次數(shù) loss_all = 0#記錄每輪四個(gè)step的loss和 ###參數(shù)優(yōu)化 # 嵌套循環(huán)迭代,with結(jié)構(gòu)更新參數(shù),顯示當(dāng)前l(fā)oss###################################################### v_w, v_b = 0, 0 ####################################################### now_time = time.time()###記錄起始時(shí)間 for epoch in range(epoch):for step, (x_train, y_train) in enumerate(train_db):with tf.GradientTape() as tape:x_train = tf.cast(x_train, dtype=w1.dtype)y = tf.matmul(x_train, w1) + b1#神經(jīng)網(wǎng)絡(luò)乘加運(yùn)算y = tf.nn.softmax(y)#使y結(jié)果符合概率分布 與獨(dú)熱碼求lossy_ = tf.one_hot(y_train, depth = 3)#將標(biāo)簽轉(zhuǎn)為獨(dú)熱碼,方便計(jì)算lossy_ = tf.cast(y_, dtype=y.dtype)loss = tf.reduce_mean(tf.square(y_-y))#采用均方誤差損失函數(shù)loss_all += loss.numpy() #加loss累加,后面求均值grads = tape.gradient(loss, [w1, b1])#########################################################################adagradv_w += tf.square(grads[0])v_b += tf.square(grads[1])w1.assign_sub(lr * grads[0] / tf.sqrt(v_w))b1.assign_sub(lr * grads[1] / tf.sqrt(v_b))#######################################################################每個(gè)epoech打印loss信息print("Epoch{},loss:{}".format(epoch, loss_all/4))train_loss_results.append(loss_all / 4)#記錄四個(gè)step的loss平均值loss_all = 0#歸0,為下一次做準(zhǔn)備 ###測(cè)試效果 # 計(jì)算當(dāng)前參數(shù)向后傳播的準(zhǔn)確率,顯示當(dāng)前的acctotal_correct, total_number = 0, 0for x_test, y_test in test_db:x_test = tf.cast(x_test, dtype=w1.dtype)#使用訓(xùn)練得到的參數(shù)進(jìn)行預(yù)測(cè)y = tf.matmul(x_test, w1) + b1y = tf.nn.softmax(y)pred = tf.argmax(y, axis=1)#返回最大值,即預(yù)測(cè)到的值#將pred轉(zhuǎn)換為y_test類型pred = tf.cast(pred, dtype=y_test.dtype)#將比較結(jié)果的布爾型轉(zhuǎn)換為int型correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)correct = tf.reduce_sum(correct)#如果分類正確則+1total_correct += int(correct)#累加,方便后面求正確率total_number += x_test.shape[0]#測(cè)試總樣本數(shù)acc = total_correct / total_numbertest_acc.append(acc)print("Test_acc:",acc)print("----------------------------") total_time = time.time() - now_time print("total_time", total_time)##記錄訓(xùn)練時(shí)間 ##繪制loss曲線方便觀察 plt.title("Loss Function Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('loss')#y軸變量名 plt.plot(train_loss_results, label="$Loss$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像##繪制acc曲線方便觀察 plt.title("Acc Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('Acc')#y軸變量名 plt.plot(test_acc, label="$Accuracy$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像4、RMSProp優(yōu)化器
from sklearn import datasets import tensorflow as tf import numpy as np from matplotlib import pyplot as plt import time ##記錄時(shí)間 #步驟 ###準(zhǔn)備數(shù)據(jù) # 數(shù)據(jù)讀入 # 這個(gè)就是sgd優(yōu)化器的案例 x_data = datasets.load_iris().data##加載數(shù)據(jù)集所有特征 y_data = datasets.load_iris().target##加載數(shù)據(jù)集所有標(biāo)簽 # 數(shù)據(jù)集亂序 np.random.seed(116)#使用相同的seed,使輸入特征/標(biāo)簽一一對(duì)應(yīng) np.random.shuffle(x_data) np.random.seed(116) np.random.shuffle(y_data) tf.random.set_seed(116) # 生成訓(xùn)練集和測(cè)試集 數(shù)據(jù)總量150,訓(xùn)練:測(cè)試一般 4:1 x_train = x_data[:-30] y_train = y_data[:-30] x_test = x_data[-30:] y_test = y_data[-30:] # 配成(輸入特征,標(biāo)簽)對(duì),每次讀入一小批(batch) train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32) test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) ##tf.data.Dataset.from_tensor_slices()的作用是使輸入標(biāo)簽配對(duì)打包###搭建神經(jīng)網(wǎng)絡(luò) # 定義神經(jīng)網(wǎng)絡(luò)中的可訓(xùn)練參數(shù) #生成神經(jīng)網(wǎng)絡(luò)參數(shù),4個(gè)輸入特征,所以輸入層為4個(gè)輸入節(jié)點(diǎn),因?yàn)槭欠殖?類,所以輸出層為3個(gè)神經(jīng)元 w1 = tf.Variable(tf.random.truncated_normal([4,3], stddev=0.1, seed=1)) b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))lr = 0.1#學(xué)習(xí)率,不能太大或太小 train_loss_results = []#記錄每輪loss test_acc = []#將每輪的acc記錄下來(lái) epoch = 500#循環(huán)次數(shù) loss_all = 0#記錄每輪四個(gè)step的loss和 ###參數(shù)優(yōu)化 # 嵌套循環(huán)迭代,with結(jié)構(gòu)更新參數(shù),顯示當(dāng)前l(fā)oss ################################################# v_w, v_b = 0, 0 beta = 0.9 ################################################# now_time = time.time()###記錄起始時(shí)間 for epoch in range(epoch):for step, (x_train, y_train) in enumerate(train_db):with tf.GradientTape() as tape:x_train = tf.cast(x_train, dtype=w1.dtype)y = tf.matmul(x_train, w1) + b1#神經(jīng)網(wǎng)絡(luò)乘加運(yùn)算y = tf.nn.softmax(y)#使y結(jié)果符合概率分布 與獨(dú)熱碼求lossy_ = tf.one_hot(y_train, depth = 3)#將標(biāo)簽轉(zhuǎn)為獨(dú)熱碼,方便計(jì)算lossy_ = tf.cast(y_, dtype=y.dtype)loss = tf.reduce_mean(tf.square(y_-y))#采用均方誤差損失函數(shù)loss_all += loss.numpy() #加loss累加,后面求均值grads = tape.gradient(loss, [w1, b1])#########################################################v_w = beta * v_w + (1 - beta) * tf.square(grads[0])v_b = beta * v_b + (1 - beta) * tf.square(grads[1])w1.assign_sub(lr * grads[0] / tf.sqrt(v_w))b1.assign_sub(lr * grads[1] / tf.sqrt(v_b))############################################################每個(gè)epoech打印loss信息print("Epoch{},loss:{}".format(epoch, loss_all/4))train_loss_results.append(loss_all / 4)#記錄四個(gè)step的loss平均值loss_all = 0#歸0,為下一次做準(zhǔn)備 ###測(cè)試效果 # 計(jì)算當(dāng)前參數(shù)向后傳播的準(zhǔn)確率,顯示當(dāng)前的acctotal_correct, total_number = 0, 0for x_test, y_test in test_db:x_test = tf.cast(x_test, dtype=w1.dtype)#使用訓(xùn)練得到的參數(shù)進(jìn)行預(yù)測(cè)y = tf.matmul(x_test, w1) + b1y = tf.nn.softmax(y)pred = tf.argmax(y, axis=1)#返回最大值,即預(yù)測(cè)到的值#將pred轉(zhuǎn)換為y_test類型pred = tf.cast(pred, dtype=y_test.dtype)#將比較結(jié)果的布爾型轉(zhuǎn)換為int型correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)correct = tf.reduce_sum(correct)#如果分類正確則+1total_correct += int(correct)#累加,方便后面求正確率total_number += x_test.shape[0]#測(cè)試總樣本數(shù)acc = total_correct / total_numbertest_acc.append(acc)print("Test_acc:",acc)print("----------------------------") total_time = time.time() - now_time print("total_time", total_time)##記錄訓(xùn)練時(shí)間 ##繪制loss曲線方便觀察 plt.title("Loss Function Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('loss')#y軸變量名 plt.plot(train_loss_results, label="$Loss$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像##繪制acc曲線方便觀察 plt.title("Acc Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('Acc')#y軸變量名 plt.plot(test_acc, label="$Accuracy$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像Adam優(yōu)化器
from sklearn import datasets import tensorflow as tf import numpy as np from matplotlib import pyplot as plt import time ##記錄時(shí)間 #步驟 ###準(zhǔn)備數(shù)據(jù) # 數(shù)據(jù)讀入 # 這個(gè)就是sgd優(yōu)化器的案例 x_data = datasets.load_iris().data##加載數(shù)據(jù)集所有特征 y_data = datasets.load_iris().target##加載數(shù)據(jù)集所有標(biāo)簽 # 數(shù)據(jù)集亂序 np.random.seed(116)#使用相同的seed,使輸入特征/標(biāo)簽一一對(duì)應(yīng) np.random.shuffle(x_data) np.random.seed(116) np.random.shuffle(y_data) tf.random.set_seed(116) # 生成訓(xùn)練集和測(cè)試集 數(shù)據(jù)總量150,訓(xùn)練:測(cè)試一般 4:1 x_train = x_data[:-30] y_train = y_data[:-30] x_test = x_data[-30:] y_test = y_data[-30:] # 配成(輸入特征,標(biāo)簽)對(duì),每次讀入一小批(batch) train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32) test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) ##tf.data.Dataset.from_tensor_slices()的作用是使輸入標(biāo)簽配對(duì)打包###搭建神經(jīng)網(wǎng)絡(luò) # 定義神經(jīng)網(wǎng)絡(luò)中的可訓(xùn)練參數(shù) #生成神經(jīng)網(wǎng)絡(luò)參數(shù),4個(gè)輸入特征,所以輸入層為4個(gè)輸入節(jié)點(diǎn),因?yàn)槭欠殖?類,所以輸出層為3個(gè)神經(jīng)元 w1 = tf.Variable(tf.random.truncated_normal([4,3], stddev=0.1, seed=1)) b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))lr = 0.1#學(xué)習(xí)率,不能太大或太小 train_loss_results = []#記錄每輪loss test_acc = []#將每輪的acc記錄下來(lái) epoch = 500#循環(huán)次數(shù) loss_all = 0#記錄每輪四個(gè)step的loss和 ###參數(shù)優(yōu)化 # 嵌套循環(huán)迭代,with結(jié)構(gòu)更新參數(shù),顯示當(dāng)前l(fā)oss ############################################################ m_w, m_b = 0, 0 v_w, v_b = 0, 0 beta1, beta2 = 0.9, 0.999 delta_w, delta_b = 0, 0 global_step = 0 ############################################################ now_time = time.time()###記錄起始時(shí)間 for epoch in range(epoch):for step, (x_train, y_train) in enumerate(train_db):with tf.GradientTape() as tape:x_train = tf.cast(x_train, dtype=w1.dtype)y = tf.matmul(x_train, w1) + b1#神經(jīng)網(wǎng)絡(luò)乘加運(yùn)算y = tf.nn.softmax(y)#使y結(jié)果符合概率分布 與獨(dú)熱碼求lossy_ = tf.one_hot(y_train, depth = 3)#將標(biāo)簽轉(zhuǎn)為獨(dú)熱碼,方便計(jì)算lossy_ = tf.cast(y_, dtype=y.dtype)loss = tf.reduce_mean(tf.square(y_-y))#采用均方誤差損失函數(shù)loss_all += loss.numpy() #加loss累加,后面求均值grads = tape.gradient(loss, [w1, b1])##########################################################adamm_w = beta1 * m_w + (1 - beta1) * grads[0]m_b = beta1 * m_b + (1 - beta1) * grads[1]v_w = beta2 * v_w + (1 - beta2) * tf.square(grads[0])v_b = beta2 * v_b + (1 - beta2) * tf.square(grads[1])m_w_correction = m_w / (1 - tf.pow(beta1, int(global_step)))m_b_correction = m_b / (1 - tf.pow(beta1, int(global_step)))v_w_correction = v_w / (1 - tf.pow(beta2, int(global_step)))v_b_correction = v_b / (1 - tf.pow(beta2, int(global_step)))w1.assign_sub(lr * m_w_correction / tf.sqrt(v_w_correction))b1.assign_sub(lr * m_b_correction / tf.sqrt(v_b_correction))#########################################################每個(gè)epoech打印loss信息print("Epoch{},loss:{}".format(epoch, loss_all/4))train_loss_results.append(loss_all / 4)#記錄四個(gè)step的loss平均值loss_all = 0#歸0,為下一次做準(zhǔn)備 ###測(cè)試效果 # 計(jì)算當(dāng)前參數(shù)向后傳播的準(zhǔn)確率,顯示當(dāng)前的acctotal_correct, total_number = 0, 0for x_test, y_test in test_db:x_test = tf.cast(x_test, dtype=w1.dtype)#使用訓(xùn)練得到的參數(shù)進(jìn)行預(yù)測(cè)y = tf.matmul(x_test, w1) + b1y = tf.nn.softmax(y)pred = tf.argmax(y, axis=1)#返回最大值,即預(yù)測(cè)到的值#將pred轉(zhuǎn)換為y_test類型pred = tf.cast(pred, dtype=y_test.dtype)#將比較結(jié)果的布爾型轉(zhuǎn)換為int型correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)correct = tf.reduce_sum(correct)#如果分類正確則+1total_correct += int(correct)#累加,方便后面求正確率total_number += x_test.shape[0]#測(cè)試總樣本數(shù)acc = total_correct / total_numbertest_acc.append(acc)print("Test_acc:",acc)print("----------------------------") total_time = time.time() - now_time print("total_time", total_time)##記錄訓(xùn)練時(shí)間 ##繪制loss曲線方便觀察 plt.title("Loss Function Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('loss')#y軸變量名 plt.plot(train_loss_results, label="$Loss$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像##繪制acc曲線方便觀察 plt.title("Acc Curve") plt.xlabel('Epoch')#x軸變量名 plt.ylabel('Acc')#y軸變量名 plt.plot(test_acc, label="$Accuracy$") plt.legend()#畫(huà)出曲線圖標(biāo) plt.show()#畫(huà)出圖像總結(jié)
以上是生活随笔為你收集整理的机器学习(六)——优化器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 机器学习(五)——缓解过拟合
- 下一篇: 机器学习(七)——tf.keras搭建神