1. 程式人生 > >Keras/Python深度學習中的網格搜尋超引數調優(附原始碼)

Keras/Python深度學習中的網格搜尋超引數調優(附原始碼)

超引數優化是深度學習中的重要組成部分。其原因在於,神經網路是公認的難以配置,而又有很多引數需要設定。最重要的是,個別模型的訓練非常緩慢。

在這篇文章中,你會了解到如何使用scikit-learn python機器學習庫中的網格搜尋功能調整Keras深度學習模型中的超引數。

閱讀本文後,你就會了解:

  • 如何包裝Keras模型以便在scikit-learn中使用,以及如何使用網格搜尋。
  • 如何網格搜尋常見的神經網路引數,如學習速率、 dropout 率、epochs 和神經元數量。
  • 如何設計自己的超引數優化實驗。

概述

本文主要想為大家介紹如何使用scikit-learn網格搜尋功能,並給出一套程式碼例項。你可以將程式碼複製貼上到自己的專案中,作為專案起始。

下文所涉及的議題列表:

  1. 如何在scikit-learn模型中使用Keras。
  2. 如何在scikit-learn模型中使用網格搜尋。
  3. 如何調優批尺寸和訓練epochs。
  4. 如何調優優化演算法。
  5. 如何調優學習率和動量因子。
  6. 如何確定網路權值初始值。
  7. 如何選擇神經元啟用函式。
  8. 如何調優Dropout正則化。
  9. 如何確定隱藏層中的神經元的數量。

如何在scikit-learn模型中使用Keras

通過用KerasClassifierKerasRegressor類包裝Keras模型,可將其用於scikit-learn。

要使用這些包裝,必須定義一個函式,以便按順序模式建立並返回Keras,然後當構建KerasClassifier

類時,把該函式傳遞給build_fn引數。

例如:

def create_model():
    ...
    return model

model = KerasClassifier(build_fn=create_model)

KerasClassifier類的構建器為可以採取預設引數,並將其被傳遞給model.fit()的呼叫函式,比如 epochs數目和批尺寸(batch size)。

例如:

def create_model():
    ...
    return model

model = KerasClassifier(build_fn=create_model, nb_epoch=10
)

KerasClassifier類的構造也可以使用新的引數,使之能夠傳遞給自定義的create_model()函式。這些新的引數,也必須由使用預設引數的 create_model() 函式的簽名定義。

例如:

def create_model(dropout_rate=0.0):
    ...
    return model

model = KerasClassifier(build_fn=create_model, dropout_rate=0.2)

您可以在Keras API文件中,瞭解到更多關於scikit-learn包裝器的知識。

如何在scikit-learn模型中使用網格搜尋

網格搜尋(grid search)是一項模型超引數優化技術。

在scikit-learn中,該技術由GridSearchCV類提供。

當構造該類時,你必須提供超引數字典,以便用來評價param_grid引數。這是模型引數名稱和大量列值的示意圖。

預設情況下,精確度是優化的核心,但其他核心可指定用於GridSearchCV建構函式的score引數。

預設情況下,網格搜尋只使用一個執行緒。在GridSearchCV建構函式中,通過將 n_jobs引數設定為-1,則程序將使用計算機上的所有核心。這取決於你的Keras後端,並可能干擾主神經網路的訓練過程。

當構造並評估一個模型中各個引數的組合時,GridSearchCV會起作用。使用交叉驗證評估每個單個模型,且預設使用3層交叉驗證,儘管通過將cv引數指定給 GridSearchCV建構函式時,有可能將其覆蓋。

下面是定義一個簡單的網格搜尋示例:

param_grid = dict(nb_epochs=[10,20,30])
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)

一旦完成,你可以訪問網格搜尋的輸出,該輸出來自結果物件,由grid.fit()返回。best_score_成員提供優化過程期間觀察到的最好的評分, best_params_描述了已取得最佳結果的引數的組合。

您可以在scikit-learn API文件中瞭解更多關於GridSearchCV類的知識。

問題描述

現在我們知道了如何使用scikit-learn 的Keras模型,如何使用scikit-learn 的網格搜尋。現在一起看看下面的例子。

所有的例子都將在一個小型的標準機器學習資料集上來演示,該資料集被稱為Pima Indians onset of diabetes 分類資料集。該小型資料集包括了所有容易工作的數值屬性。

下載資料集,並把它放置在你目前工作目錄下,命名為:pima-indians-diabetes.csv

