-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain_clustering_loss.py
86 lines (70 loc) · 2.71 KB
/
main_clustering_loss.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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 06 17:12:58 2017
@author: sakurai
"""
import colorama
from sklearn.model_selection import ParameterSampler
from lib.functions.clustering_loss import clustering_loss
from lib.common.utils import (
UniformDistribution, LogUniformDistribution, load_params)
from lib.common.train_eval import train
colorama.init()
def lossfun_one_batch(model, params, batch):
x_data, c_data = batch
x_data = model.xp.asarray(x_data)
c_data = model.xp.asarray(c_data)
y = model(x_data)
# decay gamma at regular interval
if type(params.gamma) is not float:
params.gamma = params.gamma_init
params.num_updates = 0
else:
if (params.num_updates != 0 and
params.num_updates % params.num_batches_per_epoch == 0):
params.gamma *= params.gamma_decay
params.num_updates += 1
return clustering_loss(y, c_data, params.gamma)
if __name__ == '__main__':
param_filename = 'clustering_cub200_2011.yaml'
random_search_mode = True
random_state = None
num_runs = 100000
save_distance_matrix = False
if random_search_mode:
param_distributions = dict(
learning_rate=LogUniformDistribution(low=1e-6, high=1e-4),
gamma_init=LogUniformDistribution(low=1e+1, high=1e+4),
gamma_decay=UniformDistribution(low=0.7, high=1.0),
l2_weight_decay=LogUniformDistribution(low=1e-5, high=1e-2),
optimizer=['RMSProp', 'Adam'] # 'RMSPeop' or 'Adam'
)
static_params = dict(
num_epochs=15,
num_batches_per_epoch=500,
batch_size=120,
out_dim=64,
# learning_rate=0.0001,
# gamma_init=10.0,
# gamma_decay=0.94,
crop_size=224,
normalize_output=True,
# l2_weight_decay=0, # non-negative constant
# optimizer='RMSProp', # 'Adam' or 'RMSPeop'
distance_type='euclidean', # 'euclidean' or 'cosine'
dataset='cub200_2011', # 'cars196' or 'cub200_2011' or 'products'
method='clustering' # sampling method for batch construction
)
sampler = ParameterSampler(param_distributions, num_runs, random_state)
for random_params in sampler:
params = {}
params.update(random_params)
params.update(static_params)
stop = train(__file__, lossfun_one_batch, params,
save_distance_matrix)
if stop:
break
else:
print('Train once using config file "{}".'.format(param_filename))
params = load_params(param_filename)
train(__file__, lossfun_one_batch, params, save_distance_matrix)