Browse Source

[ENH] change code from abl to ablkit

pull/5/head
troyyyyy 1 year ago
parent
commit
dab233c16a
61 changed files with 113 additions and 113 deletions
  1. +6
    -6
      README.md
  2. +0
    -0
      ablkit/__init__.py
  3. +0
    -0
      ablkit/bridge/__init__.py
  4. +0
    -0
      ablkit/bridge/base_bridge.py
  5. +0
    -0
      ablkit/bridge/simple_bridge.py
  6. +0
    -0
      ablkit/data/__init__.py
  7. +1
    -1
      ablkit/data/data_converter.py
  8. +0
    -0
      ablkit/data/evaluation/__init__.py
  9. +0
    -0
      ablkit/data/evaluation/base_metric.py
  10. +0
    -0
      ablkit/data/evaluation/reasoning_metric.py
  11. +0
    -0
      ablkit/data/evaluation/symbol_accuracy.py
  12. +0
    -0
      ablkit/data/structures/__init__.py
  13. +0
    -0
      ablkit/data/structures/base_data_element.py
  14. +1
    -1
      ablkit/data/structures/list_data.py
  15. +0
    -0
      ablkit/learning/__init__.py
  16. +0
    -0
      ablkit/learning/abl_model.py
  17. +0
    -0
      ablkit/learning/basic_nn.py
  18. +0
    -0
      ablkit/learning/model_converter.py
  19. +0
    -0
      ablkit/learning/torch_dataset/__init__.py
  20. +0
    -0
      ablkit/learning/torch_dataset/classification_dataset.py
  21. +0
    -0
      ablkit/learning/torch_dataset/prediction_dataset.py
  22. +0
    -0
      ablkit/learning/torch_dataset/regression_dataset.py
  23. +0
    -0
      ablkit/reasoning/__init__.py
  24. +0
    -0
      ablkit/reasoning/kb.py
  25. +0
    -0
      ablkit/reasoning/reasoner.py
  26. +0
    -0
      ablkit/utils/__init__.py
  27. +0
    -0
      ablkit/utils/cache.py
  28. +0
    -0
      ablkit/utils/logger.py
  29. +0
    -0
      ablkit/utils/manager.py
  30. +0
    -0
      ablkit/utils/utils.py
  31. +2
    -2
      docs/API/abl.bridge.rst
  32. +3
    -3
      docs/API/abl.data.rst
  33. +4
    -4
      docs/API/abl.learning.rst
  34. +2
    -2
      docs/API/abl.reasoning.rst
  35. +2
    -2
      docs/API/abl.utils.rst
  36. +2
    -2
      docs/Examples/HED.rst
  37. +6
    -6
      docs/Examples/HWF.rst
  38. +6
    -6
      docs/Examples/MNISTAdd.rst
  39. +5
    -5
      docs/Examples/Zoo.rst
  40. +2
    -2
      docs/Intro/Bridge.rst
  41. +3
    -3
      docs/Intro/Datasets.rst
  42. +1
    -1
      docs/Intro/Evaluation.rst
  43. +1
    -1
      docs/Intro/Learning.rst
  44. +6
    -6
      docs/Intro/Quick-Start.rst
  45. +1
    -1
      docs/Intro/Reasoning.rst
  46. +5
    -5
      docs/index.rst
  47. +7
    -7
      examples/hed/bridge.py
  48. +3
    -3
      examples/hed/consistency_metric.py
  49. +2
    -2
      examples/hed/hed.ipynb
  50. +2
    -2
      examples/hed/main.py
  51. +2
    -2
      examples/hed/reasoning/reasoning.py
  52. +6
    -6
      examples/hwf/hwf.ipynb
  53. +5
    -5
      examples/hwf/main.py
  54. +5
    -5
      examples/mnist_add/main.py
  55. +6
    -6
      examples/mnist_add/mnist_add.ipynb
  56. +1
    -1
      examples/zoo/kb.py
  57. +5
    -5
      examples/zoo/main.py
  58. +5
    -5
      examples/zoo/zoo.ipynb
  59. +3
    -3
      tests/conftest.py
  60. +1
    -1
      tests/test_abl_model.py
  61. +1
    -1
      tests/test_reasoning.py

+ 6
- 6
README.md View File

@@ -113,7 +113,7 @@ To facilitate uniform processing, ABL Kit provides the `BasicNN` class to conver


