forked from ronekko/deep_metric_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_hyperparameters_cub200_2011_n_pair.py
196 lines (165 loc) · 5.25 KB
/
plot_hyperparameters_cub200_2011_n_pair.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
192
193
194
195
196
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 10 14:39:32 2017
@author: sakurai
"""
from collections import defaultdict
from glob import glob
import os
import time
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import six
from sklearn.preprocessing import LabelEncoder
def cast_if_number(string):
try:
return float(string)
except ValueError:
return string
def read_params(target_prefix, begin=None, end=None):
if os.path.exists('config'):
config_parser = six.moves.configparser.ConfigParser()
config_parser.read('config')
log_dir_path = os.path.expanduser(
config_parser.get('logs', 'dir_path'))
else:
log_dir_path = '.'
if begin:
begin = time.strptime(begin, "%Y%m%d%H%M%S")
if end:
end = time.strptime(end, "%Y%m%d%H%M%S")
params = []
for dir_path in glob(os.path.join(log_dir_path, target_prefix) + '*'):
dir_name = dir_path.split('\\')[-1]
score = dir_name.split('-')[-1]
timestamp = dir_name.split('-')[-2]
score = float(score)
timestamp = time.strptime(timestamp, "%Y%m%d%H%M%S")
if begin and timestamp < begin:
continue
if end and timestamp >= end:
continue
param_dict = dict(score=score)
with open(os.path.join(dir_path, 'log.txt')) as f:
for line in f.readlines():
key, value = line.split(': ')
param_dict[key] = cast_if_number(value.strip())
params.append(param_dict)
paramwise = defaultdict(list)
for p in params:
for k, v in p.items():
paramwise[k].append(v)
return paramwise
def read_learning_curves(target_prefix, begin=None, end=None):
if os.path.exists('config'):
config_parser = six.moves.configparser.ConfigParser()
config_parser.read('config')
log_dir_path = os.path.expanduser(
config_parser.get('logs', 'dir_path'))
else:
log_dir_path = '.'
if begin:
begin = time.strptime(begin, "%Y%m%d%H%M%S")
if end:
end = time.strptime(end, "%Y%m%d%H%M%S")
curves = []
for dir_path in glob(os.path.join(log_dir_path, target_prefix) + '*'):
dir_name = dir_path.split('\\')[-1]
timestamp = dir_name.split('-')[-2]
timestamp = time.strptime(timestamp, "%Y%m%d%H%M%S")
if begin and timestamp < begin:
continue
if end and timestamp >= end:
continue
test_log = np.load(os.path.join(dir_path, 'test_log.npy'))
curves.append(test_log.T[0])
return curves
if __name__ == '__main__':
target_prefix = 'cub200_2011-main_n_pair_mc'
# begin = '20170822121156'
begin = '20170823203509'
begin = '20170825130000'
begin = '20170827141940'
end = None
params = read_params(target_prefix, begin, end)
curves = read_learning_curves(target_prefix, begin, end)
# # filtering
# for i in list(range(len(curves)))[::-1]:
# if curves[i][:-2].max() > curves[i][-2:].max():
# curves.pop(i)
# for param in params.values():
# param.pop(i)
# Show learning curves
for curve in curves:
plt.plot(curve, linewidth=1)
plt.grid()
plt.show()
# Show scatter plots of parameters
scores = params['score']
param_name = 'learning_rate'
learning_rates = params[param_name]
plt.plot(learning_rates, scores, '.')
plt.title(param_name)
plt.xlabel(param_name)
plt.ylabel('score')
plt.xscale('log')
plt.grid()
plt.show()
param_name = 'loss_l2_reg'
loss_l2_reg = params[param_name]
plt.plot(loss_l2_reg, scores, '.')
plt.title(param_name)
plt.xlabel(param_name)
plt.ylabel('score')
plt.xscale('log')
plt.grid()
plt.show()
param_name = 'optimizer'
optimizers = params[param_name]
le = LabelEncoder().fit(optimizers)
plt.xticks(range(len(le.classes_)), le.classes_)
plt.plot(le.transform(optimizers), scores, '.')
plt.title(param_name)
plt.xlabel(param_name)
plt.ylabel('score')
plt.grid()
plt.show()
param_name = 'l2_weight_decay'
l2_weight_decays = params[param_name]
plt.plot(l2_weight_decays, scores, '.')
plt.title(param_name)
plt.xlabel(param_name)
plt.ylabel('score')
plt.xscale('log')
plt.grid()
plt.show()
param_name = 'out_dim'
out_dim = params[param_name]
plt.xticks(list(set(out_dim)))
plt.plot(out_dim, scores, '.')
plt.title(param_name)
plt.xlabel(param_name)
plt.ylabel('score')
# plt.xscale('log')
plt.grid()
plt.show()
# scatter plot on learning rate and loss-specific hyperparameter
plt.figure()
plt.xscale('log')
plt.yscale('log')
plt.xlabel('learning_rate')
plt.ylabel('loss_l2_reg')
plt.grid()
plt.scatter(learning_rates, loss_l2_reg, c=scores, cmap=plt.cm.rainbow)
plt.colorbar()
plt.show()
# 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(np.log10(learning_rates), np.log10(loss_l2_reg), scores,
c=scores, cmap=plt.cm.rainbow)
plt.xlabel('learning_rate in common log')
plt.ylabel('loss_l2_reg in common log')
plt.show()