Browse Source

[FIX] pass flake8

pull/5/head
troyyyyy 2 years ago
parent
commit
95233ac1bc
4 changed files with 26 additions and 23 deletions
  1. +1
    -0
      abl/data/structures/list_data.py
  2. +3
    -3
      abl/learning/basic_nn.py
  3. +20
    -20
      abl/reasoning/kb.py
  4. +2
    -0
      abl/utils/utils.py

+ 1
- 0
abl/data/structures/list_data.py View File

@@ -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.


+ 3
- 3
abl/learning/basic_nn.py View File

@@ -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



+ 20
- 20
abl/reasoning/kb.py View File

@@ -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)


+ 2
- 0
abl/utils/utils.py View File

@@ -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.


Loading…
Cancel
Save