```python ```python
​import torch ​import torch
​from abl.learning import BasicNN
​from ablkit.learning import BasicNN
​loss_fn = torch.nn.CrossEntropyLoss() ​loss_fn = torch.nn.CrossEntropyLoss()
​optimizer = torch.optim.RMSprop(cls.parameters(), lr=0.001, alpha=0.9) ​optimizer = torch.optim.RMSprop(cls.parameters(), lr=0.001, alpha=0.9)
@@ -124,7 +124,7 @@ To facilitate uniform processing, ABL Kit provides the `BasicNN` class to conver
The base model built above is trained to make predictions on instance-level data (e.g., a single image), while ABL deals with example-level data. To bridge this gap, we wrap the base_model into an instance of `ABLModel`. This class serves as a unified wrapper for base models, facilitating the learning part to train, test, and predict on example-level data, (e.g., images that comprise an equation). The base model built above is trained to make predictions on instance-level data (e.g., a single image), while ABL deals with example-level data. To bridge this gap, we wrap the base_model into an instance of `ABLModel`. This class serves as a unified wrapper for base models, facilitating the learning part to train, test, and predict on example-level data, (e.g., images that comprise an equation).


```python ```python
from abl.learning import ABLModel
from ablkit.learning import ABLModel
​model = ABLModel(base_model) ​model = ABLModel(base_model)
``` ```
@@ -138,7 +138,7 @@ from abl.learning import ABLModel
To build the reasoning part, we first define a knowledge base by creating a subclass of `KBBase`. In the subclass, we initialize the `pseudo_label_list` parameter and override the `logic_forward` method, which specifies how to perform (deductive) reasoning that processes pseudo-labels of an example to the corresponding reasoning result. Specifically, for the MNIST Addition task, this `logic_forward` method is tailored to execute the sum operation. To build the reasoning part, we first define a knowledge base by creating a subclass of `KBBase`. In the subclass, we initialize the `pseudo_label_list` parameter and override the `logic_forward` method, which specifies how to perform (deductive) reasoning that processes pseudo-labels of an example to the corresponding reasoning result. Specifically, for the MNIST Addition task, this `logic_forward` method is tailored to execute the sum operation.


```python ```python
from abl.reasoning import KBBase
from ablkit.reasoning import KBBase
class AddKB(KBBase): class AddKB(KBBase):
def __init__(self, pseudo_label_list=list(range(10))): def __init__(self, pseudo_label_list=list(range(10))):
@@ -153,7 +153,7 @@ kb = AddKB()
Next, we create a reasoner by instantiating the class `Reasoner`, passing the knowledge base as a parameter. Due to the indeterminism of abductive reasoning, there could be multiple candidate pseudo-labels compatible to the knowledge base. In such scenarios, the reasoner can minimize inconsistency and return the pseudo-label with the highest consistency. Next, we create a reasoner by instantiating the class `Reasoner`, passing the knowledge base as a parameter. Due to the indeterminism of abductive reasoning, there could be multiple candidate pseudo-labels compatible to the knowledge base. In such scenarios, the reasoner can minimize inconsistency and return the pseudo-label with the highest consistency.


```python ```python
from abl.reasoning import Reasoner
from ablkit.reasoning import Reasoner
reasoner = Reasoner(kb) reasoner = Reasoner(kb)
``` ```
@@ -167,7 +167,7 @@ reasoner = Reasoner(kb)
ABL Kit provides two basic metrics, namely `SymbolAccuracy` and `ReasoningMetric`, which are used to evaluate the accuracy of the machine learning model's predictions and the accuracy of the `logic_forward` results, respectively. ABL Kit provides two basic metrics, namely `SymbolAccuracy` and `ReasoningMetric`, which are used to evaluate the accuracy of the machine learning model's predictions and the accuracy of the `logic_forward` results, respectively.


```python ```python
from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy
metric_list = [SymbolAccuracy(), ReasoningMetric(kb=kb)] metric_list = [SymbolAccuracy(), ReasoningMetric(kb=kb)]
``` ```
@@ -182,7 +182,7 @@ Now, we use `SimpleBridge` to combine learning and reasoning in a
unified ABL framework. unified ABL framework.


```python ```python
from abl.bridge import SimpleBridge
from ablkit.bridge import SimpleBridge
bridge = SimpleBridge(model, reasoner, metric_list) bridge = SimpleBridge(model, reasoner, metric_list)
``` ```


abl/__init__.py → ablkit/__init__.py View File


abl/bridge/__init__.py → ablkit/bridge/__init__.py View File


abl/bridge/base_bridge.py → ablkit/bridge/base_bridge.py View File


abl/bridge/simple_bridge.py → ablkit/bridge/simple_bridge.py View File


abl/data/__init__.py → ablkit/data/__init__.py View File


abl/data/data_converter.py → ablkit/data/data_converter.py View File

@@ -1,6 +1,6 @@
from typing import Any, Tuple from typing import Any, Tuple


from abl.utils import tab_data_to_tuple
from ablkit.utils import tab_data_to_tuple
from .structures.list_data import ListData from .structures.list_data import ListData
from lambdaLearn.Base.TabularMixin import TabularMixin from lambdaLearn.Base.TabularMixin import TabularMixin



abl/data/evaluation/__init__.py → ablkit/data/evaluation/__init__.py View File


