-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_all.py
120 lines (99 loc) · 4.41 KB
/
plot_all.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
import sys
import argparse
import ujson as json
import matplotlib.pyplot as plt
import numpy as np
from time import time
n_seed = 3
def parse_args():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--log-path', type=str, default='experiment/plot_20/')
# parser.add_argument('--log-file', type=str, default='')
parser.add_argument('--dataset', type=str, default='freebase')
parser.add_argument('--title', type=str, default='DBLP')
parser.add_argument('--epochs', type=int, default='300')
args = parser.parse_args()
return args
def plot_loss(args):
models=['HGCN','HAN','HGT','Conch5','MAGNN'] #'Hetgnn',
colors=['goldenrod','lawngreen','blue','orchid','gray']
# fill_colors=['pink','yellow','blue','orchid']
n_models = len(models)
plt.figure()
plt.title(args.title)
fig, ax = plt.subplots()
# plt.xlim(0, args.epochs)
plt.xlim(0, 200)
# x_axis = np.arange(1,args.epochs)
# x_axis = np.arange(1,args.epochs+1,20)
# plt.ylim(0, 1)
for i,model in enumerate(models):
epoches = [[]for _ in range(n_seed)]
time = [[]for _ in range(n_seed)]
val_loss = [[]for _ in range(n_seed)]
train_loss = [[]for _ in range(n_seed)]
val_acc = [[]for _ in range(n_seed)]
train_acc = [[]for _ in range(n_seed)]
test_acc = [[]for _ in range(n_seed)]
for seed in range(n_seed):
with open('{}/{}/{}_{}.txt'.format(args.log_path,args.dataset, model,seed), mode='r') as f:
print('file: {}/{}/{}_{}.txt'.format(args.log_path,args.dataset, model,seed))
for line in f:
if '{' not in line:
continue
if 'Graph' in line or 'num' in line: continue
# print(line)
line = json.loads(line)
# if 'train_loss' in line:
# train_loss[seed].append(line['train_loss'])
# if 'val_loss' in line:
# val_loss[seed].append(line['val_loss'])
if 'epoch' in line:
epoches[seed].append(line['epoch'])
if 'time' in line:
time[seed].append(line['time'])
# if 'train_metric' in line:
# if line['epoch_progress'] == 0:
# train_acc.append(line['train_metric']['accuracy'])
if 'val_metric' in line:
val_acc[seed].append(line['val_metric']['accuracy'])
# if 'test_metric' in line:
# test_acc[seed].append(line['test_metric']['accuracy'])
l = min(len(val_acc[0]), len(epoches[0]), args.epochs)#len(test_acc[0])
print(l)
train_loss[seed] = train_loss[seed][:l]
val_loss[seed] = val_loss[seed][:l]
test_acc[seed] = test_acc[seed][:l]
val_acc[seed] = val_acc[seed][:l]
epoches[seed] = epoches[seed][:l]
time[seed] = time[seed][:l]
# print(len(time[seed]))
# epoches = np.array(epoches)[:,:l]
val_acc = np.array(val_acc)[:,:l]
time = np.array(time)[:,:l]
# print (time)
#
val_acc = val_acc[:,:-1:1]
time = time[:,:-1:1]
# plt.subplot(211)
# ax.plot(x_axis, np.mean(val_acc,axis=0), colors[i], label=model)
# ax.fill_between(x_axis, np.max(val_acc,axis=0),np.min(val_acc,axis=0),color=colors[i], alpha=0.2)
ax.plot(np.mean(time,axis=0), np.mean(val_acc,axis=0), colors[i], label=model)
# ax.fill_between(np.mean(time,axis=0), np.max(val_acc,axis=0),np.min(val_acc,axis=0),color=colors[i], alpha=0.2)
# plt.plot(epoches, val_acc, 'g', label='val')
plt.ylabel('accuracy')
# plt.xlabel('epoches')
plt.xlabel('time')
plt.legend(loc='lower right')
# plt.subplot(212)
# plt.plot(epoches, test_acc, 'r', label='test')
# plt.plot(epoches, val_acc, 'g', label='val')
# plt.ylabel('accuracy')
# plt.xlabel('epoches')
# plt.legend(loc='lower right')
# plt.suptitle('file: {}{}.txt'.format(args.log_path, args.log_file))
plt.show()
plt.savefig('{}/{}_val.pdf'.format(args.log_path, args.dataset))
if __name__ == "__main__":
args = parse_args()
plot_loss(args)