-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_and_load.py
75 lines (62 loc) · 2.54 KB
/
save_and_load.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
"""save and load checkpoints and log progress"""
import os
import numpy as np
import datetime
class PopulationSaver():
"""object to save and load checkpoints and write logs"""
def __init__(self, model_name=None, load=False):
self.generation = 0
if model_name is None:
model_name = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
self.path = 'saved_runs'+ os.sep + model_name
if not os.path.isdir('saved_runs'):
os.mkdir('saved_runs')
if os.path.isdir(self.path):
if load == False:
raise Exception('model already exists')
elif load == True:
raise Exception('model doesn\'t exists')
else:
os.mkdir(self.path)
self.path += os.sep
self.log_file = self.path + 'log.txt'
if load:
highest = 0
for file_name in os.listdir(self.path):
split = file_name.split('_')
if len(split) == 2 and split[0] == 'generation':
highest = max(int(split[1].split('.')[0]), highest)
self.generation = highest
def log(self, *args, to_screen=False):
with open(self.log_file, mode='a') as log_file:
print(*args, file=log_file)
if to_screen:
print(*args)
def save(self, array):
"""save an array with population"""
np.savetxt(self.path + 'generation_' + str(self.generation) + '.csv', array, delimiter=',')
self.generation += 1
def save_best(self, array):
"""save an array with best picks"""
np.savetxt(self.path + 'best_' + str(self.generation) + '.csv', array, delimiter=',')
def load(self, generation=None):
"""load an array"""
if generation is None:
generation = self.generation
elif generation < 0:
generation = self.generation - generation
return np.loadtxt(self.path + 'generation_' + str(generation) + '.csv', delimiter=',')
if __name__ == '__main__':
population_saver = PopulationSaver()
population_saver.log('created')
a = np.array([[1, 2], [3, 4], [5, 6]])
population_saver.save(a)
population_saver.log('stored a with average value', np.mean(a))
b = a * 2
population_saver.save(b)
population_saver.log('stored b with average value', np.mean(b))
c = population_saver.load()
population_saver.log('loaded b into c')
population_saver.log('b == c', b == c)
population_saver.save_best(c)
population_saver.log('stored c as best')