abl/data/evaluation/base_metric.py → ablkit/data/evaluation/base_metric.py View File


abl/data/evaluation/reasoning_metric.py → ablkit/data/evaluation/reasoning_metric.py View File


abl/data/evaluation/symbol_accuracy.py → ablkit/data/evaluation/symbol_accuracy.py View File


abl/data/structures/__init__.py → ablkit/data/structures/__init__.py View File


abl/data/structures/base_data_element.py → ablkit/data/structures/base_data_element.py View File


abl/data/structures/list_data.py → ablkit/data/structures/list_data.py View File

@@ -57,7 +57,7 @@ class ListData(BaseDataElement):
class implemented in `MMEngine <https://github.com/open-mmlab/mmengine/blob/main/mmengine/structures/base_data_element.py>`_. # noqa: E501 class implemented in `MMEngine <https://github.com/open-mmlab/mmengine/blob/main/mmengine/structures/base_data_element.py>`_. # noqa: E501


Examples: Examples:
>>> from abl.data.structures import ListData
>>> from ablkit.data.structures import ListData
>>> import numpy as np >>> import numpy as np
>>> import torch >>> import torch
>>> data_examples = ListData() >>> data_examples = ListData()

abl/learning/__init__.py → ablkit/learning/__init__.py View File


abl/learning/abl_model.py → ablkit/learning/abl_model.py View File


abl/learning/basic_nn.py → ablkit/learning/basic_nn.py View File


abl/learning/model_converter.py → ablkit/learning/model_converter.py View File


abl/learning/torch_dataset/__init__.py → ablkit/learning/torch_dataset/__init__.py View File


abl/learning/torch_dataset/classification_dataset.py → ablkit/learning/torch_dataset/classification_dataset.py View File


abl/learning/torch_dataset/prediction_dataset.py → ablkit/learning/torch_dataset/prediction_dataset.py View File


abl/learning/torch_dataset/regression_dataset.py → ablkit/learning/torch_dataset/regression_dataset.py View File


abl/reasoning/__init__.py → ablkit/reasoning/__init__.py View File


abl/reasoning/kb.py → ablkit/reasoning/kb.py View File


abl/reasoning/reasoner.py → ablkit/reasoning/reasoner.py View File


abl/utils/__init__.py → ablkit/utils/__init__.py View File


abl/utils/cache.py → ablkit/utils/cache.py View File


abl/utils/logger.py → ablkit/utils/logger.py View File


abl/utils/manager.py → ablkit/utils/manager.py View File


abl/utils/utils.py → ablkit/utils/utils.py View File


+ 2
- 2
docs/API/abl.bridge.rst View File

@@ -1,7 +1,7 @@
abl.bridge
ablkit.bridge
================== ==================


.. automodule:: abl.bridge
.. automodule:: ablkit.bridge
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

+ 3
- 3
docs/API/abl.data.rst View File

@@ -1,10 +1,10 @@
abl.data
ablkit.data
=================== ===================


``structures`` ``structures``
-------------- --------------


.. autoclass:: abl.data.structures.ListData
.. autoclass:: ablkit.data.structures.ListData
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
@@ -12,7 +12,7 @@ abl.data
``evaluation`` ``evaluation``
-------------- --------------


.. automodule:: abl.data.evaluation
.. automodule:: ablkit.data.evaluation
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

+ 4
- 4
docs/API/abl.learning.rst View File

@@ -1,12 +1,12 @@
abl.learning
ablkit.learning
================== ==================


.. autoclass:: abl.learning.ABLModel
.. autoclass:: ablkit.learning.ABLModel
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:


.. autoclass:: abl.learning.BasicNN
.. autoclass:: ablkit.learning.BasicNN
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
@@ -14,7 +14,7 @@ abl.learning
``torch_dataset`` ``torch_dataset``
----------------- -----------------


.. automodule:: abl.learning.torch_dataset
.. automodule:: ablkit.learning.torch_dataset
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

+ 2
- 2
docs/API/abl.reasoning.rst View File

@@ -1,7 +1,7 @@
abl.reasoning
ablkit.reasoning
================== ==================


.. automodule:: abl.reasoning
.. automodule:: ablkit.reasoning
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

+ 2
- 2
docs/API/abl.utils.rst View File

@@ -1,7 +1,7 @@
abl.utils
ablkit.utils
================== ==================


.. automodule:: abl.utils
.. automodule:: ablkit.utils
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:

+ 2
- 2
docs/Examples/HED.rst View File

@@ -34,8 +34,8 @@ model.
import torch import torch
import torch.nn as nn import torch.nn as nn


from abl.learning import ABLModel, BasicNN
from abl.utils import ABLLogger, print_log
from ablkit.learning import ABLModel, BasicNN
from ablkit.utils import ABLLogger, print_log