當我們按照本文中的例子進行,能夠獲得最佳引數。因為引數可相互影響,所以這不是網格搜尋的最佳方法,但出於演示目的,它是很好的方法。

注意並行化網格搜尋

所有示例的配置為了實現並行化(n_jobs=-1)。

如果顯示像下面這樣的錯誤:

INFO (theano.gof.compilelock): Waiting for existing lock by process '55614' (I am process '55613')
INFO (theano.gof.compilelock): To manually release the lock, delete ...

結束程序,並修改程式碼,以便不併行地執行網格搜尋,設定n_jobs=1。

如何調優批尺寸和訓練epochs

在第一個簡單的例子中,當調整網路時,我們著眼於調整批尺寸和訓練epochs。

迭代梯度下降的批尺寸大小是權重更新之前顯示給網路的模式數量。它也是在網路訓練的優選法,定義一次讀取的模式數並保持在記憶體中。

訓練epochs是訓練期間整個訓練資料集顯示給網路的次數。有些網路對批尺寸大小敏感,如LSTM複發性神經網路和卷積神經網路。

在這裡,我們將以20的步長,從10到100逐步評估不同的微型批尺寸。

完整程式碼如下:

# Use scikit-learn to grid search the batch size and epochs
import numpy
from sklearn.grid_search import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
# Function to create model, required for KerasClassifier
def create_model():
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, verbose=0)
# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, nb_epoch=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))

執行之後輸出如下:

Best: 0.686198 using {'nb_epoch': 100, 'batch_size': 20}
0.348958 (0.024774) with: {'nb_epoch': 10, 'batch_size': 10}
0.348958 (0.024774) with: {'nb_epoch': 50, 'batch_size': 10}
0.466146 (0.149269) with: {'nb_epoch': 100, 'batch_size': 10}
0.647135 (0.021236) with: {'nb_epoch': 10, 'batch_size': 20}
0.660156 (0.014616) with: {'nb_epoch': 50, 'batch_size': 20}
0.686198 (0.024774) with: {'nb_epoch': 100, 'batch_size': 20}
0.489583 (0.075566) with: {'nb_epoch': 10, 'batch_size': 40}
0.652344 (0.019918) with: {'nb_epoch': 50, 'batch_size': 40}
0.654948 (0.027866) with: {'nb_epoch': 100, 'batch_size': 40}
0.518229 (0.032264) with: {'nb_epoch': 10, 'batch_size': 60}
0.605469 (0.052213) with: {'nb_epoch': 50, 'batch_size': 60}
0.665365 (0.004872) with: {'nb_epoch': 100, 'batch_size': 60}
0.537760 (0.143537) with: {'nb_epoch': 10, 'batch_size': 80}
0.591146 (0.094954) with: {'nb_epoch': 50, 'batch_size': 80}
0.658854 (0.054904) with: {'nb_epoch': 100, 'batch_size': 80}
0.402344 (0.107735) with: {'nb_epoch': 10, 'batch_size': 100}
0.652344 (0.033299) with: {'nb_epoch': 50, 'batch_size': 100}
0.542969 (0.157934) with: {'nb_epoch': 100, 'batch_size': 100}

我們可以看到,批尺寸為20、100 epochs能夠獲得最好的結果,精確度約68%。

如何調優訓練優化演算法

Keras提供了一套最先進的不同的優化演算法。

在這個例子中,我們調整用來訓練網路的優化演算法,每個都用預設引數。

這個例子有點奇怪,因為往往你會先選擇一種方法,而不是將重點放在調整問題引數上(參見下一個示例)。

完整程式碼如下:

# Use scikit-learn to grid search the batch size and epochs
import numpy
from sklearn.grid_search import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
# Function to create model, required for KerasClassifier
def create_model(optimizer='adam'):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, nb_epoch=100, batch_size=10, verbose=0)
# define the grid search parameters
optimizer = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
param_grid = dict(optimizer=optimizer)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))

執行之後輸出如下:

Best: 0.704427 using {'optimizer': 'Adam'}
0.348958 (0.024774) with: {'optimizer': 'SGD'}
0.348958 (0.024774) with: {'optimizer': 'RMSprop'}
0.471354 (0.156586) with: {'optimizer': 'Adagrad'}
0.669271 (0.029635) with: {'optimizer': 'Adadelta'}
0.704427 (0.031466) with: {'optimizer': 'Adam'}
0.682292 (0.016367) with: {'optimizer': 'Adamax'}
0.703125 (0.003189) with: {'optimizer': 'Nadam'}

結果表明,ATOM優化演算法結果最好,精確度約為70%。

