TransformedTargetRegressor 保存和加载错误

TransformedTargetRegressor save and load error

我正在使用 TransformedTargetRegressor 定义我的自定义回归器,将其添加到管道并将模型保存在 'joblib' 文件中。但是,当我尝试加载模型时,出现错误

module 'main' has no attribute 'transform_targets'

其中 transform_targets 是为回归量定义的函数之一

def transform_targets(targets):
   targets = (targets - min_t)/(max_t-min_t)
   return targets

def inv_transform_targets(outputs):
   outputs = (outputs)*(max_t-min_t)+min_t
   return outputs

# Define the model 

mlp_model = MLPRegressor(activation = 'relu', validation_fraction = 0.2, hidden_layer_sizes=(1000, ))
full_model = TransformedTargetRegressor(regressor = mlp_model, func = transform_targets,
                                 inverse_func = inv_transform_targets)

# Incorporate feature scaling via pipeline

pipeline = make_pipeline(MinMaxScaler(), full_model)
nn_model = pipeline.fit(X_train,y_train)

# Fit the model which uses the transformed target regressor + maxmin pipeline

nn_model.fit(X_train,y_train)

from joblib import dump, load
dump(nn_model, 'fitness_nn_C1.joblib')

该模型运行良好且预测良好,保存时没有错误,但不会加载回来。如果我用泡菜保存它也会returns一个类似的错误

AttributeError: Can't get attribute 'transform_targets' on module 'main'>

有谁知道如何将包含 TransformedTargetRegressor 的模型保存在一个文件中,然后可以成功重新加载?我意识到我可以将与转换目标相关的参数/函数转储到一个单独的文件中,但这正是我想要避免的

编辑:

当前的解决方法是使用 MinMaxScaler 作为转换器,或使用预处理批次中的任何其他转换器,但仍然不知道是否可以在此工作流程中包含自定义函数

问题是当您尝试加载文件时它无法解析最初未转储的 transform_targets。你可以使用 dill 来序列化它。所以基本上你必须创建一个你想要转储的项目列表,然后使用 dilljoblib 来序列化它们,如下所示:

from sklearn.neural_network import MLPRegressor
from sklearn.compose import TransformedTargetRegressor
from sklearn.pipeline import make_pipeline
from sklearn.datasets import make_friedman1
from sklearn.preprocessing import MinMaxScaler
import dill
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)

min_t = 10
max_t = 300
def transform_targets(targets):
   targets = (targets - min_t)/(max_t-min_t)
   return targets

def inv_transform_targets(outputs):
   outputs = (outputs)*(max_t-min_t)+min_t
   return outputs

# Define the model 

mlp_model = MLPRegressor(activation = 'relu', validation_fraction = 0.2, hidden_layer_sizes=(1000, ))
full_model = TransformedTargetRegressor(regressor = mlp_model, func = transform_targets,
                                 inverse_func = inv_transform_targets)

# Incorporate feature scaling via pipeline

pipeline = make_pipeline(MinMaxScaler(), full_model)
nn_model = pipeline.fit(X,y)

# Fit the model which uses the transformed target regressor + maxmin pipeline

nn_model.fit(X,y)
to_save = [transform_targets, inv_transform_targets, nn_model]
r = dill.dumps(to_save)
from joblib import dump, load
dump(r, 'fitness_nn_C1.joblib')

现在您可以加载它,如下所示:

from joblib import dump, load
import dill
Q = load('fitness_nn_C1.joblib')
T = dill.loads(Q)

T 看起来像这样:

[<function __main__.transform_targets(targets)>,
 <function __main__.inv_transform_targets(outputs)>,
 Pipeline(memory=None,
          steps=[('minmaxscaler', MinMaxScaler(copy=True, feature_range=(0, 1))),
                 ('transformedtargetregressor',
                  TransformedTargetRegressor(check_inverse=True,
                                             func=<function transform_targets at 0x000001F486D27048>,
                                             inverse_func=<function inv_transform_targets at 0x000001F4882E6C80>,
                                             regressor=MLPRegressor(activation='relu',
                                                                    alpha=0.0001,
                                                                    batch_size='a...
                                                                    beta_2=0.999,
                                                                    early_stopping=False,
                                                                    epsilon=1e-08,
                                                                    hidden_layer_sizes=(1000,),
                                                                    learning_rate='constant',
                                                                    learning_rate_init=0.001,
                                                                    max_iter=200,
                                                                    momentum=0.9,
                                                                    n_iter_no_change=10,
                                                                    nesterovs_momentum=True,
                                                                    power_t=0.5,
                                                                    random_state=None,
                                                                    shuffle=True,
                                                                    solver='adam',
                                                                    tol=0.0001,
                                                                    validation_fraction=0.2,
                                                                    verbose=False,
                                                                    warm_start=False),
                                             transformer=None))],
          verbose=False)]

希望对您有所帮助!