亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

OpenCV python sklearn隨機超參數(shù)搜索的實現(xiàn)

 更新時間:2020年01月17日 09:46:42   作者:廷益--飛鳥  
這篇文章主要介紹了OpenCV python sklearn隨機超參數(shù)搜索的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

本文介紹了OpenCV python sklearn隨機超參數(shù)搜索的實現(xiàn),分享給大家,具體如下:

"""
房價預測數(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.打印導入模塊的版本
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, sklearn, pd, tf, keras:
  print("%s version:%s" % (module.__name__, module.__version__))


# 顯示學習曲線
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.拆分數(shù)據(jù)集 訓練集 驗證集 測試集
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)絡層
  model = keras.models.Sequential()
  model.add(keras.layers.Dense(layer_size, activation="relu",
                 input_shape=x_train.shape[1:]))
 # 隱藏層設置
  for _ in range(hidden_layers - 1):
    model.add(keras.layers.Dense(layer_size,
                   activation="relu"))
  model.add(keras.layers.Dense(1))

  # 優(yōu)化器學習率
  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:訓練集分成n份, n-1訓練, 最后一份驗證.
  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.打印模型訓練過程
  plot_learning_curves(history)


if __name__ == '__main__':
  main()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • PyTorch預訓練Bert模型的示例

    PyTorch預訓練Bert模型的示例

    這篇文章主要介紹了PyTorch預訓練Bert模型的示例,幫助大家更好的進行機器學習,訓練模型,感興趣的朋友可以了解下
    2020-11-11
  • 基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析

    基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析

    今天小編就為大家分享一篇基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python坐標軸操作及設置代碼實例

    Python坐標軸操作及設置代碼實例

    這篇文章主要介紹了Python坐標軸操作及設置代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Python實現(xiàn)桌面翻譯工具【新手必學】

    Python實現(xiàn)桌面翻譯工具【新手必學】

    這篇文章主要介紹了Python實現(xiàn)一個桌面翻譯工具,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 基于pip install django失敗時的解決方法

    基于pip install django失敗時的解決方法

    今天小編就為大家分享一篇基于pip install django失敗時的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python 利用for循環(huán) 保存多個圖像或者文件的實例

    python 利用for循環(huán) 保存多個圖像或者文件的實例

    今天小編就為大家分享一篇python 利用for循環(huán) 保存多個圖像或者文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python數(shù)據(jù)分析之文件讀取詳解

    python數(shù)據(jù)分析之文件讀取詳解

    大家好,本篇文章主要講的是python數(shù)據(jù)分析之文件讀取詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解

    python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解

    這篇文章主要給大家介紹了關(guān)于python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • Python實戰(zhàn)小游戲飛機大戰(zhàn)詳解

    Python實戰(zhàn)小游戲飛機大戰(zhàn)詳解

    飛機大戰(zhàn)想必是很多人童年時期的經(jīng)典游戲,我們依舊能記得抱個老人機娛樂的場景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫一個簡單的飛機大戰(zhàn)小游戲的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 在win10和linux上分別安裝Python虛擬環(huán)境的方法步驟

    在win10和linux上分別安裝Python虛擬環(huán)境的方法步驟

    這篇文章主要介紹了在win10和linux上分別安裝Python虛擬環(huán)境的方法步驟,虛機環(huán)境有非常多的優(yōu)點,今天我們用的虛擬環(huán)境是virtualenv。感興趣的小伙伴們可以參考一下
    2019-05-05

最新評論