英文:
Pickle python closure
问题
这是我为我的lightGBM模型编写的自定义目标函数。然而,由于闭包,我无法将我的模型进行pickle。是否有解决方法?我的目标函数如下:
def custom_asymmetric_train_wrapper(delta,tau):
def custom_asymmetric_train(y_true, y_pred):
residual = (y_true - y_pred).astype("float")
grad = np.where(residual>0, -2*delta*residual, -2*residual)
hess = np.where(residual>0, 2*tau, 2.0)
return grad, hess
return custom_asymmetric_train
我已经尝试过使用joblib以及将参数delta和tau添加到custom_asymmetric_train函数中。
英文:
This is my custom objective function for my lightGBM model. However, I cannot pickle my model due to the closure. Is there a work around? My objective function can be seen below:
def custom_asymmetric_train_wrapper(delta,tau):
def custom_asymmetric_train(y_true, y_pred):
residual = (y_true - y_pred).astype("float")
grad = np.where(residual>0, -2*delta*residual, -2*residual)
hess = np.where(residual>0, 2*tau, 2.0)
return grad, hess
return custom_asymmetric_train
I have tried joblib as well as adding the parameters delta and tau to the custom_asymmetric_train function
答案1
得分: 1
以下是已翻译的代码部分:
闭包保存了每次调用时使用的 `delta` 和 `tau` 值。但是使用类也可以实现相同的功能,只需要多写一些代码。
class CustomAsymmetricTrain:
def __init__(self, delta, tau):
self.delta = delta
self.tau = tau
def __call__(self, y_true, y_pred):
residual = (y_true - y_pred).astype("float")
grad = np.where(residual > 0, -2*self.delta*residual, -2*residual)
hess = np.where(residual > 0, 2*self.tau, 2.0)
return grad, hess
使用给定的 `delta` 和 `tau` 值实例化类,然后根据需要调用生成的对象。使用高度虚构的数据:
custom_asymmetric_train_1 = CustomAsymmetricTrain(1, 2)
result1 = custom_asymmetric_train_1(3, 4)
result2 = custom_asymmetric_train(4, 5)
英文:
The closure holds the delta
and tau
values used on each call. But classes are picklable and do the same thing with a bit more typing.
class CustomAsymmetricTrain:
def __init__(self, delta, tau):
self.delta = delta
self.tau = tau
def __call__(self, y_true, y_pred):
residual = (y_true - y_pred).astype("float")
grad = np.where(residual>0, -2*self.delta*residual, -2*residual)
hess = np.where(residual>0, 2*self.tau, 2.0)
return grad, hess
Instantiate the class with delta
and tau
values and then call the resulting object as needed. Using highly contrived data:
custom_asymetric_train_1 = CustomAsymmetricTrain(1, 2)
result1 = custom_asymetric_train_1(3, 4)
result2 = custom_asymetric_train(4, 5)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论