from bridge import HedBridge from bridge import HedBridge
from consistency_metric import ConsistencyMetric from consistency_metric import ConsistencyMetric


+ 6
- 6
docs/Examples/HWF.rst View File

@@ -32,11 +32,11 @@ machine learning model.
import torch import torch
import torch.nn as nn import torch.nn as nn


from abl.bridge import SimpleBridge
from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from abl.learning import ABLModel, BasicNN
from abl.reasoning import KBBase, Reasoner
from abl.utils import ABLLogger, print_log
from ablkit.bridge import SimpleBridge
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.learning import ABLModel, BasicNN
from ablkit.reasoning import KBBase, Reasoner
from ablkit.utils import ABLLogger, print_log


from datasets import get_dataset from datasets import get_dataset
from models.nn import SymbolNet from models.nn import SymbolNet
@@ -232,7 +232,7 @@ examples.


.. code:: ipython3 .. code:: ipython3


from abl.data.structures import ListData
from ablkit.data.structures import ListData
# ListData is a data structure provided by ABL Kit that can be used to organize data examples # ListData is a data structure provided by ABL Kit that can be used to organize data examples
data_examples = ListData() data_examples = ListData()
# We use the first 1001st and 3001st data examples in the training set as an illustration # We use the first 1001st and 3001st data examples in the training set as an illustration


+ 6
- 6
docs/Examples/MNISTAdd.rst View File

@@ -31,11 +31,11 @@ machine learning model.
import torch.nn as nn import torch.nn as nn
from torch.optim import RMSprop, lr_scheduler from torch.optim import RMSprop, lr_scheduler


from abl.bridge import SimpleBridge
from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from abl.learning import ABLModel, BasicNN
from abl.reasoning import KBBase, Reasoner
from abl.utils import ABLLogger, print_log
from ablkit.bridge import SimpleBridge
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.learning import ABLModel, BasicNN
from ablkit.reasoning import KBBase, Reasoner
from ablkit.utils import ABLLogger, print_log


from datasets import get_dataset from datasets import get_dataset
from models.nn import LeNet5 from models.nn import LeNet5
@@ -202,7 +202,7 @@ examples.


.. code:: ipython3 .. code:: ipython3


from abl.data.structures import ListData
from ablkit.data.structures import ListData
# ListData is a data structure provided by ABL Kit that can be used to organize data examples # ListData is a data structure provided by ABL Kit that can be used to organize data examples
data_examples = ListData() data_examples = ListData()
# We use the first 100 data examples in the training set as an illustration # We use the first 100 data examples in the training set as an illustration


+ 5
- 5
docs/Examples/Zoo.rst View File

@@ -28,11 +28,11 @@ further update the learning model.
import numpy as np import numpy as np
from sklearn.ensemble import RandomForestClassifier from sklearn.ensemble import RandomForestClassifier


from abl.bridge import SimpleBridge
from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from abl.learning import ABLModel
from abl.reasoning import Reasoner
from abl.utils import ABLLogger, confidence_dist, print_log, tab_data_to_tuple
from ablkit.bridge import SimpleBridge
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.learning import ABLModel
from ablkit.reasoning import Reasoner
from ablkit.utils import ABLLogger, confidence_dist, print_log, tab_data_to_tuple


from get_dataset import load_and_preprocess_dataset, split_dataset from get_dataset import load_and_preprocess_dataset, split_dataset
from kb import ZooKB from kb import ZooKB


+ 2
- 2
docs/Intro/Bridge.rst View File

@@ -14,7 +14,7 @@ In this section, we will look at how to bridge learning and reasoning parts to t


.. code:: python .. code:: python


from abl.bridge import BaseBridge, SimpleBridge
from ablkit.bridge import BaseBridge, SimpleBridge


``BaseBridge`` is an abstract class with the following initialization parameters: ``BaseBridge`` is an abstract class with the following initialization parameters:


@@ -42,7 +42,7 @@ In this section, we will look at how to bridge learning and reasoning parts to t
| ``test(test_data)`` | Test the model. | | ``test(test_data)`` | Test the model. |
+---------------------------------------+----------------------------------------------------+ +---------------------------------------+----------------------------------------------------+


where ``train_data`` and ``test_data`` are both in the form of a tuple or a `ListData <../API/abl.data.html#structures.ListData>`_. Regardless of the form, they all need to include three components: ``X``, ``gt_pseudo_label`` and ``Y``. Since ``ListData`` is the underlying data structure used throughout the ABL Kit, tuple-formed data will be firstly transformed into ``ListData`` in the ``train`` and ``test`` methods, and such ``ListData`` instances are referred to as ``data_examples``. More details can be found in `preparing datasets <Datasets.html>`_.
where ``train_data`` and ``test_data`` are both in the form of a tuple or a `ListData <../API/ablkit.data.html#structures.ListData>`_. Regardless of the form, they all need to include three components: ``X``, ``gt_pseudo_label`` and ``Y``. Since ``ListData`` is the underlying data structure used throughout the ABL Kit, tuple-formed data will be firstly transformed into ``ListData`` in the ``train`` and ``test`` methods, and such ``ListData`` instances are referred to as ``data_examples``. More details can be found in `preparing datasets <Datasets.html>`_.


