-
Notifications
You must be signed in to change notification settings - Fork 47
/
run.py
191 lines (169 loc) · 6.44 KB
/
run.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
137
138
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
"""Train Knowledge Graph embeddings for link prediction."""
import argparse
import json
import logging
import os
import torch
import torch.optim
import models
import optimizers.regularizers as regularizers
from datasets.kg_dataset import KGDataset
from models import all_models
from optimizers.kg_optimizer import KGOptimizer
from utils.train import get_savedir, avg_both, format_metrics, count_params
parser = argparse.ArgumentParser(
description="Knowledge Graph Embedding"
)
parser.add_argument(
"--dataset", default="WN18RR", choices=["FB15K", "WN", "WN18RR", "FB237", "YAGO3-10"],
help="Knowledge Graph dataset"
)
parser.add_argument(
"--model", default="RotE", choices=all_models, help="Knowledge Graph embedding model"
)
parser.add_argument(
"--regularizer", choices=["N3", "F2"], default="N3", help="Regularizer"
)
parser.add_argument(
"--reg", default=0, type=float, help="Regularization weight"
)
parser.add_argument(
"--optimizer", choices=["Adagrad", "Adam", "SparseAdam"], default="Adagrad",
help="Optimizer"
)
parser.add_argument(
"--max_epochs", default=50, type=int, help="Maximum number of epochs to train for"
)
parser.add_argument(
"--patience", default=10, type=int, help="Number of epochs before early stopping"
)
parser.add_argument(
"--valid", default=3, type=float, help="Number of epochs before validation"
)
parser.add_argument(
"--rank", default=1000, type=int, help="Embedding dimension"
)
parser.add_argument(
"--batch_size", default=1000, type=int, help="Batch size"
)
parser.add_argument(
"--neg_sample_size", default=50, type=int, help="Negative sample size, -1 to not use negative sampling"
)
parser.add_argument(
"--dropout", default=0, type=float, help="Dropout rate"
)
parser.add_argument(
"--init_size", default=1e-3, type=float, help="Initial embeddings' scale"
)
parser.add_argument(
"--learning_rate", default=1e-1, type=float, help="Learning rate"
)
parser.add_argument(
"--gamma", default=0, type=float, help="Margin for distance-based losses"
)
parser.add_argument(
"--bias", default="constant", type=str, choices=["constant", "learn", "none"], help="Bias type (none for no bias)"
)
parser.add_argument(
"--dtype", default="double", type=str, choices=["single", "double"], help="Machine precision"
)
parser.add_argument(
"--double_neg", action="store_true",
help="Whether to negative sample both head and tail entities"
)
parser.add_argument(
"--debug", action="store_true",
help="Only use 1000 examples for debugging"
)
parser.add_argument(
"--multi_c", action="store_true", help="Multiple curvatures per relation"
)
def train(args):
save_dir = get_savedir(args.model, args.dataset)
# file logger
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
filename=os.path.join(save_dir, "train.log")
)
# stdout logger
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s")
console.setFormatter(formatter)
logging.getLogger("").addHandler(console)
logging.info("Saving logs in: {}".format(save_dir))
# create dataset
dataset_path = os.path.join(os.environ["DATA_PATH"], args.dataset)
dataset = KGDataset(dataset_path, args.debug)
args.sizes = dataset.get_shape()
# load data
logging.info("\t " + str(dataset.get_shape()))
train_examples = dataset.get_examples("train")
valid_examples = dataset.get_examples("valid")
test_examples = dataset.get_examples("test")
filters = dataset.get_filters()
# save config
with open(os.path.join(save_dir, "config.json"), "w") as fjson:
json.dump(vars(args), fjson)
# create model
model = getattr(models, args.model)(args)
total = count_params(model)
logging.info("Total number of parameters {}".format(total))
device = "cuda"
model.to(device)
# get optimizer
regularizer = getattr(regularizers, args.regularizer)(args.reg)
optim_method = getattr(torch.optim, args.optimizer)(model.parameters(), lr=args.learning_rate)
optimizer = KGOptimizer(model, regularizer, optim_method, args.batch_size, args.neg_sample_size,
bool(args.double_neg))
counter = 0
best_mrr = None
best_epoch = None
logging.info("\t Start training")
for step in range(args.max_epochs):
# Train step
model.train()
train_loss = optimizer.epoch(train_examples)
logging.info("\t Epoch {} | average train loss: {:.4f}".format(step, train_loss))
# Valid step
model.eval()
valid_loss = optimizer.calculate_valid_loss(valid_examples)
logging.info("\t Epoch {} | average valid loss: {:.4f}".format(step, valid_loss))
if (step + 1) % args.valid == 0:
valid_metrics = avg_both(*model.compute_metrics(valid_examples, filters))
logging.info(format_metrics(valid_metrics, split="valid"))
valid_mrr = valid_metrics["MRR"]
if not best_mrr or valid_mrr > best_mrr:
best_mrr = valid_mrr
counter = 0
best_epoch = step
logging.info("\t Saving model at epoch {} in {}".format(step, save_dir))
torch.save(model.cpu().state_dict(), os.path.join(save_dir, "model.pt"))
model.cuda()
else:
counter += 1
if counter == args.patience:
logging.info("\t Early stopping")
break
elif counter == args.patience // 2:
pass
# logging.info("\t Reducing learning rate")
# optimizer.reduce_lr()
logging.info("\t Optimization finished")
if not best_mrr:
torch.save(model.cpu().state_dict(), os.path.join(save_dir, "model.pt"))
else:
logging.info("\t Loading best model saved at epoch {}".format(best_epoch))
model.load_state_dict(torch.load(os.path.join(save_dir, "model.pt")))
model.cuda()
model.eval()
# Validation metrics
valid_metrics = avg_both(*model.compute_metrics(valid_examples, filters))
logging.info(format_metrics(valid_metrics, split="valid"))
# Test metrics
test_metrics = avg_both(*model.compute_metrics(test_examples, filters))
logging.info(format_metrics(test_metrics, split="test"))
if __name__ == "__main__":
train(parser.parse_args())