如何優化學習速率和動量因子?

預先選擇一個優化演算法來訓練你的網路和引數調整是十分常見的。目前,最常用的優化演算法是普通的隨機梯度下降法(Stochastic Gradient Descent,SGD),因為它十分易於理解。在本例中,我們將著眼於優化SGD的學習速率和動量因子(momentum)。

學習速率控制每批(batch)結束時更新的權重,動量因子控制上次權重的更新對本次權重更新的影響程度。

我們選取了一組較小的學習速率和動量因子的取值範圍:從0.2到0.8,步長為0.2,以及0.9(實際中常用引數值)。

一般來說,在優化演算法中包含epoch的數目是一個好主意,因為每批(batch)學習量(學習速率)、每個 epoch更新的數目(批尺寸)和 epoch的數量之間都具有相關性。

完整程式碼如下:

# Use scikit-learn to grid search the learning rate and momentum
import numpy
from sklearn.grid_search import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.optimizers import SGD
# Function to create model, required for KerasClassifier
def create_model(learn_rate=0.01, momentum=0):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    optimizer = SGD(lr=learn_rate, momentum=momentum)
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, nb_epoch=100, batch_size=10, verbose=0)
# define the grid search parameters
learn_rate = [0.001, 0.01, 0.1, 0.2, 0.3]
momentum = [0.0, 0.2, 0.4, 0.6, 0.8, 0.9]
param_grid = dict(learn_rate=learn_rate, momentum=momentum)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))

執行之後輸出如下:

Best: 0.680990 using {'learn_rate': 0.01, 'momentum': 0.0}
0.348958 (0.024774) with: {'learn_rate': 0.001, 'momentum': 0.0}
0.348958 (0.024774) with: {'learn_rate': 0.001, 'momentum': 0.2}
0.467448 (0.151098) with: {'learn_rate': 0.001, 'momentum': 0.4}
0.662760 (0.012075) with: {'learn_rate': 0.001, 'momentum': 0.6}
0.669271 (0.030647) with: {'learn_rate': 0.001, 'momentum': 0.8}
0.666667 (0.035564) with: {'learn_rate': 0.001, 'momentum': 0.9}
0.680990 (0.024360) with: {'learn_rate': 0.01, 'momentum': 0.0}
0.677083 (0.026557) with: {'learn_rate': 0.01, 'momentum': 0.2}
0.427083 (0.134575) with: {'learn_rate': 0.01, 'momentum': 0.4}
0.427083 (0.134575) with: {'learn_rate': 0.01, 'momentum': 0.6}
0.544271 (0.146518) with: {'learn_rate': 0.01, 'momentum': 0.8}
0.651042 (0.024774) with: {'learn_rate': 0.01, 'momentum': 0.9}
0.651042 (0.024774) with: {'learn_rate': 0.1, 'momentum': 0.0}
0.651042 (0.024774) with: {'learn_rate': 0.1, 'momentum': 0.2}
0.572917 (0.134575) with: {'learn_rate': 0.1, 'momentum': 0.4}
0.572917 (0.134575) with: {'learn_rate': 0.1, 'momentum': 0.6}
0.651042 (0.024774) with: {'learn_rate': 0.1, 'momentum': 0.8}
0.651042 (0.024774) with: {'learn_rate': 0.1, 'momentum': 0.9}
0.533854 (0.149269) with: {'learn_rate': 0.2, 'momentum': 0.0}
0.427083 (0.134575) with: {'learn_rate': 0.2, 'momentum': 0.2}
0.427083 (0.134575) with: {'learn_rate': 0.2, 'momentum': 0.4}
0.651042 (0.024774) with: {'learn_rate': 0.2, 'momentum': 0.6}
0.651042 (0.024774) with: {'learn_rate': 0.2, 'momentum': 0.8}
0.651042 (0.024774) with: {'learn_rate': 0.2, 'momentum': 0.9}
0.455729 (0.146518) with: {'learn_rate': 0.3, 'momentum': 0.0}
0.455729 (0.146518) with: {'learn_rate': 0.3, 'momentum': 0.2}
0.455729 (0.146518) with: {'learn_rate': 0.3, 'momentum': 0.4}
0.348958 (0.024774) with: {'learn_rate': 0.3, 'momentum': 0.6}
0.348958 (0.024774) with: {'learn_rate': 0.3, 'momentum': 0.8}
0.348958 (0.024774) with: {'learn_rate': 0.3, 'momentum': 0.9}