``SimpleBridge`` inherits from ``BaseBridge`` and provides a basic implementation. Besides the ``model`` and ``reasoner``, ``SimpleBridge`` has an extra initialization argument, ``metric_list``, which will be used to evaluate model performance. Its training process involves several Abductive Learning loops and each loop consists of the following five steps: ``SimpleBridge`` inherits from ``BaseBridge`` and provides a basic implementation. Besides the ``model`` and ``reasoner``, ``SimpleBridge`` has an extra initialization argument, ``metric_list``, which will be used to evaluate model performance. Its training process involves several Abductive Learning loops and each loop consists of the following five steps:




+ 3
- 3
docs/Intro/Datasets.rst View File

@@ -15,7 +15,7 @@ In this section, we will look at the dataset and data structure in ABL Kit.
.. code:: python .. code:: python


import torch import torch
from abl.data.structures import ListData
from ablkit.data.structures import ListData


Dataset Dataset
------- -------
@@ -62,11 +62,11 @@ where each sublist in ``X``, e.g., |data_example|, is a data example and each im
Data Structure Data Structure
-------------- --------------


Besides the user-provided dataset, various forms of data are utilized and dynamicly generated throughout the training and testing process of ABL framework. Examples include raw data, predicted pseudo-label, abduced pseudo-label, pseudo-label indices, etc. To manage this diversity and ensure a stable, versatile interface, ABL Kit employs `abstract data interfaces <../API/abl.data.html#structure>`_ to encapsulate different forms of data that will be used in the total learning process.
Besides the user-provided dataset, various forms of data are utilized and dynamicly generated throughout the training and testing process of ABL framework. Examples include raw data, predicted pseudo-label, abduced pseudo-label, pseudo-label indices, etc. To manage this diversity and ensure a stable, versatile interface, ABL Kit employs `abstract data interfaces <../API/ablkit.data.html#structure>`_ to encapsulate different forms of data that will be used in the total learning process.


``ListData`` is the underlying abstract data interface utilized in ABL Kit. As the fundamental data structure, ``ListData`` implements commonly used data manipulation methods and is responsible for transferring data between various components of ABL, ensuring that stages such as prediction, abductive reasoning, and training can utilize ``ListData`` as a unified input format. Before proceeding to other stages, user-provided datasets will be firstly converted into ``ListData``. ``ListData`` is the underlying abstract data interface utilized in ABL Kit. As the fundamental data structure, ``ListData`` implements commonly used data manipulation methods and is responsible for transferring data between various components of ABL, ensuring that stages such as prediction, abductive reasoning, and training can utilize ``ListData`` as a unified input format. Before proceeding to other stages, user-provided datasets will be firstly converted into ``ListData``.


Besides providing a tuple of ``(X, gt_pseudo_label, Y)``, ABL Kit also allows users to directly supply data in ``ListData`` format, which similarly requires the inclusion of these three attributes. The following code shows the basic usage of ``ListData``. More information can be found in the `API documentation <../API/abl.data.html#structure>`_.
Besides providing a tuple of ``(X, gt_pseudo_label, Y)``, ABL Kit also allows users to directly supply data in ``ListData`` format, which similarly requires the inclusion of these three attributes. The following code shows the basic usage of ``ListData``. More information can be found in the `API documentation <../API/ablkit.data.html#structure>`_.


.. code-block:: python .. code-block:: python




+ 1
- 1
docs/Intro/Evaluation.rst View File

@@ -14,7 +14,7 @@ In this section, we will look at how to build evaluation metrics.


.. code:: python .. code:: python


from abl.data.evaluation import BaseMetric, SymbolAccuracy, ReasoningMetric
from ablkit.data.evaluation import BaseMetric, SymbolAccuracy, ReasoningMetric


ABL Kit seperates the evaluation process from model training and testing as an independent class, ``BaseMetric``. The training and testing processes are implemented in the ``BaseBridge`` class, so metrics are used by this class and its sub-classes. After building a ``bridge`` with a list of ``BaseMetric`` instances, these metrics will be used by the ``bridge.valid`` method to evaluate the model performance during training and testing. ABL Kit seperates the evaluation process from model training and testing as an independent class, ``BaseMetric``. The training and testing processes are implemented in the ``BaseBridge`` class, so metrics are used by this class and its sub-classes. After building a ``bridge`` with a list of ``BaseMetric`` instances, these metrics will be used by the ``bridge.valid`` method to evaluate the model performance during training and testing.




+ 1
- 1
docs/Intro/Learning.rst View File

