Browse Source

Merge branch 'Dev' of https://github.com/AbductiveLearning/ABL-Package into Dev

pull/1/head
Tony-HYX 2 years ago
parent
commit
e7aaf10cc5
13 changed files with 117 additions and 103 deletions
  1. +39
    -34
      abl/reasoning/kb.py
  2. +1
    -1
      abl/reasoning/reasoner.py
  3. +8
    -8
      docs/Examples/HWF.rst
  4. +4
    -4
      docs/Examples/MNISTAdd.rst
  5. +6
    -6
      docs/Examples/Zoo.rst
  6. +1
    -1
      docs/Intro/Basics.rst
  7. +1
    -1
      docs/Intro/Datasets.rst
  8. +23
    -18
      docs/Intro/Reasoning.rst
  9. +4
    -0
      docs/Overview/Installation.rst
  10. +9
    -9
      examples/hwf/hwf.ipynb
  11. +1
    -1
      examples/hwf/main.py
  12. +18
    -18
      examples/mnist_add/mnist_add.ipynb
  13. +2
    -2
      examples/zoo/zoo.ipynb

+ 39
- 34
abl/reasoning/kb.py View File

@@ -26,8 +26,8 @@ class KBBase(ABC):
list so that each aligns with its corresponding index in the base model: the first with
the 0th index, the second with the 1st, and so forth.
max_err : float, optional
The upper tolerance limit when comparing the similarity between a pseudo-label example's
reasoning result and the ground truth. This is only applicable when the reasoning
The upper tolerance limit when comparing the similarity between the reasoning result of
pseudo-labels and the ground truth. This is only applicable when the reasoning
result is of a numerical type. This is particularly relevant for regression problems where
exact matches might not be feasible. Defaults to 1e-10.
use_cache : bool, optional
@@ -81,16 +81,16 @@ class KBBase(ABC):
@abstractmethod
def logic_forward(self, pseudo_label: List[Any], x: Optional[List[Any]] = None) -> Any:
"""
How to perform (deductive) logical reasoning, i.e. matching each pseudo-label example to
How to perform (deductive) logical reasoning, i.e. matching pseudo-labels to
their reasoning result. Users are required to provide this.

Parameters
----------
pseudo_label : List[Any]
Pseudo-label example.
Pseudo-labels of an example.
x : Optional[List[Any]]
The corresponding input example. If deductive logical reasoning does not require any
information from the input, the overridden function provided by the user can omit
The example. If deductive logical reasoning does not require any
information from the example, the overridden function provided by the user can omit
this parameter.
Returns
@@ -113,11 +113,11 @@ class KBBase(ABC):
Parameters
----------
pseudo_label : List[Any]
Pseudo-label example (to be revised by abductive reasoning).
Pseudo-labels of an example (to be revised by abductive reasoning).
y : Any
Ground truth of the reasoning result for the example.
x : List[Any]
The corresponding input example. If the information from the input
The example. If the information from the example
is not required in the reasoning process, then this parameter will not have
any effect.
max_revision_num : int
@@ -129,7 +129,7 @@ class KBBase(ABC):
-------
Tuple[List[List[Any]], List[Any]]
A tuple of two element. The first element is a list of candidate revisions, i.e. revised
pseudo-label examples that are compatible with the knowledge base. The second element is
pseudo-labels of the example. that are compatible with the knowledge base. The second element is
a list of reasoning results corresponding to each candidate, i.e., the outcome of the
logic_forward function.
"""
@@ -161,26 +161,26 @@ class KBBase(ABC):
revision_idx: List[int],
) -> List[List[Any]]:
"""
Revise the pseudo-label example at specified index positions.
Revise the pseudo-labels at specified index positions.

Parameters
----------
pseudo_label : List[Any]
Pseudo-label example (to be revised).
Pseudo-labels of an example (to be revised).
y : Any
Ground truth of the reasoning result for the example.
x : List[Any]
The corresponding input example. If the information from the input
The example. If the information from the example
is not required in the reasoning process, then this parameter will not have
any effect.
revision_idx : List[int]
A list specifying indices of where revisions should be made to the pseudo-label example.
A list specifying indices of where revisions should be made to the pseudo-labels.

Returns
-------
Tuple[List[List[Any]], List[Any]]
A tuple of two element. The first element is a list of candidate revisions, i.e. revised
pseudo-label examples that are compatible with the knowledge base. The second element is
pseudo-labels of the example that are compatible with the knowledge base. The second element is
a list of reasoning results corresponding to each candidate, i.e., the outcome of the
logic_forward function.
"""
@@ -203,7 +203,7 @@ class KBBase(ABC):
x: List[Any],
) -> List[List[Any]]:
"""
For a specified number of labels in a pseudo-label example to revise, iterate through
For a specified number of labels in a pseudo-labels to revise, iterate through
all possible indices to find any candidates that are compatible with the knowledge base.
"""
new_candidates, new_reasoning_results = [], []
@@ -224,31 +224,31 @@ class KBBase(ABC):
) -> List[List[Any]]:
"""
Perform abductive reasoning by exhastive search. Specifically, begin with 0 and
continuously increase the number of labels in a pseudo-label example to revise, until
continuously increase the number of labels to revise, until
candidates that are compatible with the knowledge base are found.

Parameters
----------
pseudo_label : List[Any]
Pseudo-label example (to be revised).
Pseudo-labels of an example (to be revised).
y : Any
Ground truth of the reasoning result for the example.
x : List[Any]
The corresponding input example. If the information from the input
The example. If the information from the example
is not required in the reasoning process, then this parameter will not have
any effect.
max_revision_num : int
The upper limit on the number of revisions.
require_more_revision : int
If larger than 0, then after having found any candidates compatible with the
knowledge base, continue to increase the number of labels in a pseudo-label example to
knowledge base, continue to increase the number of labels to
revise to get more possible compatible candidates.

Returns
-------
Tuple[List[List[Any]], List[Any]]
A tuple of two element. The first element is a list of candidate revisions, i.e. revised
pseudo-label examples that are compatible with the knowledge base. The second element is
pseudo-labels of the example that are compatible with the knowledge base. The second element is
a list of reasoning results corresponding to each candidate, i.e., the outcome of the
logic_forward function.
"""
@@ -291,7 +291,7 @@ class GroundKB(KBBase):
pseudo_label_list : list
Refer to class ``KBBase``.
GKB_len_list : list
List of possible lengths for a pseudo-label example.
List of possible lengths for pseudo-labels of an example.
max_err : float, optional
Refer to class ``KBBase``.

