This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainF.py
225 lines (199 loc) · 12.6 KB
/
trainF.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import sys
import argparse
import time
import numpy as np
import tensorflow as tf
import utils
from core import Play, MyModel
import log as logging
import constants
import trading_data as tdata
constants.LOG_DIR = "./log/plays"
writer = utils.get_tf_summary_writer("./log/plays")
sess = utils.get_session()
LOG = logging.getLogger(__name__)
epochs = constants.EPOCHS
EPOCHS = constants.EPOCHS
points = constants.POINTS
def fit(inputs, outputs, units, activation, width, true_weight, loss='mse', mu=0, sigma=0.01, loss_file_name="./tmp/trainF-loss.csv", nb_plays=1, learning_rate=0.1, weights_fname="model.h5"):
# mu = float(mu)
# sigma = float(sigma)
# fname = constants.FNAME_FORMAT['mc'].format(mu=mu, sigma=sigma, points=inputs.shape[-1])
# try:
# B, _ = tdata.DatasetLoader.load_data(fname)
# except:
B = tdata.DatasetGenerator.systhesis_markov_chain_generator(inputs.shape[-1], mu, sigma)
# fname = constants.FNAME_FORMAT['mc'].format(points=inputs.shape[-1], mu=mu, sigma=sigma)
# tdata.DatasetSaver.save_data(B, B, fname)
units = units
# batch_size = 1
input_dim = 10
timestep = 900
epochs = 2500
# epochs = EPOCHS // batch_size
# steps_per_epoch = batch_size
steps_per_epoch = 1
train_inputs, train_outputs = inputs, outputs
import time
start = time.time()
agent = MyModel(# batch_size=batch_size,
timestep=timestep,
input_dim=input_dim,
units=units,
activation="tanh",
nb_plays=nb_plays)
agent.load_weights(weights_fname)
agent.fit(inputs, outputs, verbose=1, epochs=epochs, steps_per_epoch=steps_per_epoch, loss_file_name=loss_file_name,
learning_rate=learning_rate)
end = time.time()
LOG.debug("time cost: {}s".format(end-start))
predictions = agent.predict(inputs)
agent.save_weights(weights_fname)
prices = agent.predict(B)
B = B.reshape(-1)
prices = prices.reshape(-1)
return B, prices, predictions
if __name__ == "__main__":
methods = constants.METHODS
weights = constants.WEIGHTS
widths = constants.WIDTHS
_units = constants.UNITS
parser = argparse.ArgumentParser()
parser.add_argument("--loss", dest="loss",
required=False)
parser.add_argument("--mu", dest="mu",
required=False,
type=float)
parser.add_argument("--sigma", dest="sigma",
required=False,
type=float)
parser.add_argument("--units", dest="units",
required=False,
type=int)
argv = parser.parse_args(sys.argv[1:])
learning_rate = 0.01
# loss_name = argv.loss
loss_name = 'mse'
mu = 0
# sigma = 0.01
sigma = 2
nb_plays = 20
nb_plays_ = 20
units = 20
points = 1000
state = 0
activation = "tanh"
for method in methods:
for weight in weights:
for width in widths:
LOG.debug("Processing method: {}, weight: {}, width: {}, units: {}, nb_plays: {}, mu: {}, sigma: {}, points: {}, state: {}".format(method, weight, width, units, nb_plays, mu, sigma, points, state))
# fname = constants.FNAME_FORMAT["models_noise"].format(method=method,
# weight=weight,
# width=width,
# nb_plays=nb_plays,
# units=units,
# mu=mu,
# sigma=sigma,
# points=points)
# fname = constants.FNAME_FORMAT['F_interp'].format(method=method,
# weight=weight,
# width=width,
# nb_plays=nb_plays,
# units=units,
# points=points,
# mu=mu,
# sigma=sigma,
# nb_plays_=nb_plays_,
# batch_size=1,
# state=state,
# loss=loss_name)
interp = 10
fname = constants.FNAME_FORMAT["models_nb_plays_noise_interp"].format(method=method,
weight=weight,
width=width,
nb_plays=nb_plays,
units=units,
points=points,
mu=mu,
sigma=sigma,
interp=interp)
inputs, outputs_ = tdata.DatasetLoader.load_data(fname)
# inputs, outputs_ = outputs_, inputs # F neural network
inputs, outputs_ = outputs_[:9000], inputs[:9000]
if True:
loss_file_name = constants.FNAME_FORMAT['F_interp_loss_history'].format(method=method,
weight=weight,
width=width,
nb_plays=nb_plays,
units=units,
mu=mu,
sigma=sigma,
points=points,
loss=loss_name,
nb_plays_=nb_plays_,
batch_size=1,
state=state)
weights_fname = constants.FNAME_FORMAT['F_interp_saved_weights'].format(method=method,
weight=weight,
width=width,
nb_plays=nb_plays,
units=units,
mu=mu,
sigma=sigma,
points=points,
loss=loss_name,
nb_plays_=nb_plays_,
batch_size=1,
state=state)
B, prices, predictions = fit(inputs=inputs,
outputs=outputs_,
units=units,
activation=activation,
width=width,
true_weight=weight,
loss=loss_name,
mu=mu,
sigma=sigma,
loss_file_name=loss_file_name,
nb_plays=nb_plays_,
learning_rate=learning_rate,
weights_fname=weights_fname)
fname = constants.FNAME_FORMAT['F_interp'].format(method=method,
weight=weight,
width=width,
nb_plays=nb_plays,
units=units,
points=points,
mu=mu,
sigma=sigma,
nb_plays_=nb_plays_,
batch_size=1,
state=state,
loss=loss_name)
tdata.DatasetSaver.save_data(inputs, outputs_, fname)
fname = constants.FNAME_FORMAT['F_interp_predictions'].format(method=method,
weight=weight,
width=width,
nb_plays=nb_plays,
units=units,
mu=mu,
sigma=sigma,
points=points,
loss=loss_name,
nb_plays_=nb_plays_,
batch_size=1,
state=state)
tdata.DatasetSaver.save_data(inputs, predictions, fname)
# fname = constants.FNAME_FORMAT['F_predictions'].format(method=method,
# weight=weight,
# width=width,
# nb_plays=nb_plays,
# units=units,
# mu=mu,
# sigma=sigma,
# points=points,
# loss=loss_name,
# nb_plays_=nb_plays_,
# batch_size=1,
# state=state)
# tdata.DatasetSaver.save_data(B, prices, fname)