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
/
play_noise_test.py
120 lines (94 loc) · 4.92 KB
/
play_noise_test.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
import sys
import argparse
import time
import numpy as np
import tensorflow as tf
import utils
from core import Play
import log as logging
import constants
import trading_data as tdata
writer = utils.get_tf_summary_writer("./log/plays")
sess = utils.get_session()
LOG = logging.getLogger(__name__)
epochs = constants.EPOCHS
EPOCHS = constants.EPOCHS
def fit(inputs, outputs, units, activation, width, true_weight, loss='mse', loss_file_name="./tmp/play_loss_history.csv"):
units = units
batch_size = 10
epochs = EPOCHS // batch_size
# epochs = 1
steps_per_epoch = batch_size
total_timesteps = inputs.shape[0]
train_timesteps = int(total_timesteps * 0.5)
train_inputs, train_outputs = inputs[:train_timesteps], outputs[:train_timesteps]
test_inputs, test_outputs = inputs[train_timesteps:], outputs[train_timesteps:]
import time
start = time.time()
play = Play(batch_size=batch_size,
units=units,
activation="tanh",
network_type=constants.NetworkType.PLAY,
loss=loss,
debug=False)
if loss == 'mse':
play.fit(train_inputs, train_outputs, verbose=1, epochs=epochs, steps_per_epoch=steps_per_epoch)
train_loss, metrics = play.evaluate(train_inputs, train_outputs, steps_per_epoch=steps_per_epoch)
test_loss, metrics = play.evaluate(test_inputs, test_outputs, steps_per_epoch=steps_per_epoch)
train_predictions = play.predict(train_inputs, steps_per_epoch=1)
test_predictions = play.predict(test_inputs, steps_per_epoch=1)
train_mu = train_sigma = test_mu = test_sigma = -1
elif loss == 'mle':
mu = 0
sigma = 0.0001
play.fit2(train_inputs, mu, sigma, verbose=1, epochs=epochs, steps_per_epoch=steps_per_epoch, loss_file_name=loss_file_name)
train_predictions, train_mu, train_sigma = play.predict2(train_inputs, steps_per_epoch=1)
test_predictions, test_mu, test_sigma = play.predict2(test_inputs, steps_per_epoch=1)
train_loss = ((train_outputs - train_predictions) ** 2).mean()
test_loss = ((test_outputs - test_predictions) ** 2).mean()
train_loss = float(train_loss)
test_loss = float(test_loss)
end = time.time()
LOG.debug("time cost: {}s".format(end-start))
LOG.debug("number of layer is: {}".format(play.number_of_layers))
LOG.debug("weight: {}".format(play.weight))
train_predictions = train_predictions.reshape(-1)
test_predictions = test_predictions.reshape(-1)
predictions = np.hstack([train_predictions, test_predictions])
if np.any(np.isnan(predictions)):
predictions = np.zeros(predictions.shape)
train_mu = train_sigma = test_mu = test_sigma = -1
loss = [train_loss, test_loss, train_mu, test_mu, train_sigma, test_sigma]
return predictions, loss
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=True)
argv = parser.parse_args(sys.argv[1:])
loss_name = argv.loss
activation = "tanh"
mu = 1
sigma = 0.01
for method in methods:
for weight in weights:
for width in widths:
LOG.debug("Processing method: {}, weight: {}, width: {}".format(method, weight, width))
fname = constants.FNAME_FORMAT["plays_noise"].format(method=method, weight=weight, width=width, mu=mu, sigma=sigma)
inputs, outputs_ = tdata.DatasetLoader.load_data(fname)
# inputs, outputs_ = inputs[:40], outputs_[:40]
# increase *units* in order to increase the capacity of the model
for units in _units:
loss_history_file = constants.FNAME_FORMAT["plays_noise_loss_history"].format(method=method, weight=weight,
width=width, activation=activation, units=units, mu=mu, sigma=sigma)
predictions, loss = fit(inputs, outputs_, units, activation, width, weight, loss_name, loss_history_file)
fname = constants.FNAME_FORMAT["plays_noise_loss"].format(method=method, weight=weight,
width=width, activation=activation, units=units, mu=mu, sigma=sigma)
tdata.DatasetSaver.save_loss({"loss": loss}, fname)
fname = constants.FNAME_FORMAT["plays_noise_predictions"].format(method=method, weight=weight,
width=width, activation=activation, units=units,
mu=mu, sigma=sigma)
tdata.DatasetSaver.save_data(inputs, predictions, fname)