@@ -364,11 +364,11 @@ class GroundKB(KBBase):
Parameters
----------
pseudo_label : List[Any]
Pseudo-label example (to be revised by abductive reasoning).
Pseudo-labels of an example (to be revised by abductive reasoning).
y : Any
Ground truth of the reasoning result for the example.
x : List[Any]
The corresponding input example (unused in GroundKB).
The example (unused in GroundKB).
max_revision_num : int
The upper limit on the number of revised labels for each example.
require_more_revision : int
@@ -378,7 +378,7 @@ class GroundKB(KBBase):
-------
Tuple[List[List[Any]], List[Any]]
A tuple of two element. The first element is a list of candidate revisions, i.e. revised
pseudo-label examples that are compatible with the knowledge base. The second element is
pseudo-labels of THE example that are compatible with the knowledge base. The second element is
a list of reasoning results corresponding to each candidate, i.e., the outcome of the
logic_forward function.
"""
@@ -467,9 +467,14 @@ class PrologKB(KBBase):
import pyswip
super().__init__(pseudo_label_list)
self.pl_file = pl_file
try:
import pyswip
except IndexError:
print("A Prolog-based knowledge base is used. You need to install Swi-Prolog from https://www.swi-prolog.org/Download.html")
self.prolog = pyswip.Prolog()

self.pl_file = pl_file
if not os.path.exists(self.pl_file):
raise FileNotFoundError(f"The Prolog file {self.pl_file} does not exist.")
self.prolog.consult(self.pl_file)
@@ -484,7 +489,7 @@ class PrologKB(KBBase):
Parameters
----------
pseudo_label : List[Any]
Pseudo-label example.
Pseudo-labels of an example.
"""
result = list(self.prolog.query("logic_forward(%s, Res)." % pseudo_label))[0]["Res"]
if result == "true":
@@ -526,7 +531,7 @@ class PrologKB(KBBase):
Parameters
----------
pseudo_label : List[Any]
Pseudo-label example (to be revised by abductive reasoning).
Pseudo-labels of an example (to be revised by abductive reasoning).
y : Any
Ground truth of the reasoning result for the example.
x : List[Any]
@@ -534,7 +539,7 @@ class PrologKB(KBBase):
is not required in the reasoning process, then this parameter will not have
any effect.
revision_idx : List[int]
A list specifying indices of where revisions should be made to the pseudo-label example.
A list specifying indices of where revisions should be made to the pseudo-labels.

