From 6a6ebb14b43b1a73c295a05826f3b282ee21e4f3 Mon Sep 17 00:00:00 2001 From: troyyyyy Date: Mon, 25 Dec 2023 10:39:12 +0800 Subject: [PATCH] [FIX] resolve some comments --- abl/reasoning/kb.py | 74 ++++++++++++++++-------------- abl/reasoning/reasoner.py | 2 +- docs/Examples/HWF.rst | 16 +++---- docs/Examples/MNISTAdd.rst | 8 ++-- docs/Examples/Zoo.rst | 12 ++--- docs/Intro/Basics.rst | 2 +- docs/Intro/Datasets.rst | 2 +- docs/Intro/Reasoning.rst | 41 +++++++++-------- docs/Overview/Installation.rst | 4 ++ examples/hwf/hwf.ipynb | 18 ++++---- examples/hwf/main.py | 2 +- examples/mnist_add/mnist_add.ipynb | 36 +++++++-------- examples/zoo/zoo.ipynb | 4 +- 13 files changed, 117 insertions(+), 104 deletions(-) diff --git a/abl/reasoning/kb.py b/abl/reasoning/kb.py index 9786233..b5e5177 100644 --- a/abl/reasoning/kb.py +++ b/abl/reasoning/kb.py @@ -9,7 +9,6 @@ from multiprocessing import Pool from typing import Callable, Any, List, Optional import numpy as np -import pyswip from ..utils.logger import print_log from ..utils.cache import abl_cache @@ -27,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 @@ -82,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 @@ -114,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 @@ -130,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. """ @@ -162,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. """ @@ -204,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 = [], [] @@ -225,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. """ @@ -292,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``. @@ -365,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 @@ -379,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. """ @@ -466,9 +465,14 @@ class PrologKB(KBBase): def __init__(self, pseudo_label_list: List[Any], pl_file: str): 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) @@ -483,7 +487,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": @@ -525,7 +529,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] @@ -533,7 +537,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 ------- @@ -554,12 +558,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] @@ -567,15 +571,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. """ diff --git a/abl/reasoning/reasoner.py b/abl/reasoning/reasoner.py index 7209c9c..18f3f1e 100644 --- a/abl/reasoning/reasoner.py +++ b/abl/reasoning/reasoner.py @@ -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") diff --git a/docs/Examples/HWF.rst b/docs/Examples/HWF.rst index 595b505..ad25717 100644 --- a/docs/Examples/HWF.rst +++ b/docs/Examples/HWF.rst @@ -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 ` 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 diff --git a/docs/Examples/MNISTAdd.rst b/docs/Examples/MNISTAdd.rst index f8175ff..425a324 100644 --- a/docs/Examples/MNISTAdd.rst +++ b/docs/Examples/MNISTAdd.rst @@ -254,16 +254,16 @@ reasoning in the knowledge base ` 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:: diff --git a/docs/Examples/Zoo.rst b/docs/Examples/Zoo.rst index bd65d37..c5acead 100644 --- a/docs/Examples/Zoo.rst +++ b/docs/Examples/Zoo.rst @@ -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 diff --git a/docs/Intro/Basics.rst b/docs/Intro/Basics.rst index 5523cdd..37e8cae 100644 --- a/docs/Intro/Basics.rst +++ b/docs/Intro/Basics.rst @@ -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. diff --git a/docs/Intro/Datasets.rst b/docs/Intro/Datasets.rst index cb4f92a..da8e6c8 100644 --- a/docs/Intro/Datasets.rst +++ b/docs/Intro/Datasets.rst @@ -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:: diff --git a/docs/Intro/Reasoning.rst b/docs/Intro/Reasoning.rst index 9263327..e93bc41 100644 --- a/docs/Intro/Reasoning.rst +++ b/docs/Intro/Reasoning.rst @@ -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 `. - ``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 `. - ``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 `. 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? `, 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 diff --git a/docs/Overview/Installation.rst b/docs/Overview/Installation.rst index ae6ed1c..229a274 100644 --- a/docs/Overview/Installation.rst +++ b/docs/Overview/Installation.rst @@ -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 `_ is necessary, you will also need to install Swi-Prolog. + +`http://www.swi-prolog.org/build/unix.html `_ \ No newline at end of file diff --git a/examples/hwf/hwf.ipynb b/examples/hwf/hwf.ipynb index 56ef943..106daab 100644 --- a/examples/hwf/hwf.ipynb +++ b/examples/hwf/hwf.ipynb @@ -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." ] }, { diff --git a/examples/hwf/main.py b/examples/hwf/main.py index 360cec7..b20c290 100644 --- a/examples/hwf/main.py +++ b/examples/hwf/main.py @@ -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() diff --git a/examples/mnist_add/mnist_add.ipynb b/examples/mnist_add/mnist_add.ipynb index 6161131..eb2bedf 100644 --- a/examples/mnist_add/mnist_add.ipynb +++ b/examples/mnist_add/mnist_add.ipynb @@ -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": {}, "outputs": [ { @@ -125,7 +125,7 @@ }, { "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAD1CAYAAADNj/Z6AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAKUklEQVR4nO3df6zVdR3H8XPuDy4iCHcRUnKRQIFkOFHzV+Z0maVhM5dZyzY1JWCUbmk/tkwrXWXRQB0xayVUarOcthhmFJpL5GflYgTiryyV3wgIXrjnnv6x/kh4fy+de++58H48/n2d+z3ff+7hyXe7n1OuVqvVEgCQVkO9bwAAqC8xAADJiQEASE4MAEByYgAAkhMDAJCcGACA5MQAACQnBgAguaauvvADDZf15H0AXfC7zgfqfQsHzWcH1F/RZ4cnAwCQnBgAgOTEAAAkJwYAIDk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dPygvcH+qLK9tfCvXHxqnAfs7g77wbeypMBAEhODABAcmIAAJITAwCQnBgAgOTEAAAkJwYAIDnnDLxp31Hx94WXSqXS0Mb4HIEiN/1rcri3LHSOAAC9z5MBAEhODABAcmIAAJITAwCQnBgAgOTEAAAkJwYAIDnnDHSjL7xyRrhv/WjxWQYA0Ns8GQCA5MQAACQnBgAgOTEAAMmJAQBITgwAQHJiAACSc87Am5oXrSx8zeRjTil4xb6CfWOX7wcAeosnAwCQnBgAgOTEAAAkJwYAIDkxAADJiQEASE4MAEByYgAAkhMDAJCcGACA5MQAACQnBgAgOTEAAMmJAQBITgwAQHLlarVarfdNAAD148kAACQnBgAgOTEAAMmJAQBITgwAQHJiAACSEwMAkJwYAIDkxAAAJPdvx8SA9zSwVzUAAAAASUVORK5CYII=", "text/plain": [ "
" ] @@ -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": [ diff --git a/examples/zoo/zoo.ipynb b/examples/zoo/zoo.ipynb index 24f33de..6af591d 100644 --- a/examples/zoo/zoo.ipynb +++ b/examples/zoo/zoo.ipynb @@ -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",