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.

Learning.rst 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. `Learn the Basics <Basics.html>`_ ||
  2. `Quick Start <Quick-Start.html>`_ ||
  3. `Dataset & Data Structure <Datasets.html>`_ ||
  4. **Learning Part** ||
  5. `Reasoning Part <Reasoning.html>`_ ||
  6. `Evaluation Metrics <Evaluation.html>`_ ||
  7. `Bridge <Bridge.html>`_
  8. Learning Part
  9. =============
  10. ``ABLModel`` class serves as a unified interface to all machine learning models. Its constructor, the ``__init__`` method, takes a singular argument, ``base_model``. This argument denotes the fundamental machine learning model, which must implement the ``fit`` and ``predict`` methods.
  11. .. code:: python
  12. class ABLModel:
  13. def __init__(self, base_model: Any) -> None:
  14. if not (hasattr(base_model, "fit") and hasattr(base_model, "predict")):
  15. raise NotImplementedError("The base_model should implement fit and predict methods.")
  16. self.base_model = base_model
  17. All scikit-learn models satisify this requirements, so we can directly use the model to create an instance of ``ABLModel``. For example, we can customize our machine learning model by
  18. .. code:: python
  19. import sklearn
  20. from abl.learning import ABLModel
  21. base_model = sklearn.neighbors.KNeighborsClassifier(n_neighbors=3)
  22. model = ABLModel(base_model)
  23. For a PyTorch-based neural network, we first need to encapsulate it within a ``BasicNN`` object and then use this object to instantiate an instance of ``ABLModel``. For example, we can customize our machine learning model by
  24. .. code:: python
  25. # Load a PyTorch-based neural network
  26. import torchvision
  27. cls = torchvision.models.resnet18(pretrained=True)
  28. # criterion and optimizer are used for training
  29. criterion = torch.nn.CrossEntropyLoss()
  30. optimizer = torch.optim.Adam(cls.parameters())
  31. base_model = BasicNN(cls, criterion, optimizer)
  32. model = ABLModel(base_model)
  33. Besides ``fit`` and ``predict``, ``BasicNN`` also implements the following methods:
  34. +---------------------------+----------------------------------------+
  35. | Method | Function |
  36. +===========================+========================================+
  37. | train_epoch(data_loader) | Train the neural network for one epoch.|
  38. +---------------------------+----------------------------------------+
  39. | predict_proba(X) | Predict the class probabilities of X. |
  40. +---------------------------+----------------------------------------+
  41. | score(X, y) | Calculate the accuracy of the model on |
  42. | | test data. |
  43. +---------------------------+----------------------------------------+
  44. | save(epoch_id, save_path) | Save the model. |
  45. +---------------------------+----------------------------------------+
  46. | load(load_path) | Load the model. |
  47. +---------------------------+----------------------------------------+

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