Returns
-------
@@ -555,12 +560,12 @@ class PrologKB(KBBase):
revision_idx: List[int],
) -> List[List[Any]]:
"""
Revise the pseudo-label example at specified index positions by querying Prolog.
Revise the pseudo-labels at specified index positions by querying Prolog.

Parameters
----------
pseudo_label : List[Any]
Pseudo-label example (to be revised).
Pseudo-labels of an example (to be revised).
y : Any
Ground truth of the reasoning result for the example.
x : List[Any]
@@ -568,15 +573,15 @@ class PrologKB(KBBase):
is not required in the reasoning process, then this parameter will not have
any effect.
revision_idx : List[int]
A list specifying indices of where revisions should be made to the pseudo-label example.
A list specifying indices of where revisions should be made to the pseudo-labels.

Returns
-------
Tuple[List[List[Any]], List[Any]]
A list of candidates, i.e. revised pseudo-label examples that are compatible with the
A list of candidates, i.e. revised pseudo-labels of the example that are compatible with the
knowledge base.
A tuple of two element. The first element is a list of candidate revisions, i.e. revised
pseudo-label examples that are compatible with the knowledge base. The second element is
pseudo-labels of the example that are compatible with the knowledge base. The second element is
a list of reasoning results corresponding to each candidate, i.e., the outcome of the
logic_forward function.
"""


+ 1
- 1
abl/reasoning/reasoner.py View File