@@ -21,7 +21,7 @@ In ABL Kit, building the learning part involves two steps:


import sklearn import sklearn
import torchvision import torchvision
from abl.learning import BasicNN, ABLModel
from ablkit.learning import BasicNN, ABLModel


Building a base model Building a base model
--------------------- ---------------------


+ 6
- 6
docs/Intro/Quick-Start.rst View File

@@ -48,7 +48,7 @@ To facilitate uniform processing, ABL Kit provides the ``BasicNN`` class to conv
.. code:: python .. code:: python


import torch import torch
from abl.learning import BasicNN
from ablkit.learning import BasicNN


loss_fn = torch.nn.CrossEntropyLoss() loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.RMSprop(cls.parameters(), lr=0.001) optimizer = torch.optim.RMSprop(cls.parameters(), lr=0.001)
@@ -59,7 +59,7 @@ The base model built above is trained to make predictions on instance-level data


.. code:: python .. code:: python


from abl.learning import ABLModel
from ablkit.learning import ABLModel


model = ABLModel(base_model) model = ABLModel(base_model)


@@ -72,7 +72,7 @@ To build the reasoning part, we first define a knowledge base by creating a subc


.. code:: python .. code:: python


from abl.reasoning import KBBase
from ablkit.reasoning import KBBase


class AddKB(KBBase): class AddKB(KBBase):
def __init__(self, pseudo_label_list=list(range(10))): def __init__(self, pseudo_label_list=list(range(10))):
@@ -89,7 +89,7 @@ In such scenarios, the reasoner can minimize inconsistency and return the pseudo


.. code:: python .. code:: python


from abl.reasoning import Reasoner
from ablkit.reasoning import Reasoner
reasoner = Reasoner(kb) reasoner = Reasoner(kb)


@@ -102,7 +102,7 @@ ABL Kit provides two basic metrics, namely ``SymbolAccuracy`` and ``ReasoningMet


.. code:: python .. code:: python


from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy


metric_list = [SymbolAccuracy(), ReasoningMetric(kb=kb)] metric_list = [SymbolAccuracy(), ReasoningMetric(kb=kb)]


@@ -115,7 +115,7 @@ Now, we use ``SimpleBridge`` to combine learning and reasoning in a unified ABL


.. code:: python .. code:: python


from abl.bridge import SimpleBridge
from ablkit.bridge import SimpleBridge


bridge = SimpleBridge(model, reasoner, metric_list) bridge = SimpleBridge(model, reasoner, metric_list)




+ 1
- 1
docs/Intro/Reasoning.rst View File

@@ -22,7 +22,7 @@ In ABL Kit, building the reasoning part involves two steps:


.. code:: python .. code:: python


from abl.reasoning import KBBase, GroundKB, PrologKB, Reasoner
from ablkit.reasoning import KBBase, GroundKB, PrologKB, Reasoner


Building a knowledge base Building a knowledge base
------------------------- -------------------------


+ 5
- 5
docs/index.rst View File

@@ -32,11 +32,11 @@
:maxdepth: 1 :maxdepth: 1
:caption: API :caption: API


API/abl.data
API/abl.learning
API/abl.reasoning
API/abl.bridge
API/abl.utils
API/ablkit.data
API/ablkit.learning
API/ablkit.reasoning
API/ablkit.bridge
API/ablkit.utils


.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 1


+ 7
- 7
examples/hed/bridge.py View File

@@ -4,13 +4,13 @@ from typing import Any, List, Optional, Tuple, Union


import torch import torch


from abl.bridge import SimpleBridge
from abl.data.evaluation import BaseMetric
from abl.data.structures import ListData
from abl.learning import ABLModel, BasicNN
from abl.learning.torch_dataset import RegressionDataset
from abl.reasoning import Reasoner
from abl.utils import print_log
from ablkit.bridge import SimpleBridge
from ablkit.data.evaluation import BaseMetric
from ablkit.data.structures import ListData
from ablkit.learning import ABLModel, BasicNN
from ablkit.learning.torch_dataset import RegressionDataset
from ablkit.reasoning import Reasoner
from ablkit.utils import print_log


from datasets import get_pretrain_data from datasets import get_pretrain_data
from models.nn import SymbolNetAutoencoder from models.nn import SymbolNetAutoencoder


+ 3
- 3
examples/hed/consistency_metric.py View File

@@ -1,8 +1,8 @@
from typing import Optional from typing import Optional


from abl.data.evaluation.base_metric import BaseMetric
from abl.data.structures import ListData
from abl.reasoning import KBBase
from ablkit.data.evaluation.base_metric import BaseMetric
from ablkit.data.structures import ListData
from ablkit.reasoning import KBBase




class ConsistencyMetric(BaseMetric): class ConsistencyMetric(BaseMetric):


+ 2
- 2
examples/hed/hed.ipynb View File

