一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - OpenCV python sklearn隨機超參數搜索的實現

OpenCV python sklearn隨機超參數搜索的實現

2020-04-18 10:48廷益--飛鳥 Python

這篇文章主要介紹了OpenCV python sklearn隨機超參數搜索的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
房價預測數據集 使用sklearn執行超參數搜索
"""
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.加載數據集 california 房價
housing = fetch_california_housing()
 
print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)
 
# 2.拆分數據集 訓練集 驗證集 測試集
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.數據集歸一化
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)
 
 
# 創建keras模型
def build_model(hidden_layers=1, # 中間層的參數
        layer_size=30,
        learning_rate=3e-3):
  # 創建網絡層
  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))
 
  # 優化器學習率
  optimizer = keras.optimizers.SGD(lr=learning_rate)
  model.compile(loss="mse", optimizer=optimizer)
 
  return model
 
 
def main():
  # RandomizedSearchCV
 
  # 1.轉化為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.定義超參數集合
  # 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.執行超搜索參數
  # 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.顯示超參數
  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()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/weixin_45875105/article/details/104008975

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: freehd182d动漫| 久久一本综合 | 91制片厂(果冻传媒)原档破解 | 四虎最新免费网址 | 国产福利专区精品视频 | 武侠艳妇屈辱的张开双腿 | 国产精品久久免费 | 四川女人偷人真实视频 | 99视频精品国在线视频艾草 | 色婷婷天天综合在线 | 色一情一区二区三区四区 | 精品无码久久久久久久久 | 亚洲免费在线观看 | 办公室里被迫高h | 精品一区二区三区视频 | 99在线免费观看 | 色综合网亚洲精品久久 | 欧美日韩高清完整版在线观看免费 | 五月天综合久久 | 亚洲 欧美 另类 中文 在线 | 男人影院天堂网址 | 我把校花黑色蕾丝胸罩脱了 | 雪恋电影完整版免费观看 | 9 1 视频在线 | 日韩大片在线播放 | 狠狠色狠狠色综合系列 | 亚洲成av人在线视 | 四虎成人4hutv影院 | 亚洲天堂一区二区在线观看 | 国产欧美视频一区二区三区 | 小货SAO边洗澡边CAO你动漫 | 日本xxxx在线视频免费 | 9191久久 | h视频免费高清在线观看 | 色女的乖男人 | 日韩亚洲欧美一区二区三区 | 日本黄色大片网站 | 手机看片自拍自自拍日韩免费 | 亚洲精选在线观看 | 热久久最新视频 | 四虎免费永久观看 |