@@ -283,7 +283,7 @@ class Reasoner:
Returns
-------
List[Any]
A revised pseudo-label example through abductive reasoning, which is compatible
A revised pseudo-labels of the example through abductive reasoning, which is compatible
with the knowledge base.
"""
symbol_num = data_example.elements_num("pred_pseudo_label")


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

@@ -169,8 +169,8 @@ sklearn-style interface.

.. code:: ipython3

# class of symbol may be one of ['0', '1', ..., '9', '+', '-', '*', '/'], total of 14 classes
cls = SymbolNet(num_classes=14, image_size=(45, 45, 1))
# class of symbol may be one of ['1', ..., '9', '+', '-', '*', '/'], total of 14 classes
cls = SymbolNet(num_classes=13, image_size=(45, 45, 1))
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cls.parameters(), lr=0.001, betas=(0.9, 0.99))
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@@ -297,16 +297,16 @@ reasoning in the knowledge base <kb-abd>` for details of abductive reasoning.

.. code:: ipython3

pseudo_label_example = ["1", "-", "2", "*", "5"]
reasoning_result = kb.logic_forward(pseudo_label_example)
print(f"Reasoning result of pseudo-label example {pseudo_label_example} is {reasoning_result}.")
pseudo_labels = ["1", "-", "2", "*", "5"]
reasoning_result = kb.logic_forward(pseudo_labels)
print(f"Reasoning result of pseudo-labels {pseudo_labels} is {reasoning_result}.")


Out:
.. code:: none
:class: code-out

Reasoning result of pseudo-label example ['1', '-', '2', '*', '5'] is -9.
Reasoning result of pseudo-labels ['1', '-', '2', '*', '5'] is -9.

.. note::
@@ -319,8 +319,8 @@ Out:
Also, when building the knowledge base, we can also set the
``max_err`` parameter during initialization, which is shown in the
``examples/hwf/main.py`` file. This parameter specifies the upper tolerance limit
when comparing the similarity between a pseudo-label example’s reasoning
result and the ground truth during abductive reasoning, with a default
when comparing the similarity between the reasoning result of pseudo-labels and
the ground truth during abductive reasoning, with a default
value of 1e-10.

Then, we create a reasoner by instantiating the class ``Reasoner``. Due


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

@@ -254,16 +254,16 @@ reasoning in the knowledge base <kb-abd>` for details of abductive reasoning.

.. code:: ipython3

pseudo_label_example = [1, 2]
reasoning_result = kb.logic_forward(pseudo_label_example)
print(f"Reasoning result of pseudo-label example {pseudo_label_example} is {reasoning_result}.")
pseudo_labels = [1, 2]
reasoning_result = kb.logic_forward(pseudo_labels)
print(f"Reasoning result of pseudo-labels {pseudo_labels} is {reasoning_result}.")


Out:
.. code:: none
:class: code-out

Reasoning result of pseudo-label example [1, 2] is 3.
Reasoning result of pseudo-labels [1, 2] is 3.

.. note::


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

@@ -227,22 +227,22 @@ Out:
.. code:: none
:class: code-out

abl - INFO - Abductive Learning on the Zoo example.
abl - INFO - Abductive Learning on the ZOO example.
abl - INFO - ------- Use labeled data to pretrain the model -----------
abl - INFO - ------- Test the initial model -----------
abl - INFO - Evaluation ended, zoo/character_accuracy: 0.935 zoo/reasoning_accuracy: 0.935
abl - INFO - Evaluation ended, zoo/character_accuracy: 0.903 zoo/reasoning_accuracy: 0.903
abl - INFO - ------- Use ABL to train the model -----------
abl - INFO - loop(train) [1/3] segment(train) [1/1]
abl - INFO - Evaluation start: loop(val) [1]
abl - INFO - Evaluation ended, zoo/character_accuracy: 0.984 zoo/reasoning_accuracy: 1.000
abl - INFO - Evaluation ended, zoo/character_accuracy: 1.000 zoo/reasoning_accuracy: 1.000
abl - INFO - loop(train) [2/3] segment(train) [1/1]
abl - INFO - Evaluation start: loop(val) [2]
abl - INFO - Evaluation ended, zoo/character_accuracy: 0.984 zoo/reasoning_accuracy: 1.000
abl - INFO - Evaluation ended, zoo/character_accuracy: 1.000 zoo/reasoning_accuracy: 1.000
abl - INFO - loop(train) [3/3] segment(train) [1/1]
abl - INFO - Evaluation start: loop(val) [3]
abl - INFO - Evaluation ended, zoo/character_accuracy: 0.984 zoo/reasoning_accuracy: 1.000
abl - INFO - Evaluation ended, zoo/character_accuracy: 1.000 zoo/reasoning_accuracy: 1.000
abl - INFO - ------- Test the final model -----------
abl - INFO - Evaluation ended, zoo/character_accuracy: 0.903 zoo/reasoning_accuracy: 0.935
abl - INFO - Evaluation ended, zoo/character_accuracy: 0.968 zoo/reasoning_accuracy: 0.968

We may see from the results, after undergoing training with ABL, the


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

@@ -80,7 +80,7 @@ To implement this process, the following five steps are necessary:
3. :green:`Build the` :green-bold:`reasoning` :green:`part`

Define a knowledge base by building a subclass of ``KBBase``, specifying how to
process pseudo-label examples to reasoning results.
process pseudo-labels to reasoning results.
Also, create a ``Reasoner`` for minimizing inconsistencies
between the knowledge base and data.



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

@@ -28,7 +28,7 @@ ABL-Package requires user data to be either structured as a tuple ``(X, gt_pseud

- ``gt_pseudo_label``: List[List[Any]], optional
A list of sublists with each sublist representing a ground-truth pseudo-label example. Each example consists of ground-truth pseudo-labels for each **instance** within a example of ``X``.
A list of sublists with each sublist representing ground-truth pseudo-labels of an example. Each pseudo-label in the sublist serves as ground-truth for each **instance** within the example.
.. note::



+ 23
- 18
docs/Intro/Reasoning.rst View File

@@ -42,15 +42,15 @@ and override the ``logic_forward`` function:
- ``pseudo_label_list`` is the list of possible pseudo-labels (also,
the output of the machine learning model).
- ``logic_forward`` defines how to perform (deductive) reasoning,
i.e. matching each pseudo-label example to its reasoning result.
i.e. matching each pseudo-labels to its reasoning result.

.. note::

Generally, the overridden function ``logic_forward`` provided by the user accepts
only one parameter, ``pseudo_label`` (a pseudo-label example). However, for certain
only one parameter, ``pseudo_label`` (pseudo-labels of an example). However, for certain
scenarios, deductive reasoning in the knowledge base may necessitate information
from the input. In these scenarios, ``logic_forward`` can also accept two parameters:
``pseudo_label`` and ``x``.
``pseudo_label`` and ``x``. See examples in `Zoo <../Examples/Zoo.html>`_.

After that, other operations, including how to perform abductive
reasoning, will be **automatically** set up.
@@ -60,7 +60,7 @@ MNIST Addition example

As an example, the ``pseudo_label_list`` passed in MNIST Addition is all the
possible digits, namely, ``[0,1,2,...,9]``, and the ``logic_forward``
should be: “Add the two labels in the pseudo-label example to get the result.”. Therefore, the
should be: “Add the two pseudo-labels to get the result.”. Therefore, the
construction of the KB (``add_kb``) for MNIST Addition would be:

.. code:: python
@@ -78,15 +78,15 @@ and (deductive) reasoning in ``add_kb`` would be:

.. code:: python

pseudo_label_example = [1, 2]
reasoning_result = add_kb.logic_forward(pseudo_label_example)
print(f"Reasoning result of pseudo-label example {pseudo_label_example} is {reasoning_result}.")
pseudo_labels = [1, 2]
reasoning_result = add_kb.logic_forward(pseudo_labels)
print(f"Reasoning result of pseudo-labels {pseudo_labels} is {reasoning_result}.")

Out:
.. code:: none
:class: code-out

Reasoning result of pseudo-label example [1, 2] is 3
Reasoning result of pseudo-labels [1, 2] is 3

.. _other-par:

@@ -97,19 +97,21 @@ We can also pass the following parameters in the ``__init__`` function when buil
knowledge base:

- ``max_err`` (float, optional), specifying the upper tolerance limit
when comparing the similarity between a pseudo-label example's reasoning result
when comparing the similarity between the reasoning result of pseudo-labels
and the ground truth during abductive reasoning. This is only
applicable when the reasoning result is of a numerical type. This is
particularly relevant for regression problems where exact matches
might not be feasible. Defaults to 1e-10. See :ref:`an example <kb-abd-2>`.
- ``use_cache`` (bool, optional), indicating whether to use cache to store
previous candidates (pseudo-label examples generated from abductive reasoning)
previous candidates (pseudo-labels generated from abductive reasoning)
to speed up subsequent abductive reasoning operations. Defaults to True.
For more information of abductive reasoning, please refer to :ref:`this <kb-abd>`.
- ``cache_size`` (int, optional), specifying the maximum cache
size. This is only operational when ``use_cache`` is set to True.
Defaults to 4096.

.. _prolog:

Building a knowledge base from Prolog file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@@ -179,7 +181,7 @@ override the ``logic_forward`` function, and are allowed to pass other
:ref:`optional parameters <other-par>`. Additionally, we are required pass the
``GKB_len_list`` parameter in the ``__init__`` function.

- ``GKB_len_list`` is the list of possible lengths for a pseudo-label example.
- ``GKB_len_list`` is the list of possible lengths for pseudo-labels of an example.

After that, other operations, including auto-construction of GKB, and
how to perform abductive reasoning, will be **automatically** set up.
@@ -212,18 +214,18 @@ Performing abductive reasoning in the knowledge base
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As mentioned in :ref:`What is Abductive Reasoning? <abd>`, abductive reasoning
enables the inference of candidates (which are pseudo-label examples) as potential
enables the inference of candidates (i.e., possible pseudo-labels) as potential
explanations for the reasoning result. Also, in Abductive Learning where
an observation (a pseudo-label example predicted by the learning part) is
an observation (pseudo-labels of an example predicted by the learning part) is
available, we aim to let the candidate do not largely revise the
previously identified pseudo-label example.
previously identified pseudo-labels.

``KBBase`` (also, ``GroundKB`` and ``PrologKB``) implement the method
``abduce_candidates(pseudo_label, y, x, max_revision_num, require_more_revision)``
for performing abductive reasoning, where the parameters are:

- ``pseudo_label``, the pseudo-label example to be revised by abductive
reasoning, usually generated by the learning part.
- ``pseudo_label``, pseudo-labels of an example, usually generated by the learning
part. They are to be revised by abductive reasoning.
- ``y``, the ground truth of the reasoning result for the example. The
returned candidates should be compatible with it.
- ``x``, the corresponding input example. If the information from the input
@@ -237,8 +239,8 @@ for performing abductive reasoning, where the parameters are:
method will only output candidates with the minimum possible
revisions.)

And it return a list of candidates (i.e., revised pseudo-label examples) that
are all compatible with ``y``.
And it return a list of candidates (i.e., revised pseudo-labels of the example)
that are all compatible with ``y``.

MNIST Addition example (cont.)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -320,6 +322,9 @@ specify:
derived from the predicted probability in the data example. For
“hamming”, it directly calculates the Hamming distance between the
predicted pseudo-label in the data example and candidate.
- ``idx_to_label`` (dict, optional), a mapping from index in the base model to label.
If not provided, a default order-based index to label mapping is created.
Defaults to None.

The main method implemented by ``Reasoner`` is
``abduce(data_example)``, which obtains the most consistent candidate


+ 4
- 0
docs/Overview/Installation.rst View File

@@ -22,3 +22,7 @@ sequentially run following commands in your terminal/command line.
$ git clone https://github.com/AbductiveLearning/ABL-Package.git
$ cd ABL-Package
$ pip install -v -e .

(Optional) If the use of a `Prolog-based knowledge base <prolog>`_ is necessary, you will also need to install Swi-Prolog.

`http://www.swi-prolog.org/build/unix.html <http://www.swi-prolog.org/build/unix.html>`_

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

@@ -211,8 +211,8 @@
"metadata": {},
"outputs": [],
"source": [
"# class of symbol may be one of ['0', '1', ..., '9', '+', '-', '*', '/'], total of 14 classes\n",
"cls = SymbolNet(num_classes=14, image_size=(45, 45, 1))\n",
"# class of symbol may be one of ['1', ..., '9', '+', '-', '*', '/'], total of 13 classes\n",
"cls = SymbolNet(num_classes=13, image_size=(45, 45, 1))\n",
"loss_fn = nn.CrossEntropyLoss()\n",
"optimizer = torch.optim.Adam(cls.parameters(), lr=0.001)\n",
"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
@@ -244,7 +244,7 @@
"output_type": "stream",
"text": [
"Predicted class index for a batch of 32 instances: ndarray with shape (32,)\n",
"Predicted class probabilities for a batch of 32 instances: ndarray with shape (32, 14)\n"
"Predicted class probabilities for a batch of 32 instances: ndarray with shape (32, 13)\n"
]
}
],
@@ -294,7 +294,7 @@
"the first element is a ndarray of shape (3,), and the second element is a ndarray of shape (5,).\n",
"\n",
"Predicted class probabilities for the 100 data examples: a list of length 2, \n",
"the first element is a ndarray of shape (3, 14), and the second element is a ndarray of shape (5, 14).\n"
"the first element is a ndarray of shape (3, 13), and the second element is a ndarray of shape (5, 13).\n"
]
}
],
@@ -378,14 +378,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Reasoning result of pseudo-label example ['1', '-', '2', '*', '5'] is -9.\n"
"Reasoning result of pseudo-labels ['1', '-', '2', '*', '5'] is -9.\n"
]
}
],
"source": [
"pseudo_label_example = [\"1\", \"-\", \"2\", \"*\", \"5\"]\n",
"reasoning_result = kb.logic_forward(pseudo_label_example)\n",
"print(f\"Reasoning result of pseudo-label example {pseudo_label_example} is {reasoning_result}.\")"
"pseudo_labels = [\"1\", \"-\", \"2\", \"*\", \"5\"]\n",
"reasoning_result = kb.logic_forward(pseudo_labels)\n",
"print(f\"Reasoning result of pseudo-labels {pseudo_labels} is {reasoning_result}.\")"
]
},
{
@@ -394,7 +394,7 @@
"source": [
"Note: In addition to building a knowledge base based on `KBBase`, we can also establish a knowledge base with a ground KB using `GroundKB`. The corresponding code can be found in the `main.py` file. Those interested are encouraged to examine it for further insights.\n",
"\n",
"Note: Also, when building the knowledge base, we can also set the `max_err` parameter during initialization, which is shown in the `main.py` file. This parameter specifies the upper tolerance limit when comparing the similarity between a pseudo-label example’s reasoning result and the ground truth during abductive reasoning, with a default value of 1e-10."
"Note: Also, when building the knowledge base, we can also set the `max_err` parameter during initialization, which is shown in the `main.py` file. This parameter specifies the upper tolerance limit when comparing the similarity between the reasoning result of pseudo-labels and the ground truth during abductive reasoning, with a default value of 1e-10."
]
},
{


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

@@ -120,7 +120,7 @@ def main():

### Building the Learning Part
# Build necessary components for BasicNN
cls = SymbolNet(num_classes=14, image_size=(45, 45, 1))
cls = SymbolNet(num_classes=13, image_size=(45, 45, 1))
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cls.parameters(), lr=args.lr)
use_cuda = not args.no_cuda and torch.cuda.is_available()


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

@@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -113,7 +113,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
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wCQnBgAgOTEAAAkJwYAIDkxAADJiQEASE4MAEByzhnoRo0njA33R2/8bsEV4u8c31eNzxGYfvN14T5k55KC9wf+V2XNM+E+efGMwmusv+CH4d5Yru3/ZT19jsD9O1vDff4l54f78DXOEejrPBkAgOTEAAAkJwYAIDkxAADJiQEASE4MAEByYgAAknPOwEFoGDAg3J+9OT4noLUh3ouc+tTV4d423zkC0NvGXrWy8DWTHvpUuD992n3ddTv7dcUL54b7qgUnhPuoBzaEe2VdfBYDfZ8nAwCQnBgAgOTEAAAkJwYAIDkxAADJiQEASE4MAEByYgAAknPo0EF45ZqTwv1vZ99Z0/X/9EZzuI+aVnDwR03vDvSUEVe+HO7nT4oPFHvhw/3CvW1R/Nvf77cr4p+vPhnuPlsOf54MAEByYgAAkhMDAJCcGACA5MQAACQnBgAgOTEAAMk5Z+A/TptY+JKFN9xe8IojwvWfHXvC/ZYZU8O9ZdPygvcH+qLK9tfCvXHxqnAfs7g77wbeypMBAEhODABAcmIAAJITAwCQnBgAgOTEAAAkJwYAIDnnDLxp31Hx94WXSqXS0Mb4HIEiN/1rcri3LHSOAAC9z5MBAEhODABAcmIAAJITAwCQnBgAgOTEAAAkJwYAIDnnDHSjL7xyRrhv/WjxWQYA0Ns8GQCA5MQAACQnBgAgOTEAAMmJAQBITgwAQHJiAACSc87Am5oXrSx8zeRjTil4xb6CfWOX7wcAeosnAwCQnBgAgOTEAAAkJwYAIDkxAADJiQEASE4MAEByYgAAkhMDAJCcGACA5MQAACQnBgAgOTEAAMmJAQBITgwAQHLlarVarfdNAAD148kAACQnBgAgOTEAAMmJAQBITgwAQHJiAACSEwMAkJwYAIDkxAAAJPdvx8SA9zSwVzUAAAAASUVORK5CYII=",
"text/plain": [
"<Figure size 640x480 with 2 Axes>"
]
@@ -173,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@@ -203,7 +203,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -234,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -250,7 +250,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -300,7 +300,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -324,21 +324,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reasoning result of pseudo-label example [1, 2] is 3.\n"
"Reasoning result of pseudo-labels [1, 2] is 3.\n"
]
}
],
"source": [
"pseudo_label_example = [1, 2]\n",
"reasoning_result = kb.logic_forward(pseudo_label_example)\n",
"print(f\"Reasoning result of pseudo-label example {pseudo_label_example} is {reasoning_result}.\")"
"pseudo_labels = [1, 2]\n",
"reasoning_result = kb.logic_forward(pseudo_labels)\n",
"print(f\"Reasoning result of pseudo-labels {pseudo_labels} is {reasoning_result}.\")"
]
},
{
@@ -357,7 +357,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@@ -390,7 +390,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@@ -409,7 +409,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [


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

@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# ZOO\n",
"# Zoo\n",
"\n",
"This notebook shows an implementation of [Zoo](https://archive.ics.uci.edu/dataset/111/zoo). In this task, attributes of animals (such as presence of hair, eggs, etc.) and their targets (the animal class they belong to) are given, along with a knowledge base which contain information about the relations between attributes and targets, e.g., Implies(milk == 1, mammal == 1). \n",
"\n",
@@ -328,7 +328,7 @@
],
"source": [
"# Build logger\n",
"print_log(\"Abductive Learning on the ZOO example.\", logger=\"current\")\n",
"print_log(\"Abductive Learning on the Zoo example.\", logger=\"current\")\n",
"log_dir = ABLLogger.get_current_instance().log_dir\n",
"weights_dir = osp.join(log_dir, \"weights\")\n",
"\n",


Loading…
Cancel
Save