graphwar

set_seed(seed: Optional[int] = None) Optional[int][source]

Set random seed for reproduction.

Parameters

seed (Optional[int], optional) – random seed, by default None

Returns

the random seed.

Return type

Optional[int]

Example

>>> from graphwar import set_seed
>>> set_seed(42)
42
is_edge_index(x: Any) bool[source]

Check if the input x is PyG-like edge_index with shape [2, M], where M is the number of edges.

Example

>>> from graphwar import is_edge_index
>>> import torch
>>> edges = torch.LongTensor([[1,2], [3,4]])
>>> is_edge_index(edges)
True
>>> is_edge_index(edges.t()))
False
class Surrogate(device: str = 'cpu')[source]

Base class for attacker or defenders that require a surrogate model for estimating labels or computing gradient information.

Parameters

device (str, optional) – the device of a model to use for, by default “cpu”

setup_surrogate(surrogate: Module, *, eps: float = 1.0, freeze: bool = True, required: Optional[Union[Module, Tuple[Module]]] = None) Surrogate[source]

Method used to initialize the (trained) surrogate model.

Parameters
  • surrogate (Module) – the input surrogate module

  • eps (float, optional) – temperature used for softmax activation, by default 1.0

  • freeze (bool, optional) – whether to freeze the model’s parameters to save time, by default True

  • required (Union[Module, Tuple[Module]], optional) – which class(es) of the surrogate model are required, by default None

Returns

the class itself

Return type

Surrogate

Raises
  • RuntimeError – if the surrogate model is not an instance of torch.nn.Module

  • RuntimeError – if the surrogate model is not an instance of required

estimate_self_training_labels(nodes: Optional[Tensor] = None) Tensor[source]

Estimate the labels of nodes using the trained surrogate model.

Parameters

nodes (Optional[Tensor], optional) – the input nodes, if None, it would be all nodes in the graph, by default None

Returns

the labels of the input nodes.

Return type

Tensor

freeze_surrogate() Surrogate[source]

Freezie the parameters of the surrogate model.

Returns

the class itself

Return type

Surrogate

defrozen_surrogate() Surrogate[source]

Defrozen the parameters of the surrogate model

Returns

the class itself

Return type

Surrogate