@@ -24,8 +24,8 @@
"import torch\n", "import torch\n",
"import torch.nn as nn\n", "import torch.nn as nn\n",
"\n", "\n",
"from abl.learning import ABLModel, BasicNN\n",
"from abl.utils import ABLLogger, print_log\n",
"from ablkit.learning import ABLModel, BasicNN\n",
"from ablkit.utils import ABLLogger, print_log\n",
"\n", "\n",
"from bridge import HedBridge\n", "from bridge import HedBridge\n",
"from consistency_metric import ConsistencyMetric\n", "from consistency_metric import ConsistencyMetric\n",


+ 2
- 2
examples/hed/main.py View File

@@ -4,8 +4,8 @@ import os.path as osp
import torch import torch
import torch.nn as nn import torch.nn as nn


from abl.learning import ABLModel, BasicNN
from abl.utils import ABLLogger, print_log
from ablkit.learning import ABLModel, BasicNN
from ablkit.utils import ABLLogger, print_log


from bridge import HedBridge from bridge import HedBridge
from consistency_metric import ConsistencyMetric from consistency_metric import ConsistencyMetric


+ 2
- 2
examples/hed/reasoning/reasoning.py View File

@@ -3,8 +3,8 @@ import os


import numpy as np import numpy as np


from abl.reasoning import PrologKB, Reasoner
from abl.utils import reform_list
from ablkit.reasoning import PrologKB, Reasoner
from ablkit.utils import reform_list


CURRENT_DIR = os.path.abspath(os.path.dirname(__file__)) CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))




+ 6
- 6
examples/hwf/hwf.ipynb View File

@@ -25,11 +25,11 @@
"import torch\n", "import torch\n",
"import torch.nn as nn\n", "import torch.nn as nn\n",
"\n", "\n",
"from abl.bridge import SimpleBridge\n",
"from abl.data.evaluation import ReasoningMetric, SymbolAccuracy\n",
"from abl.learning import ABLModel, BasicNN\n",
"from abl.reasoning import KBBase, Reasoner\n",
"from abl.utils import ABLLogger, print_log\n",
"from ablkit.bridge import SimpleBridge\n",
"from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy\n",
"from ablkit.learning import ABLModel, BasicNN\n",
"from ablkit.reasoning import KBBase, Reasoner\n",
"from ablkit.utils import ABLLogger, print_log\n",
"\n", "\n",
"from datasets import get_dataset\n", "from datasets import get_dataset\n",
"from models.nn import SymbolNet" "from models.nn import SymbolNet"
@@ -235,7 +235,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from abl.data.structures import ListData\n",
"from ablkit.data.structures import ListData\n",
"\n", "\n",
"# ListData is a data structure provided by ABL Kit that can be used to organize data examples\n", "# ListData is a data structure provided by ABL Kit that can be used to organize data examples\n",
"data_examples = ListData()\n", "data_examples = ListData()\n",


+ 5
- 5
examples/hwf/main.py View File

@@ -5,11 +5,11 @@ import numpy as np
import torch import torch
from torch import nn from torch import nn


from abl.bridge import SimpleBridge
from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from abl.learning import ABLModel, BasicNN
from abl.reasoning import GroundKB, KBBase, Reasoner
from abl.utils import ABLLogger, print_log
from ablkit.bridge import SimpleBridge
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.learning import ABLModel, BasicNN
from ablkit.reasoning import GroundKB, KBBase, Reasoner
from ablkit.utils import ABLLogger, print_log


from datasets import get_dataset from datasets import get_dataset
from models.nn import SymbolNet from models.nn import SymbolNet


+ 5
- 5
examples/mnist_add/main.py View File

@@ -5,11 +5,11 @@ import torch
from torch import nn from torch import nn
from torch.optim import RMSprop, lr_scheduler from torch.optim import RMSprop, lr_scheduler


from abl.bridge import SimpleBridge
from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from abl.learning import ABLModel, BasicNN
from abl.reasoning import GroundKB, KBBase, PrologKB, Reasoner
from abl.utils import ABLLogger, print_log
from ablkit.bridge import SimpleBridge
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.learning import ABLModel, BasicNN
from ablkit.reasoning import GroundKB, KBBase, PrologKB, Reasoner
from ablkit.utils import ABLLogger, print_log


from datasets import get_dataset from datasets import get_dataset
from models.nn import LeNet5 from models.nn import LeNet5


+ 6
- 6
examples/mnist_add/mnist_add.ipynb View File

