-
Notifications
You must be signed in to change notification settings - Fork 3
/
ross_main.py
94 lines (69 loc) · 2.59 KB
/
ross_main.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
import sys
import numpy as np
from ross_util import write_submission, submit_to_kaggle
from ross_model import NN_Embedding
from ross_basemodel import NN_Embedding_Base
from ross_data import get_dataset, get_dataset_5_9
MODEL = NN_Embedding
#MODEL = NN_Embedding_Base
N_NETWORKS = 1
VALIDATION_WEEKS = 0
DO_EVAL = VALIDATION_WEEKS > 0
INIT_EPOCH = 0
EPOCHS = 25
BATCH_SIZE = 2048
FILT_STORES_FOR_TRAIN = True
FILT_STORES_FOR_VALID = True
SAVE_CHECKPT = False
SAVE_MODEL = True
PRINT_SUMMARY = True
SUBMIT = True
saved_model_file = './models/model_1.hdf5'
#saved_model_file = './output/checkpt/weights.40.hdf5'
submission_file = './output/pred2018-3-29.csv'
model_dir = './models/'
def load_models_from_hdf(model_files):
models = []
for model_file in model_files:
print('loading model from', model_file)
model = MODEL()
model.model.load_weights(model_file)
models.append(model)
return models
def main():
train_set, valid_set, X_test = get_dataset(VALIDATION_WEEKS, FILT_STORES_FOR_TRAIN, FILT_STORES_FOR_VALID)
models = []
for i in range(N_NETWORKS):
print('\nNN_Embedding Model', i+1)
print('-'*50)
model = MODEL(print_model_summary=PRINT_SUMMARY, save_checkpt=SAVE_CHECKPT)
if INIT_EPOCH > 0:
model.model.load_weights(saved_model_file)
model.fit(train_set, valid_set, batch_size=BATCH_SIZE, epochs=EPOCHS + INIT_EPOCH, init_epoch=INIT_EPOCH)
if SAVE_MODEL:
print('saving model', i+1)
model.model.save(model_dir + 'model_'+str(i+1)+'.hdf5')
models.append(model)
# models.extend(load_models_from_hdf(['./output/checkpt/weights.{:02d}.hdf5'.format(i)
# for i in range(EPOCHS-2, EPOCHS)]))
if DO_EVAL:
errors = [model.eval() for model in models]
for i,err in enumerate(errors):
print('Model {}: RMSPE = {}'.format(i+1, err))
print('Mean:', np.mean(errors))
for i, model in enumerate(models):
model.plot_loss('./output/loss_model_{}.png'.format(i+1))
if SUBMIT:
if len(sys.argv) > 1:
filename = './output/'+sys.argv[1]
else:
filename = submission_file
write_submission(models, X_test, filename)
submit_to_kaggle(filename)
if N_NETWORKS > 1:
for i in range(N_NETWORKS):
filename = '{}_model_{}.csv'.format(submission_file[:-4], i+1)
write_submission([models[i]], X_test, filename)
submit_to_kaggle(filename)
if __name__=='__main__':
main()