.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/1_intro.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_1_intro.py: Detector Example ================ In this tutorial, you will learn the basic workflow of PyGOD with an example of DOMINANT. This tutorial assumes that you have basic familiarity with PyTorch and PyTorch Geometric (PyG). (Time estimate: 5 minutes) .. GENERATED FROM PYTHON SOURCE LINES 12-19 Data Loading ------------ PyGOD use ``torch_geometric.data.Data`` to handle the data. Here, we use Cora, a PyG built-in dataset, as an example. To load your own dataset into PyGOD, you can refer to `creating your own datasets tutorial `__ in PyG. .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: Python import torch_geometric.transforms as T from torch_geometric.datasets import Planetoid data = Planetoid('./data/Cora', 'Cora', transform=T.NormalizeFeatures())[0] .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.x Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.tx Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.allx Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.y Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ty Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ally Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.graph Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.test.index Processing... Done! .. GENERATED FROM PYTHON SOURCE LINES 27-32 Because there is no ground truth label of outliers in Cora, we follow the method used by DOMINANT to inject 100 contextual outliers and 100 structure outliers into the graph. Note: If your dataset already contains the outliers you want to detect, you don't have to inject more outliers. .. GENERATED FROM PYTHON SOURCE LINES 32-41 .. code-block:: Python import torch from pygod.generator import gen_contextual_outlier, gen_structural_outlier data, ya = gen_contextual_outlier(data, n=100, k=50) data, ys = gen_structural_outlier(data, m=10, n=10) data.y = torch.logical_or(ys, ya).long() .. GENERATED FROM PYTHON SOURCE LINES 42-46 We also provide various type of built-in datasets. You can load them by passing the name of the dataset to ``load_data`` function. See `data repository `__ for more details. .. GENERATED FROM PYTHON SOURCE LINES 46-53 .. code-block:: Python from pygod.utils import load_data data = load_data('inj_cora') data.y = data.y.bool() .. GENERATED FROM PYTHON SOURCE LINES 54-60 Initialization -------------- You can use any detector by simply initializing without passing any arguments. Default hyperparameters are ready for you. Of course, you can also customize the parameters by passing arguments. Here, we use ``pygod.detector.DOMINANT`` as an example. .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: Python from pygod.detector import DOMINANT detector = DOMINANT(hid_dim=64, num_layers=4, epoch=100) .. GENERATED FROM PYTHON SOURCE LINES 67-71 Training -------- To train the detector with the loaded data, simply feed the ``torch_geometric.data.Data`` object into the detector via ``fit``. .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python detector.fit(data) .. rst-class:: sphx-glr-script-out .. code-block:: none DOMINANT(act=, backbone=, batch_size=2708, compile_model=False, contamination=0.1, dropout=0.0, epoch=100, 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 76-82 Inference --------- After training, the detector is ready to use. You can use the detector to predict the labels, raw outlier scores, probability of the outlierness, and prediction confidence. Here, we use the loaded data as an example. .. GENERATED FROM PYTHON SOURCE LINES 82-101 .. code-block:: Python pred, score, prob, conf = detector.predict(data, return_pred=True, return_score=True, return_prob=True, return_conf=True) print('Labels:') print(pred) print('Raw scores:') print(score) print('Probability:') print(prob) print('Confidence:') print(conf) .. rst-class:: sphx-glr-script-out .. code-block:: none Labels: tensor([0, 0, 0, ..., 0, 0, 0]) Raw scores: tensor([1.0296, 0.9659, 1.2203, ..., 0.6119, 1.1292, 1.1304]) Probability: tensor([0.0784, 0.0671, 0.1121, ..., 0.0045, 0.0960, 0.0962]) Confidence: tensor([1., 1., 1., ..., 1., 1., 1.]) .. GENERATED FROM PYTHON SOURCE LINES 102-105 Evaluation ---------- To evaluate the performance outlier detector with AUC score, you can: .. GENERATED FROM PYTHON SOURCE LINES 105-111 .. code-block:: Python from pygod.metric import eval_roc_auc auc_score = eval_roc_auc(data.y, score) print('AUC Score:', auc_score) .. rst-class:: sphx-glr-script-out .. code-block:: none AUC Score: 0.7675492020526702 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 32.003 seconds) .. _sphx_glr_download_tutorials_1_intro.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 1_intro.ipynb <1_intro.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 1_intro.py <1_intro.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 1_intro.zip <1_intro.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_