Python 中的 Pickle 闭包

huangapple go评论64阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年6月26日 03:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定