pygod.nn.functional#

Functional Interface for PyGOD

pygod.nn.functional.KL_neighbor_loss(predictions, targets, mask_len, device)[source]#

The local neighor distribution KL divergence loss used in GAD-NR. Source: https://github.com/Graph-COM/GAD-NR/blob/master/GAD-NR_inj_cora.ipynb

pygod.nn.functional.W2_neighbor_loss(predictions, targets, mask_len, device)[source]#

The local neighor distribution W2 loss used in GAD-NR. Source: https://github.com/Graph-COM/GAD-NR/blob/master/GAD-NR_inj_cora.ipynb

pygod.nn.functional.double_recon_loss(x, x_, s, s_, weight=0.5, pos_weight_a=0.5, pos_weight_s=0.5, bce_s=False)[source]#

Double reconstruction loss function for feature and structure. The loss function is defined as \(\alpha \symbf{E_a} + (1-\alpha) \symbf{E_s}\), where \(\alpha\) is the weight between 0 and 1 inclusive, and \(\symbf{E_a}\) and \(\symbf{E_s}\) are the reconstruction loss for feature and structure, respectively. The first dimension is kept for outlier scores of each node.

For feature reconstruction, we use mean squared error loss: \(\symbf{E_a} = \|\symbf{X}-\symbf{X}'\|\odot H\), where \(H=\begin{cases}1 - \eta & \text{if }x_{ij}=0\\ \eta & \text{if }x_{ij}>0\end{cases}\), and \(\eta\) is the positive weight for feature.

For structure reconstruction, we use mean squared error loss by default: \(\symbf{E_s} = \|\symbf{S}-\symbf{S}'\|\odot \Theta\), where \(\Theta=\begin{cases}1 - \theta & \text{if }s_{ij}=0\\ \theta & \text{if }s_{ij}>0 \end{cases}\), and \(\theta\) is the positive weight for structure. Alternatively, we can use binary cross entropy loss for structure reconstruction: \(\symbf{E_s} = \text{BCE}(\symbf{S}, \symbf{S}' \odot \Theta)\).

Parameters:
  • x (torch.Tensor) – Ground truth node feature

  • x – Reconstructed node feature

  • s (torch.Tensor) – Ground truth node structure

  • s – Reconstructed node structure

  • weight (float, optional) – Balancing weight \(\alpha\) between 0 and 1 inclusive between node feature and graph structure. Default: 0.5.

  • pos_weight_a (float, optional) – Positive weight for feature \(\eta\). Default: 0.5.

  • pos_weight_s (float, optional) – Positive weight for structure \(\theta\). Default: 0.5.

  • bce_s (bool, optional) – Use binary cross entropy for structure reconstruction loss.

Returns:

score – Outlier scores of shape \(N\) with gradients.

Return type:

torch.tensor