-
Notifications
You must be signed in to change notification settings - Fork 104
/
run_train.py
135 lines (111 loc) · 4.04 KB
/
run_train.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'maxim'
import numpy as np
from models import *
from train import JobRunner, JobInfo
import util
def iterate_neural(job_info, job_runner, iterations=10, k_lim=21):
if NeuralNetworkModel is None:
return
job_runner.iterate(iterations, params_fun=lambda: {
'target': job_info.target,
'k': np.random.randint(1, k_lim),
'model_class': NeuralNetworkModel,
'model_params': {
'batch_size': np.random.choice([500, 1000, 2000, 4000]),
'epochs': 100,
'learning_rate': 10 ** np.random.uniform(-10, -2),
'init_sigma': 10 ** np.random.uniform(-10, -3),
'layers': _random_layers(np.random.randint(1, 4)),
'cost_func': np.random.choice(['l1', 'l2']),
'lambda': 10 ** np.random.uniform(-8, -2),
}
})
def _random_layers(num):
return [{
'size': np.random.randint(50, 200),
'batchnorm': np.random.choice([True, False]),
'activation_func': np.random.choice(['relu', 'elu', 'sigmoid', 'leaky_relu', 'prelu']),
'dropout': np.random.uniform(0.1, 0.95),
} for _ in range(num)]
def iterate_rnn(job_info, job_runner, iterations=10):
if RecurrentModel is None:
return
job_runner.iterate(iterations, params_fun=lambda: {
'target': job_info.target,
'k': np.random.choice([24, 32, 48, 64, 96]),
'model_class': RecurrentModel,
'model_params': {
'batch_size': np.random.choice([1000, 2000, 4000]),
'epochs': 100,
'learning_rate': 10 ** np.random.uniform(-4, -2),
'layers': [np.random.choice([32, 64, 96]) for _ in range(np.random.randint(1, 4))],
'cell_type': np.random.choice(['lstm', 'gru']),
'double_state': np.random.choice([True, False]),
'dropout': np.random.uniform(0.0, 1.0),
'cost_func': np.random.choice(['l1', 'l2']),
'lambda': 10 ** np.random.uniform(-10, -6),
}
})
def iterate_cnn(job_info, job_runner, iterations=10):
if ConvModel is None:
return
job_runner.iterate(iterations, params_fun=lambda: {
'target': job_info.target,
'k': np.random.choice([24, 32, 48, 64]),
'model_class': ConvModel,
'model_params': {
'batch_size': np.random.choice([1000, 2000, 4000]),
'epochs': 150,
'learning_rate': 10 ** np.random.uniform(-4, -2),
'layers': [(np.random.choice([32, 64, 96, 128]),
np.random.randint(2, 6))
for _ in range(np.random.randint(1, 4))],
'dropout': np.random.uniform(0.0, 0.7),
'cost_func': np.random.choice(['l1', 'l2']),
'lambda': 10 ** np.random.uniform(-10, -6),
}
})
def iterate_linear(job_info, job_runner, k_lim=25):
if LinearModel is None:
return
for k in range(1, k_lim):
job_runner.single_run(**{
'target': job_info.target,
'k': k,
'model_class': LinearModel,
'model_params': {}
})
def iterate_xgb(job_info, job_runner, iterations=10, k_lim=21):
if XgbModel is None:
return
job_runner.iterate(iterations, params_fun=lambda: {
'target': job_info.target,
'k': np.random.randint(1, k_lim),
'model_class': XgbModel,
'model_params': {
'max_depth': np.random.randint(3, 8),
'n_estimators': np.random.randint(100, 300),
'learning_rate': 10 ** np.random.uniform(-2, -0.5),
'gamma': np.random.uniform(0, 0.1),
'subsample': np.random.uniform(0.5, 1),
}
})
def main():
tickers, periods, targets = util.parse_command_line(default_periods=['day'],
default_targets=['high'])
while True:
for ticker in tickers:
for period in periods:
for target in targets:
job_info = JobInfo('_data', '_zoo', name='%s_%s' % (ticker, period), target=target)
job_runner = JobRunner(job_info, limit=np.median)
iterate_linear(job_info, job_runner)
iterate_neural(job_info, job_runner)
iterate_xgb(job_info, job_runner)
iterate_rnn(job_info, job_runner)
iterate_cnn(job_info, job_runner)
job_runner.print_result()
if __name__ == '__main__':
main()