日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

神经网络如何调参、超参数的最优化方法、python实现

發布時間:2025/3/12 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 神经网络如何调参、超参数的最优化方法、python实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

神經網絡如何調參、超參數的最優化方法、python實現

    • 一、what is 超參數
    • 二、超參數優化實驗

一、what is 超參數

超參數是什么,其實就是,各層神經元數量、batch大小、學習率等人為設定的一些數。

數據集分為訓練數據、測試數據、驗證數據。

用測試數據評估超參數值的好壞,就可能導致超參數的值被調整為只擬合測試數據,所以加了個驗證數據。

訓練數據用于參數的學習,驗證數據用于超參數的性能評估。

進行超參數最優化,重要的是,逐漸縮小超參數好值存在范圍。

一開始大致設定一個范圍,從范圍中隨機采樣出超參數,用這個采樣值進行識別精度評估,根據這個結果縮小超參數好值范圍,然后重復上述操作。研究發現,隨機采樣效果好點。

二、超參數優化實驗

接下來用MNISIT數據集進行超參數最優化,參考斯坦福大學的實驗。

實驗:最優化學習率和控制權值衰減強度系數這兩個參數。

實驗中,權值衰減系數初始范圍1e- 8到1e- 4,學習率初始范圍1e- 6到1e- 2。

隨機采樣體現在下面代碼:

weight_decay = 10 ** np.random.uniform(-8, -4)lr = 10 ** np.random.uniform(-6, -2)

實驗結果:

結果可以看出,學習率在0.001到0.01之間,權值衰減系數在1e-8到1e-6之間時,學習可以順利進行。

觀察可以使學習順利進行的超參數范圍,從而縮小值的范圍。

然后可以從縮小的范圍中繼續縮小,然后選個最終值。

=========== Hyper-Parameter Optimization Result =========== Best-1(val acc:0.8) | lr:0.008986830875594513, weight decay:3.716187805144909e-07 Best-2(val acc:0.76) | lr:0.007815234765792472, weight decay:8.723036800420108e-08 Best-3(val acc:0.73) | lr:0.004924088836198354, weight decay:5.044414627324654e-07 Best-4(val acc:0.7) | lr:0.006838530258012433, weight decay:7.678322790416307e-06 Best-5(val acc:0.69) | lr:0.0037618568422154793, weight decay:6.384663995933291e-08 Best-6(val acc:0.69) | lr:0.004818463383741305, weight decay:4.875486288914377e-08 Best-7(val acc:0.65) | lr:0.004659925318439445, weight decay:1.4968108648982665e-05 Best-8(val acc:0.64) | lr:0.005664124223619111, weight decay:6.070191899324037e-06 Best-9(val acc:0.56) | lr:0.003954240835144594, weight decay:1.5725686195018805e-06 Best-10(val acc:0.5) | lr:0.002554755378245952, weight decay:4.481334628759244e-08 Best-11(val acc:0.5) | lr:0.002855983685917335, weight decay:1.9598718051356917e-05 Best-12(val acc:0.47) | lr:0.004592998586693871, weight decay:4.888121831499798e-05 Best-13(val acc:0.47) | lr:0.0025326736070483947, weight decay:3.200796060402024e-05 Best-14(val acc:0.44) | lr:0.002645798359877985, weight decay:5.0830237860839325e-06 Best-15(val acc:0.42) | lr:0.001942571686958991, weight decay:3.0673143794194257e-06 Best-16(val acc:0.37) | lr:0.001289748323175032, weight decay:2.3690338828642213e-06 Best-17(val acc:0.36) | lr:0.0017017390582746337, weight decay:9.176068035802207e-05 Best-18(val acc:0.3) | lr:0.0015961247160317246, weight decay:1.3527453417413358e-08 Best-19(val acc:0.28) | lr:0.002261959202515378, weight decay:6.004620370338303e-05 Best-20(val acc:0.26) | lr:0.0008799239275589458, weight decay:4.600825912333848e-07

# coding: utf-8 import sys, os sys.path.append(os.pardir) # 為了導入父目錄的文件而進行的設定 import numpy as np import matplotlib.pyplot as plt from dataset.mnist import load_mnist from common.multi_layer_net import MultiLayerNet from common.util import shuffle_dataset from common.trainer import Trainer(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True)# 為了實現高速化,減少訓練數據 x_train = x_train[:500] t_train = t_train[:500]# 分割驗證數據 validation_rate = 0.20 validation_num = int(x_train.shape[0] * validation_rate) x_train, t_train = shuffle_dataset(x_train, t_train) x_val = x_train[:validation_num] t_val = t_train[:validation_num] x_train = x_train[validation_num:] t_train = t_train[validation_num:]def __train(lr, weight_decay, epocs=50):network = MultiLayerNet(input_size=784, hidden_size_list=[100, 100, 100, 100, 100, 100],output_size=10, weight_decay_lambda=weight_decay)trainer = Trainer(network, x_train, t_train, x_val, t_val,epochs=epocs, mini_batch_size=100,optimizer='sgd', optimizer_param={'lr': lr}, verbose=False)trainer.train()return trainer.test_acc_list, trainer.train_acc_list# 超參數的隨機搜索====================================== optimization_trial = 100 results_val = {} results_train = {} for _ in range(optimization_trial):# 指定搜索的超參數的范圍===============weight_decay = 10 ** np.random.uniform(-8, -4)lr = 10 ** np.random.uniform(-6, -2)# ================================================val_acc_list, train_acc_list = __train(lr, weight_decay)print("val acc:" + str(val_acc_list[-1]) + " | lr:" + str(lr) + ", weight decay:" + str(weight_decay))key = "lr:" + str(lr) + ", weight decay:" + str(weight_decay)results_val[key] = val_acc_listresults_train[key] = train_acc_list# 繪制圖形======================================================== print("=========== Hyper-Parameter Optimization Result ===========") graph_draw_num = 20 col_num = 5 row_num = int(np.ceil(graph_draw_num / col_num)) i = 0for key, val_acc_list in sorted(results_val.items(), key=lambda x:x[1][-1], reverse=True):print("Best-" + str(i+1) + "(val acc:" + str(val_acc_list[-1]) + ") | " + key)plt.subplot(row_num, col_num, i+1)plt.title("Best-" + str(i+1))plt.ylim(0.0, 1.0)if i % 5: plt.yticks([])plt.xticks([])x = np.arange(len(val_acc_list))plt.plot(x, val_acc_list)plt.plot(x, results_train[key], "--")i += 1if i >= graph_draw_num:breakplt.show()

總結

以上是生活随笔為你收集整理的神经网络如何调参、超参数的最优化方法、python实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。