.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/2_convert.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_2_convert.py: Score Conversion ================ Currently, the majority of outlier detectors are on node level. However, in some real-world applications, we may interest in the outlier edges or outlier graphs. In this tutorial, we introduce score converters provided in ``pygod.utils`` module, including ``to_edge_score`` and ``to_graph_score``. (Time estimate: 3 minutes) .. GENERATED FROM PYTHON SOURCE LINES 15-18 Data Loading ------------ We first load the data from PyGOD with ``load_data`` function. .. GENERATED FROM PYTHON SOURCE LINES 18-25 .. code-block:: Python from pygod.utils import load_data data = load_data('weibo') print(data) .. rst-class:: sphx-glr-script-out .. code-block:: none Data(x=[8405, 400], edge_index=[2, 407963], y=[8405], train_mask=[8405], test_mask=[8405], val_mask=[8405]) .. GENERATED FROM PYTHON SOURCE LINES 26-31 Detector Training ----------------- Initialize and train a detector in PyGOD. Here, we use ``pygod.detector.DOMINANT`` as an example. For faster demonstration, we set ``epoch`` to 3. .. GENERATED FROM PYTHON SOURCE LINES 31-38 .. code-block:: Python from pygod.detector import DOMINANT detector = DOMINANT(epoch=3) detector.fit(data) .. rst-class:: sphx-glr-script-out .. code-block:: none DOMINANT(act=, backbone=, batch_size=8405, compile_model=False, contamination=0.1, dropout=0.0, epoch=3, gpu=None, hid_dim=64, lr=0.004, num_layers=4, num_neigh=[-1, -1, -1, -1], save_emb=False, sigmoid_s=False, verbose=0, weight=0.5, weight_decay=0.0) .. GENERATED FROM PYTHON SOURCE LINES 39-43 Obtaining Node Score -------------------- After training, we obtain raw outlier scores for each node with ``predict``. The shape of ``node_score`` is ``(N, )``. .. GENERATED FROM PYTHON SOURCE LINES 43-48 .. code-block:: Python node_score = detector.predict(data, return_pred=False, return_score=True) print(node_score.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none torch.Size([8405]) .. GENERATED FROM PYTHON SOURCE LINES 49-53 Converting Score to Edge Level ------------------------------ To detect outlier edges, we convert the outlier scores on node level to edge level. The shape of ``edge_score`` is ``(E, )``. .. GENERATED FROM PYTHON SOURCE LINES 53-60 .. code-block:: Python from pygod.utils import to_edge_score edge_score = to_edge_score(node_score, data.edge_index) print(edge_score.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none torch.Size([407963]) .. GENERATED FROM PYTHON SOURCE LINES 61-66 Converting Score to Graph Level ------------------------------- To detect outlier graphs, we convert the outlier scores on node level to graph level for each graph. ``graph_score`` is a scalar for each ``Data`` object. Here, we give an example for scoring a list of graph. .. GENERATED FROM PYTHON SOURCE LINES 66-78 .. code-block:: Python from pygod.utils import to_graph_score data_list = [data, data, data] graph_scores = [] for data in data_list: node_score = detector.predict(data, return_pred=False, return_score=True) graph_score = to_graph_score(node_score) graph_scores.append(graph_score.item()) print(graph_scores) .. rst-class:: sphx-glr-script-out .. code-block:: none [17773.826171875, 17773.826171875, 17773.826171875] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 19.528 seconds) .. _sphx_glr_download_tutorials_2_convert.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 2_convert.ipynb <2_convert.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 2_convert.py <2_convert.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 2_convert.zip <2_convert.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_