如何在 class(尤其是 use_named_args 装饰器)中使用 scikit-learn 优化?
How to use scikit-learn optimize in a class (especially the use_named_args decorator)?
我正在使用 scikit-learn 优化包来调整我的模型的超参数。出于性能和可读性的原因(我正在使用相同的过程训练多个模型),我想在 class:
中构建整个超参数调整
...
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import InputLayer, Input, Dense, Embedding, BatchNormalization, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import TensorBoard, EarlyStopping
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.model_selection import train_test_split
import skopt
from skopt import gp_minimize
from skopt.space import Real, Categorical, Integer
from skopt.plots import plot_convergence
from skopt.plots import plot_objective, plot_evaluations
from skopt.utils import use_named_args
class hptuning:
def __init__(self, input_df):
self.inp_df = input_df
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(...)
self.param_space = self.dim_hptuning()
self.best_loss = 10000
def dim_hptuning(self):
dim_layers = Integer(low=0, high=7, name='layers')
dim_nodes = Integer(low=2, high=90, name='num_nodes')
dimensions = [dim_layers, dim_nodes]
return dimensions
def create_model(self, layers, nodes):
model = Sequential()
for layer in range(layers):
model.add(Dense(nodes))
model.add(Dense(1,activation='sigmoid'))
optimizer = Adam
model.compile(loss='mean_absolute_error',
optimizer=optimizer,
metrics=['mae', 'mse'])
return model
@use_named_args(dimensions=self.param_space)
def fitness(self,nodes, layers):
model = self.create_model(layers=layers, nodes=nodes)
history = model.fit(x=self.X_train.values,y=self.y_train.values,epochs=200,batch_size=200,verbose=0)
loss = history.history['val_loss'][-1]
if loss < self.best_loss:
model.save('model.h5')
self.best_loss = loss
del model
K.clear_session()
return loss
def find_best_model(self):
search_result = gp.minimize(func=self.fitness, dimensions=self.param_space,acq_func='EI',n_calls=10)
return search_result
hptun = hptuning(input_df=df)
search_result = hptun.find_best_model()
print(search_result.fun)
现在我发现装饰器@use_named_args 没有在 class 中正常工作 (example code of scikit-optimize). 我收到错误消息
Traceback (most recent call last):
File "main.py", line 138, in <module>
class hptuning:
File "main.py", line 220, in hptuning
@use_named_args(dimensions=self.param_space)
NameError: name 'self' is not defined
这显然是关于装饰器在这种情况下的误用。
可能是由于我对此类装饰器的功能缺乏了解,我无法获得此 运行。有人可以帮我解决这个问题吗?
在此先感谢大家的支持!
self
未定义的问题与scikit.learn无关。您不能使用 self
来定义装饰器,因为它只在您正在装饰的方法内部定义。但是即使你回避了这个问题(例如通过提供 param_space 作为全局变量)我预计下一个问题将是 self
将被传递给 use_named_args
装饰器,但它只期望待优化参数。
最明显的解决方案是不在 fitness
方法上使用装饰器,而是在 find_best_model
方法内部定义一个调用 fitness
方法的装饰函数,例如这个:
def find_best_model(self):
@use_named_args(dimensions=self.param_space)
def fitness_wrapper(*args, **kwargs):
return self.fitness(*args, **kwargs)
search_result = gp.minimize(func=fitness_wrapper, dimensions=self.param_space,acq_func='EI',n_calls=10)
return search_result
我正在使用 scikit-learn 优化包来调整我的模型的超参数。出于性能和可读性的原因(我正在使用相同的过程训练多个模型),我想在 class:
中构建整个超参数调整...
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import InputLayer, Input, Dense, Embedding, BatchNormalization, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import TensorBoard, EarlyStopping
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.model_selection import train_test_split
import skopt
from skopt import gp_minimize
from skopt.space import Real, Categorical, Integer
from skopt.plots import plot_convergence
from skopt.plots import plot_objective, plot_evaluations
from skopt.utils import use_named_args
class hptuning:
def __init__(self, input_df):
self.inp_df = input_df
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(...)
self.param_space = self.dim_hptuning()
self.best_loss = 10000
def dim_hptuning(self):
dim_layers = Integer(low=0, high=7, name='layers')
dim_nodes = Integer(low=2, high=90, name='num_nodes')
dimensions = [dim_layers, dim_nodes]
return dimensions
def create_model(self, layers, nodes):
model = Sequential()
for layer in range(layers):
model.add(Dense(nodes))
model.add(Dense(1,activation='sigmoid'))
optimizer = Adam
model.compile(loss='mean_absolute_error',
optimizer=optimizer,
metrics=['mae', 'mse'])
return model
@use_named_args(dimensions=self.param_space)
def fitness(self,nodes, layers):
model = self.create_model(layers=layers, nodes=nodes)
history = model.fit(x=self.X_train.values,y=self.y_train.values,epochs=200,batch_size=200,verbose=0)
loss = history.history['val_loss'][-1]
if loss < self.best_loss:
model.save('model.h5')
self.best_loss = loss
del model
K.clear_session()
return loss
def find_best_model(self):
search_result = gp.minimize(func=self.fitness, dimensions=self.param_space,acq_func='EI',n_calls=10)
return search_result
hptun = hptuning(input_df=df)
search_result = hptun.find_best_model()
print(search_result.fun)
现在我发现装饰器@use_named_args 没有在 class 中正常工作 (example code of scikit-optimize). 我收到错误消息
Traceback (most recent call last):
File "main.py", line 138, in <module>
class hptuning:
File "main.py", line 220, in hptuning
@use_named_args(dimensions=self.param_space)
NameError: name 'self' is not defined
这显然是关于装饰器在这种情况下的误用。
可能是由于我对此类装饰器的功能缺乏了解,我无法获得此 运行。有人可以帮我解决这个问题吗?
在此先感谢大家的支持!
self
未定义的问题与scikit.learn无关。您不能使用 self
来定义装饰器,因为它只在您正在装饰的方法内部定义。但是即使你回避了这个问题(例如通过提供 param_space 作为全局变量)我预计下一个问题将是 self
将被传递给 use_named_args
装饰器,但它只期望待优化参数。
最明显的解决方案是不在 fitness
方法上使用装饰器,而是在 find_best_model
方法内部定义一个调用 fitness
方法的装饰函数,例如这个:
def find_best_model(self):
@use_named_args(dimensions=self.param_space)
def fitness_wrapper(*args, **kwargs):
return self.fitness(*args, **kwargs)
search_result = gp.minimize(func=fitness_wrapper, dimensions=self.param_space,acq_func='EI',n_calls=10)
return search_result