-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
136 lines (117 loc) · 3.87 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import argparse
import datetime as dt
import json
import os
import csv
import pickle
import shutil
import time
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset, random_split
from tqdm import tqdm
from datasets.graph_dataset import GraphEmbeddingDataset
from models import GIN
from utils import ConfigParse, get_logger, sup_loss, unsup_loss
class Args:
seed = 2020
epochs = 10000
node_feature_dim = 64
nhid = 64
batch_size = 256
dropout = 0.1
learning_rate = 0.001
ratio1 = 1.0
ratio2 = 1.0
ratio3 = 0.8
weight_decay = 5e-4
device = "cuda:0"
mode = "RW" # Specify hypergraph construction mode NEighbor(NE)/RandomWalk(RW)
patience = 3
k = 5 # Hyperparameter for construction hyperedge
args = Args()
dataset = GraphEmbeddingDataset(args)
args.num_features = dataset.number_features
device = args.device
model = GIN(args).to(args.device)
# model = BaseModel(args).to(args.device)
optimizer = torch.optim.Adam(
model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay
)
loss_fn = unsup_loss
def train():
print("\nModel training.\n")
start = time.time()
val_loss_values = []
patience_cnt = 0
best_epoch = 0
min_loss = 1e10
# with torch.autograd.detect_anomaly():
for epoch in range(args.epochs):
model.train()
all_loss = []
batches = dataset.create_batches(dataset.training_funcs, dataset.collate)
for index, batch_pair in enumerate(batches):
optimizer.zero_grad()
# print(data["g1"])
pred = model(batch_pair.to(device))
loss = loss_fn(pred, device=device)
loss.backward()
optimizer.step()
all_loss.append(loss.item())
loss = sum(all_loss) / len(all_loss)
# start validate at 9000th iteration
if epoch + 1 < 5:
end = time.time()
print(
"Epoch: {:05d},".format(epoch + 1),
"loss_train: {:.6f},".format(loss),
"time: {:.6f}s".format(end - start),
)
else:
val_loss, aucscore = validate(dataset, dataset.validation_funcs)
end = time.time()
print(
"Epoch: {:05d},".format(epoch + 1),
"loss_train: {:.6f},".format(loss),
"loss_val: {:.6f},".format(val_loss),
"AUC: {:.6f},".format(aucscore),
"time: {:.6f}s".format(end - start),
)
val_loss_values.append(val_loss)
if val_loss_values[-1] < min_loss:
min_loss = val_loss_values[-1]
patience_cnt = 0
torch.save(
model.state_dict(),
"checkpoint/graph_model.pth",
)
else:
patience_cnt += 1
if patience_cnt == args.patience:
print(f"early stopping in epoch {epoch}")
break
print(
"Optimization Finished! Total time elapsed: {:.6f}".format(time.time() - start)
)
def validate(datasets, funcs):
model.eval()
all_loss = []
with torch.no_grad():
batches = datasets.create_batches(funcs, datasets.collate)
for index, batch_pair in enumerate(batches):
pred = model(batch_pair.to(device))
loss = loss_fn(pred, device=device)
all_loss.append(loss.item())
loss = sum(all_loss) / len(all_loss)
return loss, 0
if __name__ == "__main__":
train()
best_model = "checkpoint/graph_model.pth"
model.load_state_dict(torch.load("{}.pth".format(best_model)))
print("\nModel evaluation.")
test_loss, test_auc = validate(dataset, dataset.testing_funcs)
print("Test set results, loss = {:.6f}, AUC = {:.6f}".format(test_loss, test_auc))