"src/array/cuda/spmm_hetero.hip" did not exist on "96297fb8fdd629fc2fd52872d718d78fa491cba1"
main.py 4.6 KB
Newer Older
1
import argparse
2
3
import warnings

4
5
6
7
import numpy as np
import torch as th
import torch.nn as nn

8
warnings.filterwarnings("ignore")
9
10

from dataset import process_dataset
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
11
from model import LogReg, MVGRL
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
parser = argparse.ArgumentParser(description="mvgrl")

parser.add_argument(
    "--dataname", type=str, default="cora", help="Name of dataset."
)
parser.add_argument(
    "--gpu", type=int, default=0, help="GPU index. Default: -1, using cpu."
)
parser.add_argument("--epochs", type=int, default=500, help="Training epochs.")
parser.add_argument(
    "--patience",
    type=int,
    default=20,
    help="Patient epochs to wait before early stopping.",
)
parser.add_argument(
    "--lr1", type=float, default=0.001, help="Learning rate of mvgrl."
)
parser.add_argument(
    "--lr2", type=float, default=0.01, help="Learning rate of linear evaluator."
)
parser.add_argument(
    "--wd1", type=float, default=0.0, help="Weight decay of mvgrl."
)
parser.add_argument(
    "--wd2", type=float, default=0.0, help="Weight decay of linear evaluator."
)
parser.add_argument(
    "--epsilon",
    type=float,
    default=0.01,
    help="Edge mask threshold of diffusion graph.",
)
parser.add_argument(
    "--hid_dim", type=int, default=512, help="Hidden layer dim."
)
49
50
51
52
53

args = parser.parse_args()

# check cuda
if args.gpu != -1 and th.cuda.is_available():
54
    args.device = "cuda:{}".format(args.gpu)
55
else:
56
    args.device = "cpu"
57

58
if __name__ == "__main__":
59
60
61
    print(args)

    # Step 1: Prepare data =================================================================== #
62
63
64
65
66
67
68
69
70
71
    (
        graph,
        diff_graph,
        feat,
        label,
        train_idx,
        val_idx,
        test_idx,
        edge_weight,
    ) = process_dataset(args.dataname, args.epsilon)
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    n_feat = feat.shape[1]
    n_classes = np.unique(label).shape[0]

    graph = graph.to(args.device)
    diff_graph = diff_graph.to(args.device)
    feat = feat.to(args.device)
    edge_weight = th.tensor(edge_weight).float().to(args.device)

    train_idx = train_idx.to(args.device)
    val_idx = val_idx.to(args.device)
    test_idx = test_idx.to(args.device)

    n_node = graph.number_of_nodes()
    lbl1 = th.ones(n_node * 2)
    lbl2 = th.zeros(n_node * 2)
    lbl = th.cat((lbl1, lbl2))

    # Step 2: Create model =================================================================== #
    model = MVGRL(n_feat, args.hid_dim)
    model = model.to(args.device)

    lbl = lbl.to(args.device)

    # Step 3: Create training components ===================================================== #
96
97
98
    optimizer = th.optim.Adam(
        model.parameters(), lr=args.lr1, weight_decay=args.wd1
    )
99
100
101
    loss_fn = nn.BCEWithLogitsLoss()

    # Step 4: Training epochs ================================================================ #
102
    best = float("inf")
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    cnt_wait = 0
    for epoch in range(args.epochs):
        model.train()
        optimizer.zero_grad()

        shuf_idx = np.random.permutation(n_node)
        shuf_feat = feat[shuf_idx, :]
        shuf_feat = shuf_feat.to(args.device)

        out = model(graph, diff_graph, feat, shuf_feat, edge_weight)
        loss = loss_fn(out, lbl)

        loss.backward()
        optimizer.step()

118
        print("Epoch: {0}, Loss: {1:0.4f}".format(epoch, loss.item()))
119
120
121
122

        if loss < best:
            best = loss
            cnt_wait = 0
123
            th.save(model.state_dict(), "model.pkl")
124
125
126
127
        else:
            cnt_wait += 1

        if cnt_wait == args.patience:
128
            print("Early stopping")
129
130
            break

131
    model.load_state_dict(th.load("model.pkl"))
132
133
134
135
136
137
138
139
140
141
142
143
144
    embeds = model.get_embedding(graph, diff_graph, feat, edge_weight)

    train_embs = embeds[train_idx]
    test_embs = embeds[test_idx]

    label = label.to(args.device)
    train_labels = label[train_idx]
    test_labels = label[test_idx]
    accs = []

    # Step 5:  Linear evaluation ========================================================== #
    for _ in range(5):
        model = LogReg(args.hid_dim, n_classes)
145
146
147
        opt = th.optim.Adam(
            model.parameters(), lr=args.lr2, weight_decay=args.wd2
        )
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

        model = model.to(args.device)
        loss_fn = nn.CrossEntropyLoss()
        for epoch in range(300):
            model.train()
            opt.zero_grad()
            logits = model(train_embs)
            loss = loss_fn(logits, train_labels)
            loss.backward()
            opt.step()

        model.eval()
        logits = model(test_embs)
        preds = th.argmax(logits, dim=1)
        acc = th.sum(preds == test_labels).float() / test_labels.shape[0]
        accs.append(acc * 100)

    accs = th.stack(accs)
Hengrui Zhang's avatar
Hengrui Zhang committed
166
    print(accs.mean().item(), accs.std().item())