小編給大家分享一下OpenCV python sklearn如何實(shí)現(xiàn)隨機(jī)超參數(shù)搜索,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)公司專注于沙灣企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,購物商城網(wǎng)站建設(shè)。沙灣網(wǎng)站建設(shè)公司,為沙灣等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站建設(shè),專業(yè)設(shè)計,全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)本文介紹了OpenCV python sklearn隨機(jī)超參數(shù)搜索的實(shí)現(xiàn),分享給大家,具體如下:
""" 房價預(yù)測數(shù)據(jù)集 使用sklearn執(zhí)行超參數(shù)搜索 """ import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import sklearn import pandas as pd import os import sys import tensorflow as tf from tensorflow_core.python.keras.api._v2 import keras # 不能使用 python from sklearn.preprocessing import StandardScaler from sklearn.datasets import fetch_california_housing from sklearn.model_selection import train_test_split, RandomizedSearchCV from scipy.stats import reciprocal os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' assert tf.__version__.startswith('2.') # 0.打印導(dǎo)入模塊的版本 print(tf.__version__) print(sys.version_info) for module in mpl, np, sklearn, pd, tf, keras: print("%s version:%s" % (module.__name__, module.__version__)) # 顯示學(xué)習(xí)曲線 def plot_learning_curves(his): pd.DataFrame(his.history).plot(figsize=(8, 5)) plt.grid(True) plt.gca().set_ylim(0, 1) plt.show() # 1.加載數(shù)據(jù)集 california 房價 housing = fetch_california_housing() print(housing.DESCR) print(housing.data.shape) print(housing.target.shape) # 2.拆分?jǐn)?shù)據(jù)集 訓(xùn)練集 驗(yàn)證集 測試集 x_train_all, x_test, y_train_all, y_test = train_test_split( housing.data, housing.target, random_state=7) x_train, x_valid, y_train, y_valid = train_test_split( x_train_all, y_train_all, random_state=11) print(x_train.shape, y_train.shape) print(x_valid.shape, y_valid.shape) print(x_test.shape, y_test.shape) # 3.數(shù)據(jù)集歸一化 scaler = StandardScaler() x_train_scaled = scaler.fit_transform(x_train) x_valid_scaled = scaler.fit_transform(x_valid) x_test_scaled = scaler.fit_transform(x_test) # 創(chuàng)建keras模型 def build_model(hidden_layers=1, # 中間層的參數(shù) layer_size=30, learning_rate=3e-3): # 創(chuàng)建網(wǎng)絡(luò)層 model = keras.models.Sequential() model.add(keras.layers.Dense(layer_size, activation="relu", input_shape=x_train.shape[1:])) # 隱藏層設(shè)置 for _ in range(hidden_layers - 1): model.add(keras.layers.Dense(layer_size, activation="relu")) model.add(keras.layers.Dense(1)) # 優(yōu)化器學(xué)習(xí)率 optimizer = keras.optimizers.SGD(lr=learning_rate) model.compile(loss="mse", optimizer=optimizer) return model def main(): # RandomizedSearchCV # 1.轉(zhuǎn)化為sklearn的model sk_learn_model = keras.wrappers.scikit_learn.KerasRegressor(build_model) callbacks = [keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)] history = sk_learn_model.fit(x_train_scaled, y_train, epochs=100, validation_data=(x_valid_scaled, y_valid), callbacks=callbacks) # 2.定義超參數(shù)集合 # f(x) = 1/(x*log(b/a)) a <= x <= b param_distribution = { "hidden_layers": [1, 2, 3, 4], "layer_size": np.arange(1, 100), "learning_rate": reciprocal(1e-4, 1e-2), } # 3.執(zhí)行超搜索參數(shù) # cross_validation:訓(xùn)練集分成n份, n-1訓(xùn)練, 最后一份驗(yàn)證. random_search_cv = RandomizedSearchCV(sk_learn_model, param_distribution, n_iter=10, cv=3, n_jobs=1) random_search_cv.fit(x_train_scaled, y_train, epochs=100, validation_data=(x_valid_scaled, y_valid), callbacks=callbacks) # 4.顯示超參數(shù) print(random_search_cv.best_params_) print(random_search_cv.best_score_) print(random_search_cv.best_estimator_) model = random_search_cv.best_estimator_.model print(model.evaluate(x_test_scaled, y_test)) # 5.打印模型訓(xùn)練過程 plot_learning_curves(history) if __name__ == '__main__': main()
看完了這篇文章,相信你對“OpenCV python sklearn如何實(shí)現(xiàn)隨機(jī)超參數(shù)搜索”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道,感謝各位的閱讀!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。