sklearn.gp_的包装器使用嵌套字典最小化以获得更简单的参数规范。
gaussian-process的Python项目详细描述
sklearn.gp_minimize“的包装器,用于使用嵌套字典的更简单的参数规范。
如何安装此软件包?
像往常一样,只需使用pip:
pip install gaussian_process
使用高斯过程的keras模型优化
importsilence_tensorflowfromkeras.modelsimportSequentialfromkeras.layersimportDense,Dropoutfromkeras.datasetsimportboston_housingfromextra_keras_utilsimportset_seedfromtypingimportCallable,Dictimportnumpyasnpfromholdouts_generatorimportholdouts_generator,random_holdoutsfromgaussian_processimportTQDMGaussianProcess,Space,GaussianProcessclassMLP:def__init__(self,holdouts:Callable):self._holdouts=holdoutsdefmlp(self,dense_layers:Dict,dropout_rate:float)->Sequential:returnSequential([*[Dense(**kwargs)forkwargsindense_layers],Dropout(dropout_rate),Dense(1,activation="relu"),])defmodel_score(self,train:np.ndarray,test:np.ndarray,structure:Dict,fit:Dict):model=self.mlp(**structure)model.compile(optimizer="nadam",loss="mse")returnmodel.fit(*train,epochs=1,validation_data=test,verbose=0,**fit).history["val_loss"][-1]defscore(self,structure:Dict,fit:Dict):return-np.mean([self.model_score(training,test,structure,fit)for(training,test),_inself._holdouts()])if__name__=="__main__":set_seed(42)generator=holdouts_generator(*boston_housing.load_data()[0],holdouts=random_holdouts([0.1],[2]))mlp=MLP(generator)space=Space({"structure":{"dense_layers":[{"units":(8,16,32),"activation":("relu","selu")},{"units":[8,16,32],"activation":("relu","selu")}],"dropout_rate":[0.0,1.0]},"fit":{"batch_size":[100,1000]}})gp=GaussianProcess(mlp.score,space)n_calls=3results=gp.minimize(n_calls=n_calls,n_random_starts=1,callback=[TQDMGaussianProcess(n_calls=n_calls)],random_state=42)results=gp.minimize(n_calls=n_calls,n_random_starts=1,callback=[TQDMGaussianProcess(n_calls=n_calls)],random_state=42)print(gp.best_parameters)print(gp.best_optimized_parameters)gp.clear_cache()