Python 中的 Pickle 闭包

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

Pickle python closure

问题

  1. 这是我为我的lightGBM模型编写的自定义目标函数然而由于闭包我无法将我的模型进行pickle是否有解决方法我的目标函数如下
  2. def custom_asymmetric_train_wrapper(delta,tau):
  3. def custom_asymmetric_train(y_true, y_pred):
  4. residual = (y_true - y_pred).astype("float")
  5. grad = np.where(residual>0, -2*delta*residual, -2*residual)
  6. hess = np.where(residual>0, 2*tau, 2.0)
  7. return grad, hess
  8. return custom_asymmetric_train
  9. 我已经尝试过使用joblib以及将参数deltatau添加到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:

  1. def custom_asymmetric_train_wrapper(delta,tau):
  2. def custom_asymmetric_train(y_true, y_pred):
  3. residual = (y_true - y_pred).astype("float")
  4. grad = np.where(residual>0, -2*delta*residual, -2*residual)
  5. hess = np.where(residual>0, 2*tau, 2.0)
  6. return grad, hess
  7. 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

以下是已翻译的代码部分:

  1. 闭包保存了每次调用时使用的 `delta` `tau` 但是使用类也可以实现相同的功能只需要多写一些代码
  2. class CustomAsymmetricTrain:
  3. def __init__(self, delta, tau):
  4. self.delta = delta
  5. self.tau = tau
  6. def __call__(self, y_true, y_pred):
  7. residual = (y_true - y_pred).astype("float")
  8. grad = np.where(residual > 0, -2*self.delta*residual, -2*residual)
  9. hess = np.where(residual > 0, 2*self.tau, 2.0)
  10. return grad, hess
  11. 使用给定的 `delta` `tau` 值实例化类然后根据需要调用生成的对象使用高度虚构的数据
  12. custom_asymmetric_train_1 = CustomAsymmetricTrain(1, 2)
  13. result1 = custom_asymmetric_train_1(3, 4)
  14. 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.

  1. class CustomAsymmetricTrain:
  2. def __init__(self, delta, tau):
  3. self.delta = delta
  4. self.tau = tau
  5. def __call__(self, y_true, y_pred):
  6. residual = (y_true - y_pred).astype("float")
  7. grad = np.where(residual>0, -2*self.delta*residual, -2*residual)
  8. hess = np.where(residual>0, 2*self.tau, 2.0)
  9. return grad, hess

Instantiate the class with delta and tau values and then call the resulting object as needed. Using highly contrived data:

  1. custom_asymetric_train_1 = CustomAsymmetricTrain(1, 2)
  2. result1 = custom_asymetric_train_1(3, 4)
  3. 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:

确定