You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

abl_model.py 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import pickle
  2. from typing import Any, Dict
  3. from ..data.structures import ListData
  4. from ..utils import reform_list
  5. class ABLModel:
  6. """
  7. Serialize data and provide a unified interface for different machine learning models.
  8. Parameters
  9. ----------
  10. base_model : Machine Learning Model
  11. The machine learning base model used for training and prediction. This model should
  12. implement the ``fit`` and ``predict`` methods. It's recommended, but not required, for the
  13. model to also implement the ``predict_proba`` method for generating
  14. predictions on the probabilities.
  15. """
  16. def __init__(self, base_model: Any) -> None:
  17. if not (hasattr(base_model, "fit") and hasattr(base_model, "predict")):
  18. raise NotImplementedError("The base_model should implement fit and predict methods.")
  19. self.base_model = base_model
  20. def predict(self, data_examples: ListData) -> Dict:
  21. """
  22. Predict the labels and probabilities for the given data.
  23. Parameters
  24. ----------
  25. data_examples : ListData
  26. A batch of data to predict on.
  27. Returns
  28. -------
  29. dict
  30. A dictionary containing the predicted labels and probabilities.
  31. """
  32. model = self.base_model
  33. data_X = data_examples.flatten("X")
  34. if hasattr(model, "predict_proba"):
  35. prob = model.predict_proba(X=data_X)
  36. label = prob.argmax(axis=1)
  37. prob = reform_list(prob, data_examples.X)
  38. else:
  39. prob = None
  40. label = model.predict(X=data_X)
  41. label = reform_list(label, data_examples.X)
  42. data_examples.pred_idx = label
  43. data_examples.pred_prob = prob
  44. return {"label": label, "prob": prob}
  45. def train(self, data_examples: ListData) -> float:
  46. """
  47. Train the model on the given data.
  48. Parameters
  49. ----------
  50. data_examples : ListData
  51. A batch of data to train on, which typically contains the data, ``X``, and the
  52. corresponding labels, ``abduced_idx``.
  53. Returns
  54. -------
  55. float
  56. The loss value of the trained model.
  57. """
  58. data_X = data_examples.flatten("X")
  59. data_y = data_examples.flatten("abduced_idx")
  60. return self.base_model.fit(X=data_X, y=data_y)
  61. def valid(self, data_examples: ListData) -> float:
  62. """
  63. Validate the model on the given data.
  64. Parameters
  65. ----------
  66. data_examples : ListData
  67. A batch of data to train on, which typically contains the data, ``X``,
  68. and the corresponding labels, ``abduced_idx``.
  69. Returns
  70. -------
  71. float
  72. The accuracy the trained model.
  73. """
  74. data_X = data_examples.flatten("X")
  75. data_y = data_examples.flatten("abduced_idx")
  76. score = self.base_model.score(X=data_X, y=data_y)
  77. return score
  78. def _model_operation(self, operation: str, *args, **kwargs):
  79. model = self.base_model
  80. if hasattr(model, operation):
  81. method = getattr(model, operation)
  82. method(*args, **kwargs)
  83. else:
  84. if f"{operation}_path" not in kwargs.keys():
  85. raise ValueError(f"'{operation}_path' should not be None")
  86. else:
  87. try:
  88. if operation == "save":
  89. with open(kwargs["save_path"], "wb") as file:
  90. pickle.dump(model, file, protocol=pickle.HIGHEST_PROTOCOL)
  91. elif operation == "load":
  92. with open(kwargs["load_path"], "rb") as file:
  93. self.base_model = pickle.load(file)
  94. except (OSError, pickle.PickleError):
  95. raise NotImplementedError(
  96. f"{type(model).__name__} object doesn't have the {operation} method \
  97. and the default pickle-based {operation} method failed."
  98. )
  99. def save(self, *args, **kwargs) -> None:
  100. """
  101. Save the model to a file.
  102. This method delegates to the ``save`` method of self.base_model. The arguments passed to
  103. this method should match those expected by the ``save`` method of self.base_model.
  104. """
  105. self._model_operation("save", *args, **kwargs)
  106. def load(self, *args, **kwargs) -> None:
  107. """
  108. Load the model from a file.
  109. This method delegates to the ``load`` method of self.base_model. The arguments passed to
  110. this method should match those expected by the ``load`` method of self.base_model.
  111. """
  112. self._model_operation("load", *args, **kwargs)

An efficient Python toolkit for Abductive Learning (ABL), a novel paradigm that integrates machine learning and logical reasoning in a unified framework.