Belle II Software light-2406-ragdoll
PerfectLCA Class Reference
Inheritance diagram for PerfectLCA:
Collaboration diagram for PerfectLCA:

Public Member Functions

def __init__ (self, ignore_index, output_transform, device='cpu')
 
def reset (self)
 
def update (self, output)
 
def compute (self)
 

Public Attributes

 ignore_index
 Ignore index.
 
 device
 CPU or GPU.
 

Protected Attributes

 _per_corrects
 Good samples.
 
 _num_examples
 Total samples.
 

Detailed Description

Computes the rate of perfectly predicted LCAS matrices over a batch.

``output_transform`` should return the following items: ``(edge_pred, edge_y, edge_index, u_y, batch, num_graphs)``.

* ``edge_pred`` must contain edge prediction logits and have shape (num_edges_in_batch, edge_classes);
* ``edge_y`` must contain edge ground-truth class indices and have shape (num_edges_in_batch, 1);
* ``edge index`` maps edges to its nodes;
* ``u_y`` is the signal/background class (always 1 in the current setting);
* ``batch`` maps nodes to their graph;
* ``num_graphs`` is the number of graph in a batch (could be derived from ``batch`` also).

.. seealso::
    `Ignite metrics <https://pytorch.org/ignite/metrics.html>`_

:param ignore_index: Class or list of classes to ignore during the computation (e.g. padding).
:type ignore_index: list[int]
:param output_transform: Function to transform engine's output to desired output.
:type output_transform: `function <https://docs.python.org/3/glossary.html#term-function>`_
:param device: ``cpu`` or ``gpu``.
:type device: str

Definition at line 17 of file metrics.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  ignore_index,
  output_transform,
  device = 'cpu' 
)
Initialization.

Definition at line 41 of file metrics.py.

41 def __init__(self, ignore_index, output_transform, device='cpu'):
42 """
43 Initialization.
44 """
45
46 self.ignore_index = ignore_index if isinstance(ignore_index, list) else [ignore_index]
47
48 self.device = device
49
50 self._per_corrects = None
51
52 self._num_examples = None
53
54 super(PerfectLCA, self).__init__(output_transform=output_transform, device=device)
55

Member Function Documentation

◆ compute()

def compute (   self)
Final result.

Definition at line 101 of file metrics.py.

101 def compute(self):
102 """
103 Final result.
104 """
105 if self._num_examples == 0:
106 raise NotComputableError(
107 "CustomAccuracy must have at least one example before it can be computed."
108 )
109 return self._per_corrects / self._num_examples
110
111

◆ reset()

def reset (   self)
Resets counters.

Definition at line 57 of file metrics.py.

57 def reset(self):
58 """
59 Resets counters.
60 """
61 self._per_corrects = 0
62 self._num_examples = 0
63
64 super(PerfectLCA, self).reset()
65

◆ update()

def update (   self,
  output 
)
Updates counts.

Definition at line 67 of file metrics.py.

67 def update(self, output):
68 """
69 Updates counts.
70 """
71 edge_pred, edge_y, edge_index, u_y, batch, num_graphs = output
72
73 num_graphs = num_graphs.item()
74
75 probs = torch.softmax(edge_pred, dim=1)
76 winners = probs.argmax(dim=1)
77
78 assert winners.shape == edge_y.shape, 'Edge predictions shape does not match target shape'
79
80 # Create a mask for the zeroth elements (padded entries)
81 mask = torch.ones(edge_y.size(), dtype=torch.long, device=self.device)
82 for ig_class in self.ignore_index:
83 mask &= (edge_y != ig_class)
84
85 # Zero the respective entries in the predictions
86 y_pred_mask = winners * mask
87 y_mask = edge_y * mask
88
89 # (N) compare the masked predictions with the target. The padded will be equal due to masking
90 truth = y_pred_mask.eq(y_mask) + 0 # +0 so it's not bool but 0 and 1
91 truth = scatter(truth, edge_index[0], reduce="min")
92 truth = scatter(truth, batch, reduce="min")
93
94 # Count the number of zero wrong predictions across the batch
95 batch_perfect = truth.sum().item()
96
97 self._per_corrects += batch_perfect
98 self._num_examples += num_graphs
99

Member Data Documentation

◆ _num_examples

_num_examples
protected

Total samples.

Definition at line 52 of file metrics.py.

◆ _per_corrects

_per_corrects
protected

Good samples.

Definition at line 50 of file metrics.py.

◆ device

device

CPU or GPU.

Definition at line 48 of file metrics.py.

◆ ignore_index

ignore_index

Ignore index.

Definition at line 46 of file metrics.py.


The documentation for this class was generated from the following file: