main.py 6.26 KB
Newer Older
1
import argparse
2

3
4
import torch as th
import torch.nn.functional as F
5
import torch.optim as optim
6
7
from dataloader import GASDataset
from model import GAS
8
from sklearn.metrics import f1_score, precision_recall_curve, roc_auc_score
9
10
11
12
13
14
15
16
17
18


def main(args):
    # Step 1: Prepare graph data and retrieve train/validation/test index ============================= #
    # Load dataset
    dataset = GASDataset(args.dataset)
    graph = dataset[0]

    # check cuda
    if args.gpu >= 0 and th.cuda.is_available():
19
        device = "cuda:{}".format(args.gpu)
20
    else:
21
        device = "cpu"
22
23
24
25
26

    # binary classification
    num_classes = dataset.num_classes

    # retrieve labels of ground truth
27
    labels = graph.edges["forward"].data["label"].to(device).long()
28
29

    # Extract node features
30
31
32
    e_feat = graph.edges["forward"].data["feat"].to(device)
    u_feat = graph.nodes["u"].data["feat"].to(device)
    v_feat = graph.nodes["v"].data["feat"].to(device)
33
34

    # retrieve masks for train/validation/test
35
36
37
    train_mask = graph.edges["forward"].data["train_mask"]
    val_mask = graph.edges["forward"].data["val_mask"]
    test_mask = graph.edges["forward"].data["test_mask"]
38
39
40
41
42
43
44
45

    train_idx = th.nonzero(train_mask, as_tuple=False).squeeze(1).to(device)
    val_idx = th.nonzero(val_mask, as_tuple=False).squeeze(1).to(device)
    test_idx = th.nonzero(test_mask, as_tuple=False).squeeze(1).to(device)

    graph = graph.to(device)

    # Step 2: Create model =================================================================== #
46
47
48
49
50
51
52
53
54
55
56
57
    model = GAS(
        e_in_dim=e_feat.shape[-1],
        u_in_dim=u_feat.shape[-1],
        v_in_dim=v_feat.shape[-1],
        e_hid_dim=args.e_hid_dim,
        u_hid_dim=args.u_hid_dim,
        v_hid_dim=args.v_hid_dim,
        out_dim=num_classes,
        num_layers=args.num_layers,
        dropout=args.dropout,
        activation=F.relu,
    )
58
59
60
61
62

    model = model.to(device)

    # Step 3: Create training components ===================================================== #
    loss_fn = th.nn.CrossEntropyLoss()
63
64
65
    optimizer = optim.Adam(
        model.parameters(), lr=args.lr, weight_decay=args.weight_decay
    )
66
67
68
69
70
71
72
73
74

    # Step 4: training epochs =============================================================== #
    for epoch in range(args.max_epoch):
        # Training and validation using a full graph
        model.train()
        logits = model(graph, e_feat, u_feat, v_feat)

        # compute loss
        tr_loss = loss_fn(logits[train_idx], labels[train_idx])
75
76
77
78
79
80
81
82
83
        tr_f1 = f1_score(
            labels[train_idx].cpu(), logits[train_idx].argmax(dim=1).cpu()
        )
        tr_auc = roc_auc_score(
            labels[train_idx].cpu(), logits[train_idx][:, 1].detach().cpu()
        )
        tr_pre, tr_re, _ = precision_recall_curve(
            labels[train_idx].cpu(), logits[train_idx][:, 1].detach().cpu()
        )
84
85
86
87
        tr_rap = tr_re[tr_pre > args.precision].max()

        # validation
        valid_loss = loss_fn(logits[val_idx], labels[val_idx])
88
89
90
91
92
93
94
95
96
        valid_f1 = f1_score(
            labels[val_idx].cpu(), logits[val_idx].argmax(dim=1).cpu()
        )
        valid_auc = roc_auc_score(
            labels[val_idx].cpu(), logits[val_idx][:, 1].detach().cpu()
        )
        valid_pre, valid_re, _ = precision_recall_curve(
            labels[val_idx].cpu(), logits[val_idx][:, 1].detach().cpu()
        )
97
98
99
100
101
102
103
104
        valid_rap = valid_re[valid_pre > args.precision].max()

        # backward
        optimizer.zero_grad()
        tr_loss.backward()
        optimizer.step()

        # Print out performance
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        print(
            "In epoch {}, Train R@P: {:.4f} | Train F1: {:.4f} | Train AUC: {:.4f} | Train Loss: {:.4f}; "
            "Valid R@P: {:.4f} | Valid F1: {:.4f} | Valid AUC: {:.4f} | Valid loss: {:.4f}".format(
                epoch,
                tr_rap,
                tr_f1,
                tr_auc,
                tr_loss.item(),
                valid_rap,
                valid_f1,
                valid_auc,
                valid_loss.item(),
            )
        )
119
120
121
122
123
124
125
126
127

    # Test after all epoch
    model.eval()

    # forward
    logits = model(graph, e_feat, u_feat, v_feat)

    # compute loss
    test_loss = loss_fn(logits[test_idx], labels[test_idx])
128
129
130
131
132
133
134
135
136
    test_f1 = f1_score(
        labels[test_idx].cpu(), logits[test_idx].argmax(dim=1).cpu()
    )
    test_auc = roc_auc_score(
        labels[test_idx].cpu(), logits[test_idx][:, 1].detach().cpu()
    )
    test_pre, test_re, _ = precision_recall_curve(
        labels[test_idx].cpu(), logits[test_idx][:, 1].detach().cpu()
    )
137
138
    test_rap = test_re[test_pre > args.precision].max()

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    print(
        "Test R@P: {:.4f} | Test F1: {:.4f} | Test AUC: {:.4f} | Test loss: {:.4f}".format(
            test_rap, test_f1, test_auc, test_loss.item()
        )
    )


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="GCN-based Anti-Spam Model")
    parser.add_argument(
        "--dataset", type=str, default="pol", help="'pol', or 'gos'"
    )
    parser.add_argument(
        "--gpu", type=int, default=-1, help="GPU Index. Default: -1, using CPU."
    )
    parser.add_argument(
        "--e_hid_dim",
        type=int,
        default=128,
        help="Hidden layer dimension for edges",
    )
    parser.add_argument(
        "--u_hid_dim",
        type=int,
        default=128,
        help="Hidden layer dimension for source nodes",
    )
    parser.add_argument(
        "--v_hid_dim",
        type=int,
        default=128,
        help="Hidden layer dimension for destination nodes",
    )
    parser.add_argument(
        "--num_layers", type=int, default=2, help="Number of GCN layers"
    )
    parser.add_argument(
        "--max_epoch",
        type=int,
        default=100,
        help="The max number of epochs. Default: 100",
    )
    parser.add_argument(
        "--lr", type=float, default=0.001, help="Learning rate. Default: 1e-3"
    )
    parser.add_argument(
        "--dropout", type=float, default=0.0, help="Dropout rate. Default: 0.0"
    )
    parser.add_argument(
        "--weight_decay",
        type=float,
        default=5e-4,
        help="Weight Decay. Default: 0.0005",
    )
    parser.add_argument(
        "--precision",
        type=float,
        default=0.9,
        help="The value p in recall@p precision. Default: 0.9",
    )
199
200
201
202

    args = parser.parse_args()
    print(args)
    main(args)