gpt4 book ai didi

python - GridSearchCV 如何使用内核?

转载 作者:行者123 更新时间:2023-12-04 18:50:09 30 4
gpt4 key购买 nike

我正在做一个 GridSearchCV,我已经监控了核心的百分比,并且我看到当我运行一个简单的神经网络时,4 个核心具有相同的百分比,但是当网格搜索 cv (n_jobs = 1) 开始时在情节的线条上是一个很大的困惑。为什么?我用佐林

enter image description here

您可以在下面找到我执行 gscv 的代码部分(但我更改了 n_jobs,在上图中使用了 n_jobs = 1):

def build_regressor(opti, units1,units2, units3, acti, a, kern):

model_TP = Sequential()
model_TP.add(Dense(units = units1, input_shape = (X_train_TP.shape[1],), activation = acti, kernel_initializer = kern))
model_TP.add(Dropout(a))
model_TP.add(Dense(units = units2, activation = acti, kernel_initializer = kern))
model_TP.add(Dropout(a))
model_TP.add(Dense(units = units3, activation = acti, kernel_initializer = kern))
model_TP.add(Dense(1,activation = 'linear'))
model_TP.compile(loss = 'mse', optimizer = opti, metrics = ['mse'])
return model_TP

parameters ={'units1' : [50,100,150],
'units2' : [50,100,150],
'units3' : [50,100,150],
'batch_size' : [50,100,250],
'kern':['random_normal','glorot_uniform','random_uniform'],
'acti':['relu','sigmoid','linear'],
'opti':[tf.keras.optimizers.Adamax(lr=0.001),
tf.keras.optimizers.Adam(lr=0.001)],
'a' : [0.2,0.25]
}

regressor = KerasRegressor(build_fn = build_regressor, verbose = 1)

gridSearch = GridSearchCV(estimator=regressor,
param_grid=parameters,
cv=5, n_jobs = 4
)

grid_result = gridSearch.fit(X_train_TP, y_train_TP)

print('Grid Search Best score',gridSearch.best_score_)
print('Grid Search Best Parameters', gridSearch.best_params_)
print('Execution time',gridSearch.refit_time_)

最佳答案

根据 the docs :

n_jobs: int or None, optional (default=None)

Number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.



默认情况下,您应该处于并行上下文中。

from sklearn.utils import _joblib                                                                                                                                                                  
backend = _joblib.parallel_backend(_joblib.joblib.parallel.DEFAULT_BACKEND)
backend.new_backend_and_jobs
>>> (<joblib._parallel_backends.LokyBackend at 0x7f984f580dd8>, -1)

因此默认的 n_jobs id -1所以 GridSearchCV 在没有 n_jobs 时使用你所有的 CPU已通过。

关于python - GridSearchCV 如何使用内核?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60289339/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com