-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainers.py
163 lines (141 loc) · 6.63 KB
/
Trainers.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
from typing import Any
from utils import EnsurePath, GetFromDict, set_instance_variable, search_dict
class Trainer:
def __init__(self, dict_, load=False):
if options is not None:
self.receive_options(options)
self.dict = dict_
#set_instance_variable(self, self.dict)
self.epoch_num = self.dict['epoch_num']
self.batch_num = self.dict['batch_num']
self.batch_size = self.dict['batch_size']
if not hasattr(self, 'anal_path'):
self.anal_path = self.dict.setdefault('anal_path', './anal/')
'''
self.epoch_index = GetFromDict(self.dict, 'epoch_index', default=self.epoch_start, write_default=True)
self.epoch_start = GetFromDict(self.dict, 'epoch_start', default=1, write_default=True)
self.epoch_end = GetFromDict(self.dict, 'epoch_end', default=self.epoch_um, write_default=True)
'''
self.epoch_index = 0
self.epoch_end = self.epoch_num - 1
# save directory setting
self.save_path = search_dict(self.dict, ['save_path', 'save_model_path', 'save_dir_model'], default='./saved_models/',
write_default=True, write_default_key='save_path')
EnsurePath(self.save_path)
self.save = search_dict(self.dict, ['save', 'save_model'], default=True, write_default=True)
self.save_after_train = GetFromDict(self.dict, 'save_after_train', default=True, write_default=True)
self.save_before_train = GetFromDict(self.dict, 'save_before_train', default=True, write_default=True)
self.anal_before_train = GetFromDict(self.dict, 'anal_before_train', default=True, write_default=True)
if self.save:
self.save_interval = search_dict(self.dict, ['save_interval', 'save_model_interval'], default=int(self.epoch_num / 10), write_default=True)
'''
if options is not None:
self.options = options
self.set_options()
'''
self.test_performs = self.dict['test_performs'] = {}
self.train_performs = self.dict['train_performs'] = {}
self.anal_model = self.dict.setdefault('anal_model', True)
def train(self, report_in_batch=None, report_interval=None):
if report_in_batch is None:
if not hasattr(self, 'report_in_batch'):
report_in_batch = True
else:
report_in_batch = self.report_in_batch
if report_in_batch:
if report_interval is None:
if not hasattr(self, 'report_interval'):
report_interval = int(self.batch_num / 40)
else:
report_interval = self.report_interval
if self.save_before_train:
self.agent.save(self.save_path, self.agent.dict['name'] + '_epoch=beforeTrain')
if self.anal_before_train:
self.anal(title='beforeTrain')
self.optimizer.update_before_train()
#print('epoch_index:%d epoch_end:%d'%(self.epoch_index, self.epoch_end))
while self.epoch_index <= self.epoch_end:
print('epoch=%d/%d'%(self.epoch_index, self.epoch_end), end=' ')
# train model
self.agent.reset_perform()
#batch_num = 0
for batch_index in range(self.batch_num):
#print(batch_index)
# prepare_data
'''
path = self.agent.walk_random(num=self.batch_size)
self.optimizer.train(path)
'''
self.agent.train(self.batch_size)
if report_in_batch:
if batch_index % report_interval == 0:
print('batch=%d/%d' % (batch_index, self.batch_num))
self.agent.report_perform()
self.agent.reset_perform()
print('lr: %.3e'%self.optimizer.Getlr())
#batch_num += 1
train_perform = self.agent.report_perform(prefix='train: ')
'''
# evaluate model
self.agent.reset_perform()
for data in list(train_loader):
inputs, labels = data
self.optimizer.evaluate(inputs.to(self.device), labels.to(self.device))
test_perform = self.agent.report_perform(prefix='test: ')
self.test_performs[self.epoch_index] = test_perform
'''
#print('save:%s save_interval:%d'%(self.save, self.save_interval))
if self.save_model and self.epoch_index % self.save_interval == 0:
print('saving_model at this epoch')
self.agent.save(self.save_path, self.agent.dict['name'] + '_epoch=%d' % self.epoch_index)
if self.anal_model and self.epoch_index % self.anal_interval == 1:
self.anal()
self.optimizer.update_epoch()
self.epoch_index += 1
if self.save_after_train:
self.agent.save(self.save_path, self.agent.dict['name'] + '_epoch=afterTrain')
'''
def receive_options(self, options):
self.options = options
self.device = options.device
self.optimizer = options.optimizer
self.agent = options.agent
#self.model = options.model
self.arenas = options.arenas
'''
def bind_agent(self, agent):
self.agent = agent
def bind_model(self, model):
self.model = model
def bind_optimizer(self, optimizer):
self.optimizer = optimizer
def anal(self, title=None, save_path=None, verbose=True):
if save_path is None:
if title is None:
save_path = self.anal_path + 'epoch=%d/'%(self.epoch_index)
else:
save_path = self.anal_path + 'epoch=%s/'%(title)
EnsurePath(save_path)
self.agent.anal(save_path=save_path, trainer=self)
'''
class Evaluator():
def __init__(self, dict_={}, options=None):
if options is not None:
self.receive_options(options)
self.dict = dict_
def bind_data_loader(self, data_loader):
self.data_loader = data_loader
def bind_model(self, model):
self.model = model
def receive_options(self, options):
self.options = options
self.device = self.options.device
def evaluate(self):
# evaluate model
train_loader, test_loader = self.data_loader.Getloader()
self.agent.reset_perform()
for data in list(train_loader):
inputs, labels = data
self.agent.Getperform(inputs.to(self.device), labels.to(self.device))
test_perform = self.agent.report_perform(prefix='test: ')
'''