可以看到,SGD在該問題上相對錶現不是很好,但當學習速率為0.01、動量因子為0.0時可取得最好的結果,正確率約為68%。

如何調優網路權值初始化

神經網路權值初始化一度十分簡單:採用小的隨機數即可。

在本例中,我們將著眼於通過評估所有可用的技術,來調優網路權值初始化的選擇。

我們將在每一層採用相同的權值初始化方法。理想情況下,根據每層使用的啟用函式選用不同的權值初始化方法效果可能更好。在下面的例子中,我們在隱藏層使用了整流器(rectifier)。因為預測是二進位制,因此在輸出層使用了sigmoid函式。

完整程式碼如下:

# Use scikit-learn to grid search the weight initialization
import numpy
from sklearn.grid_search import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
# Function to create model, required for KerasClassifier
def create_model(init_mode='uniform'):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, init=init_mode, activation='relu'))
    model.add(Dense(1, init=init_mode, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, nb_epoch=100, batch_size=10, verbose=0)
# define the grid search parameters
init_mode = ['uniform', 'lecun_uniform', 'normal', 'zero', 'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']
param_grid = dict(init_mode=init_mode)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))

執行之後輸出如下:

Best: 0.720052 using {'init_mode': 'uniform'}
0.720052 (0.024360) with: {'init_mode': 'uniform'}
0.348958 (0.024774) with: {'init_mode': 'lecun_uniform'}
0.712240 (0.012075) with: {'init_mode': 'normal'}
0.651042 (0.024774) with: {'init_mode': 'zero'}
0.700521 (0.010253) with: {'init_mode': 'glorot_normal'}
0.674479 (0.011201) with: {'init_mode': 'glorot_uniform'}
0.661458 (0.028940) with: {'init_mode': 'he_normal'}
0.678385 (0.004872) with: {'init_mode': 'he_uniform'}

我們可以看到,當採用均勻權值初始化方案(uniform weight initialization )時取得最好的結果,可以實現約72%的效能。

如何選擇神經元啟用函式

啟用函式控制著單個神經元的非線性以及何時啟用。

通常來說,整流器(rectifier)的啟用功能是最受歡迎的,但應對不同的問題, sigmoid函式和tanh 函式可能是更好的選擇。

在本例中,我們將探討、評估、比較Keras提供的不同型別的啟用函式。我們僅在隱層中使用這些函式。考慮到二元分類問題,需要在輸出層使用sigmoid啟用函式。

通常而言,為不同範圍的傳遞函式準備資料是一個好主意,但在本例中我們不會這麼做。

完整程式碼如下:

# Use scikit-learn to grid search the activation function
import numpy
from sklearn.grid_search import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
# Function to create model, required for KerasClassifier
def create_model(activation='relu'):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, init='uniform', activation=activation))
    model.add(Dense(1, init='uniform', activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, nb_epoch=100, batch_size=10, verbose=0)
# define the grid search parameters
activation = ['softmax', 'softplus', 'softsign', 'relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']
param_grid = dict(activation=activation)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))

執行之後輸出如下:

Best: 0.722656 using {'activation': 'linear'}
0.649740 (0.009744) with: {'activation': 'softmax'}
0.720052 (0.032106) with: {'activation': 'softplus'}
0.688802 (0.019225) with: {'activation': 'softsign'}
0.720052 (0.018136) with: {'activation': 'relu'}
0.691406 (0.019401) with: {'activation': 'tanh'}
0.680990 (0.009207) with: {'activation': 'sigmoid'}
0.691406 (0.014616) with: {'activation': 'hard_sigmoid'}
0.722656 (0.003189) with: {'activation': 'linear'}

令人驚訝的是(至少對我來說是),“線性(linear)”啟用函式取得了最好的效果,準確率約為72%。

如何調優Dropout正則化

在本例中,我們將著眼於調整正則化中的dropout速率,以期限制過擬合(overfitting)和提高模型的泛化能力。為了得到較好的結果,dropout最好結合一個如最大範數約束之類的權值約束。

瞭解更多dropout在深度學習框架Keras的使用請檢視下面這篇文章:

它涉及到擬合dropout率和權值約束。我們選定dropout percentages取值範圍是:0.0-0.9(1.0無意義);最大範數權值約束( maxnorm weight constraint)的取值範圍是0-5。

完整程式碼如下:

