diff --git a/abl/data/structures/list_data.py b/abl/data/structures/list_data.py index 133b277..d9f2cb5 100644 --- a/abl/data/structures/list_data.py +++ b/abl/data/structures/list_data.py @@ -16,6 +16,7 @@ LongTypeTensor = Union[torch.LongTensor, torch.cuda.LongTensor] IndexType = Union[str, slice, int, list, LongTypeTensor, BoolTypeTensor, np.ndarray] + class ListData(BaseDataElement): """ Abstract Data Interface used throughout the ABL-Package. diff --git a/abl/learning/basic_nn.py b/abl/learning/basic_nn.py index 18f38ec..3693bb7 100644 --- a/abl/learning/basic_nn.py +++ b/abl/learning/basic_nn.py @@ -309,7 +309,7 @@ class BasicNN: batch_size=self.batch_size, num_workers=self.num_workers, collate_fn=self.collate_fn, - pin_memory=torch.cuda.is_available() + pin_memory=torch.cuda.is_available(), ) return self._predict(data_loader).argmax(axis=1).cpu().numpy() @@ -351,7 +351,7 @@ class BasicNN: batch_size=self.batch_size, num_workers=self.num_workers, collate_fn=self.collate_fn, - pin_memory=torch.cuda.is_available() + pin_memory=torch.cuda.is_available(), ) return self._predict(data_loader).softmax(axis=1).cpu().numpy() @@ -484,7 +484,7 @@ class BasicNN: shuffle=shuffle, num_workers=self.num_workers, collate_fn=self.collate_fn, - pin_memory=torch.cuda.is_available() + pin_memory=torch.cuda.is_available(), ) return data_loader diff --git a/abl/reasoning/kb.py b/abl/reasoning/kb.py index 90e0dc0..c3665a8 100644 --- a/abl/reasoning/kb.py +++ b/abl/reasoning/kb.py @@ -128,10 +128,10 @@ class KBBase(ABC): Returns ------- Tuple[List[List[Any]], List[Any]] - A tuple of two elements. The first element is a list of candidate revisions, i.e. revised - 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. + A tuple of two elements. The first element is a list of candidate revisions, + i.e. revised 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. """ return self._abduce_by_search(pseudo_label, y, x, max_revision_num, require_more_revision) @@ -179,10 +179,10 @@ class KBBase(ABC): Returns ------- Tuple[List[List[Any]], List[Any]] - A tuple of two elements. The first element is a list of candidate revisions, i.e. revised - 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. + A tuple of two elements. The first element is a list of candidate revisions, + i.e. revised 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. """ candidates, reasoning_results = [], [] abduce_c = product(self.pseudo_label_list, repeat=len(revision_idx)) @@ -249,10 +249,10 @@ class KBBase(ABC): Returns ------- Tuple[List[List[Any]], List[Any]] - A tuple of two elements. The first element is a list of candidate revisions, i.e. revised - 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. + A tuple of two elements. The first element is a list of candidate revisions, + i.e. revised 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. """ candidates, reasoning_results = [], [] for revision_num in range(len(pseudo_label) + 1): @@ -389,10 +389,10 @@ class GroundKB(KBBase): Returns ------- Tuple[List[List[Any]], List[Any]] - A tuple of two elements. The first element is a list of candidate revisions, i.e. revised - 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. + A tuple of two elements. The first element is a list of candidate revisions, + i.e. revised 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. """ if self.GKB == {} or len(pseudo_label) not in self.GKB_len_list: return [], [] @@ -592,10 +592,10 @@ class PrologKB(KBBase): Returns ------- Tuple[List[List[Any]], List[Any]] - A tuple of two elements. The first element is a list of candidate revisions, i.e. revised - 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. + A tuple of two elements. The first element is a list of candidate revisions, + i.e. revised 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. """ candidates, reasoning_results = [], [] query_string = self.get_query_string(pseudo_label, y, x, revision_idx) diff --git a/abl/utils/utils.py b/abl/utils/utils.py index cc9b6e3..2872747 100644 --- a/abl/utils/utils.py +++ b/abl/utils/utils.py @@ -109,6 +109,7 @@ def confidence_dist(pred_prob: np.ndarray, candidates_idxs: List[List[Any]]) -> cols = np.arange(len(candidates_idxs[0]))[None, :] return 1 - np.prod(pred_prob[cols, candidates_idxs], axis=1) + def avg_confidence_dist(pred_prob: np.ndarray, candidates_idxs: List[List[Any]]) -> np.ndarray: """ Compute the average confidence distance between prediction probabilities and candidates, @@ -130,6 +131,7 @@ def avg_confidence_dist(pred_prob: np.ndarray, candidates_idxs: List[List[Any]]) cols = np.arange(len(candidates_idxs[0]))[None, :] return 1 - np.average(pred_prob[cols, candidates_idxs], axis=1) + def to_hashable(x: Union[List[Any], Any]) -> Union[Tuple[Any, ...], Any]: """ Convert a nested list to a nested tuple so it is hashable.