@@ -25,11 +25,11 @@
"import torch.nn as nn\n", "import torch.nn as nn\n",
"from torch.optim import RMSprop, lr_scheduler\n", "from torch.optim import RMSprop, lr_scheduler\n",
"\n", "\n",
"from abl.bridge import SimpleBridge\n",
"from abl.data.evaluation import ReasoningMetric, SymbolAccuracy\n",
"from abl.learning import ABLModel, BasicNN\n",
"from abl.reasoning import KBBase, Reasoner\n",
"from abl.utils import ABLLogger, print_log\n",
"from ablkit.bridge import SimpleBridge\n",
"from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy\n",
"from ablkit.learning import ABLModel, BasicNN\n",
"from ablkit.reasoning import KBBase, Reasoner\n",
"from ablkit.utils import ABLLogger, print_log\n",
"\n", "\n",
"from datasets import get_dataset\n", "from datasets import get_dataset\n",
"from models.nn import LeNet5" "from models.nn import LeNet5"
@@ -280,7 +280,7 @@
} }
], ],
"source": [ "source": [
"from abl.data.structures import ListData\n",
"from ablkit.data.structures import ListData\n",
"\n", "\n",
"# ListData is a data structure provided by ABL Kit that can be used to organize data examples\n", "# ListData is a data structure provided by ABL Kit that can be used to organize data examples\n",
"data_examples = ListData()\n", "data_examples = ListData()\n",


+ 1
- 1
examples/zoo/kb.py View File

@@ -1,7 +1,7 @@
import openml import openml
from z3 import If, Implies, Int, Not, Solver, Sum, sat # noqa: F401 from z3 import If, Implies, Int, Not, Solver, Sum, sat # noqa: F401


from abl.reasoning import KBBase
from ablkit.reasoning import KBBase




class ZooKB(KBBase): class ZooKB(KBBase):


+ 5
- 5
examples/zoo/main.py View File

@@ -4,11 +4,11 @@ import os.path as osp
import numpy as np import numpy as np
from sklearn.ensemble import RandomForestClassifier from sklearn.ensemble import RandomForestClassifier


from abl.bridge import SimpleBridge
from abl.data.evaluation import ReasoningMetric, SymbolAccuracy
from abl.learning import ABLModel
from abl.reasoning import Reasoner
from abl.utils import ABLLogger, avg_confidence_dist, print_log, tab_data_to_tuple
from ablkit.bridge import SimpleBridge
from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy
from ablkit.learning import ABLModel
from ablkit.reasoning import Reasoner
from ablkit.utils import ABLLogger, avg_confidence_dist, print_log, tab_data_to_tuple


from get_dataset import load_and_preprocess_dataset, split_dataset from get_dataset import load_and_preprocess_dataset, split_dataset
from kb import ZooKB from kb import ZooKB


+ 5
- 5
examples/zoo/zoo.ipynb View File

@@ -23,11 +23,11 @@
"import numpy as np\n", "import numpy as np\n",
"from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.ensemble import RandomForestClassifier\n",
"\n", "\n",
"from abl.bridge import SimpleBridge\n",
"from abl.data.evaluation import ReasoningMetric, SymbolAccuracy\n",
"from abl.learning import ABLModel\n",
"from abl.reasoning import Reasoner\n",
"from abl.utils import ABLLogger, avg_confidence_dist, print_log, tab_data_to_tuple\n",
"from ablkit.bridge import SimpleBridge\n",
"from ablkit.data.evaluation import ReasoningMetric, SymbolAccuracy\n",
"from ablkit.learning import ABLModel\n",
"from ablkit.reasoning import Reasoner\n",
"from ablkit.utils import ABLLogger, avg_confidence_dist, print_log, tab_data_to_tuple\n",
"\n", "\n",
"from get_dataset import load_and_preprocess_dataset, split_dataset\n", "from get_dataset import load_and_preprocess_dataset, split_dataset\n",
"from kb import ZooKB" "from kb import ZooKB"


+ 3
- 3
tests/conftest.py View File

@@ -5,9 +5,9 @@ import torch
import torch.nn as nn import torch.nn as nn
import torch.optim as optim import torch.optim as optim


from abl.data.structures import ListData
from abl.learning import BasicNN
from abl.reasoning import GroundKB, KBBase, PrologKB, Reasoner
from ablkit.data.structures import ListData
from ablkit.learning import BasicNN
from ablkit.reasoning import GroundKB, KBBase, PrologKB, Reasoner




class LeNet5(nn.Module): class LeNet5(nn.Module):


+ 1
- 1
tests/test_abl_model.py View File

@@ -3,7 +3,7 @@ from unittest.mock import Mock, create_autospec
import numpy as np import numpy as np
import pytest import pytest


from abl.learning import ABLModel
from ablkit.learning import ABLModel




class TestABLModel(object): class TestABLModel(object):


+ 1
- 1
tests/test_reasoning.py View File

@@ -2,7 +2,7 @@ import numpy as np
import platform import platform
import pytest import pytest


from abl.reasoning import PrologKB, Reasoner
from ablkit.reasoning import PrologKB, Reasoner




class TestKBBase(object): class TestKBBase(object):


Loading…
Cancel
Save