# Use scikit-learn to grid search the dropout rate
import numpy
from sklearn.grid_search import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.wrappers.scikit_learn import KerasClassifier
from keras.constraints import maxnorm
# Function to create model, required for KerasClassifier
def create_model(dropout_rate=0.0, weight_constraint=0):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, init='uniform', activation='linear', W_constraint=maxnorm(weight_constraint)))
    model.add(Dropout(dropout_rate))
    model.add(Dense(1, init='uniform', activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, nb_epoch=100, batch_size=10, verbose=0)
# define the grid search parameters
weight_constraint = [1, 2, 3, 4, 5]
dropout_rate = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
param_grid = dict(dropout_rate=dropout_rate, weight_constraint=weight_constraint)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))

執行之後輸出如下:

Best: 0.723958 using {'dropout_rate': 0.2, 'weight_constraint': 4}
0.696615 (0.031948) with: {'dropout_rate': 0.0, 'weight_constraint': 1}
0.696615 (0.031948) with: {'dropout_rate': 0.0, 'weight_constraint': 2}
0.691406 (0.026107) with: {'dropout_rate': 0.0, 'weight_constraint': 3}
0.708333 (0.009744) with: {'dropout_rate': 0.0, 'weight_constraint': 4}
0.708333 (0.009744) with: {'dropout_rate': 0.0, 'weight_constraint': 5}
0.710937 (0.008438) with: {'dropout_rate': 0.1, 'weight_constraint': 1}
0.709635 (0.007366) with: {'dropout_rate': 0.1, 'weight_constraint': 2}
0.709635 (0.007366) with: {'dropout_rate': 0.1, 'weight_constraint': 3}
0.695312 (0.012758) with: {'dropout_rate': 0.1, 'weight_constraint': 4}
0.695312 (0.012758) with: {'dropout_rate': 0.1, 'weight_constraint': 5}
0.701823 (0.017566) with: {'dropout_rate': 0.2, 'weight_constraint': 1}
0.710938 (0.009568) with: {'dropout_rate': 0.2, 'weight_constraint': 2}
0.710938 (0.009568) with: {'dropout_rate': 0.2, 'weight_constraint': 3}
0.723958 (0.027126) with: {'dropout_rate': 0.2, 'weight_constraint': 4}
0.718750 (0.030425) with: {'dropout_rate': 0.2, 'weight_constraint': 5}
0.721354 (0.032734) with: {'dropout_rate': 0.3, 'weight_constraint': 1}
0.707031 (0.036782) with: {'dropout_rate': 0.3, 'weight_constraint': 2}
0.707031 (0.036782) with: {'dropout_rate': 0.3, 'weight_constraint': 3}
0.694010 (0.019225) with: {'dropout_rate': 0.3, 'weight_constraint': 4}
0.709635 (0.006639) with: {'dropout_rate': 0.3, 'weight_constraint': 5}
0.704427 (0.008027) with: {'dropout_rate': 0.4, 'weight_constraint': 1}
0.717448 (0.031304) with: {'dropout_rate': 0.4, 'weight_constraint': 2}
0.718750 (0.030425) with: {'dropout_rate': 0.4, 'weight_constraint': 3}
0.718750 (0.030425) with: {'dropout_rate': 0.4, 'weight_constraint': 4}
0.722656 (0.029232) with: {'dropout_rate': 0.4, 'weight_constraint': 5}
0.720052 (0.028940) with: {'dropout_rate': 0.5, 'weight_constraint': 1}
0.703125 (0.009568) with: {'dropout_rate': 0.5, 'weight_constraint': 2}
0.716146 (0.029635) with: {'dropout_rate': 0.5, 'weight_constraint': 3}
0.709635 (0.008027) with: {'dropout_rate': 0.5, 'weight_constraint': 4}
0.703125 (0.011500) with: {'dropout_rate': 0.5, 'weight_constraint': 5}
0.707031 (0.017758) with: {'dropout_rate': 0.6, 'weight_constraint': 1}
0.701823 (0.018688) with: {'dropout_rate': 0.6, 'weight_constraint': 2}
0.701823 (0.018688) with: {'dropout_rate': 0.6, 'weight_constraint': 3}
0.690104 (0.027498) with: {'dropout_rate': 0.6, 'weight_constraint': 4}
0.695313 (0.022326) with: {'dropout_rate': 0.6, 'weight_constraint': 5}
0.697917 (0.014382) with: {'dropout_rate': 0.7, 'weight_constraint': 1}
0.697917 (0.014382) with: {'dropout_rate': 0.7, 'weight_constraint': 2}
0.687500 (0.008438) with: {'dropout_rate': 0.7, 'weight_constraint':