-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurrent_nn_pred_matches_manager.py
190 lines (171 loc) · 12.2 KB
/
recurrent_nn_pred_matches_manager.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
import datetime
import tensorflow as tf
import keras_tuner as kt
import numpy as np
from matplotlib import pyplot as plt
from tensorflow import keras
from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.python.keras.regularizers import l2
from constants import saved_model_weights_base_path, saved_model_based_path, PredMatchesStrategy
from nn_manager.common import eval_model_after_learning, plot_metric, save_model, eval_model_after_learning_within_threshold, plot_many_metrics
from nn_manager.custom_bayesian_tuner import CustomBayesianSearch
from nn_manager.metrics import categorical_crossentropy_with_bets, categorical_acc_with_bets, odds_profit_with_biggest_gap_over_threshold, \
get_all_profit_metrics_for_pred_matches, pred_matches_precision, pred_matches_how_many_no_bets
from nn_manager.neural_network_manager import NeuralNetworkManager
class RecurrentNNPredictingMatchesManager(NeuralNetworkManager):
def __init__(self, train_set, val_set, should_hyper_tune, test_set, **kwargs):
self.best_params = {
'confidence_threshold': 0.065,
'dropout_rate': 0.65,
'gru_reccurent_regularization_factor': 0.00031378,
'gru_regularization_factor': 0.01,
'learning_rate': 0.004,
'number_of_addit_hidden_layers': 2,
'number_of_gru_units': 8,
'number_of_neurons_0_layer': 16,
'number_of_neurons_1_layer': 32,
'recurrent_type': 'GRU',
'regularization_factor': 0.00219613,
'use_bn_for_input': True,
'use_bn_for_rest': True
}
self.best_params.update(kwargs)
super().__init__(train_set, val_set, should_hyper_tune, test_set)
def create_model(self, hp: kt.HyperParameters = None):
factor = self.best_params["regularization_factor"] if not self.should_hyper_tune else hp.Float('regularization_factor', 0, 1e-2, step=1e-8)
gru_regularization_factor = self.best_params["gru_regularization_factor"] if not self.should_hyper_tune else \
hp.Float('gru_regularization_factor', 0, 1e-2, step=1e-8)
recurrent_regulizer = self.best_params["gru_reccurent_regularization_factor"] if not self.should_hyper_tune else \
hp.Float('gru_reccurent_regularization_factor', 0, 1e-3, step=1e-8)
number_of_gru_units = self.best_params["number_of_gru_units"] if not self.should_hyper_tune else hp.Choice('number_of_gru_units', [1, 2, 4, 8, 16])
first_hidden_units = self.best_params["number_of_neurons_0_layer"] if not self.should_hyper_tune else hp.Choice(
'number_of_neurons_0_layer', [8, 16, 32, 64, 128, 256, 512])
max_layers_quantity = 4
n_hidden_layers = self.best_params["number_of_addit_hidden_layers"] if not self.should_hyper_tune else hp.Int('number_of_addit_hidden_layers', 1,
max_layers_quantity)
learning_rate = self.best_params["learning_rate"] if not self.should_hyper_tune else hp.Float('learning_rate', 1e-5, 3e-3, step=1e-5)
confidence_threshold = self.best_params["confidence_threshold"] if not self.should_hyper_tune else hp.Float('confidence_threshold', 0.005, 0.15,
step=0.005)
dropout_rate = self.best_params["dropout_rate"] if not self.should_hyper_tune else hp.Float('dropout_rate', 0, 0.65, step=0.025)
recurrent_type = 'SimpleRNN' if not self.should_hyper_tune else hp.Choice('recurrent_type', ['SimpleRNN', 'GRU', 'LSTM'])
recurrent_type_callable = getattr(tf.keras.layers, recurrent_type)
use_bn_for_input = self.best_params["use_bn_for_input"] if not self.should_hyper_tune else hp.Boolean('use_bn_for_input')
use_bn_for_rest = self.best_params["use_bn_for_rest"] if not self.should_hyper_tune else hp.Boolean('use_bn_for_rest')
home_input = tf.keras.layers.Input((self.x_train[0].shape[1], self.x_train[0].shape[2],))
home_rnn = recurrent_type_callable(number_of_gru_units,
kernel_regularizer=l2(gru_regularization_factor),
bias_regularizer=l2(gru_regularization_factor),
recurrent_regularizer=l2(recurrent_regulizer))(home_input)
away_input = tf.keras.layers.Input((self.x_train[1].shape[1], self.x_train[1].shape[2],))
away_model = recurrent_type_callable(number_of_gru_units,
kernel_regularizer=l2(gru_regularization_factor),
bias_regularizer=l2(gru_regularization_factor),
recurrent_regularizer=l2(recurrent_regulizer))(away_input)
rest_of_input = tf.keras.layers.Input((self.x_train[2].shape[1],))
all_merged = tf.keras.layers.Concatenate()([
home_rnn,
away_model,
rest_of_input
])
if use_bn_for_input:
main_hidden = keras.layers.BatchNormalization()(all_merged)
main_hidden = keras.layers.Dropout(dropout_rate)(main_hidden)
else:
main_hidden = keras.layers.Dropout(dropout_rate)(all_merged)
main_hidden = keras.layers.Dense(first_hidden_units, activation='relu',
kernel_regularizer=l2(factor),
bias_regularizer=l2(factor),
kernel_initializer=tf.keras.initializers.he_normal())(main_hidden)
for i in range(1, n_hidden_layers):
neurons_quantity = self.best_params[f"number_of_neurons_{i}_layer"] if not self.should_hyper_tune else hp.Choice(f'number_of_neurons_{i}_layer',
[4, 6, 8, 16, 32, 64, 128, 256],
parent_name='number_of_addit_hidden_layers',
parent_values=list(
range(i + 1,
max_layers_quantity + 1))
)
if use_bn_for_rest:
main_hidden = keras.layers.BatchNormalization()(main_hidden)
main_hidden = keras.layers.Dropout(dropout_rate)(main_hidden)
main_hidden = keras.layers.Dense(neurons_quantity, activation='relu',
kernel_regularizer=l2(factor),
bias_regularizer=l2(factor),
kernel_initializer=tf.keras.initializers.he_normal())(main_hidden)
main_hidden = keras.layers.Dense(3, activation='softmax')(main_hidden)
main_model = keras.models.Model(inputs=[home_input, away_input, rest_of_input], outputs=main_hidden)
opt = keras.optimizers.Adam(learning_rate=learning_rate)
main_model.compile(loss=categorical_crossentropy_with_bets,
optimizer=opt,
# get_all_profit_metrics_for_pred_matches(confidence_threshold)
# categorical_acc_with_bets,
metrics=[odds_profit_with_biggest_gap_over_threshold(confidence_threshold),
pred_matches_how_many_no_bets(confidence_threshold), pred_matches_precision(confidence_threshold)])
return main_model
def perform_model_learning(self, verbose=True):
self.history = self.model.fit(x=[self.x_train[0], self.x_train[1], self.x_train[2]], y=self.y_train, epochs=1000,
batch_size=256,
verbose=1 if verbose is True else 0,
shuffle=False,
validation_data=([self.x_val[0], self.x_val[1], self.x_val[2]], self.y_val),
validation_batch_size=self.y_val.shape[0],
callbacks=[
EarlyStopping(patience=150, monitor='val_loss', mode='min',
verbose=1 if verbose is True else 0
),
ModelCheckpoint(self.get_path_for_saving_weights(), save_best_only=True, save_weights_only=True,
monitor='val_profit', mode='max', verbose=1 if verbose is True else 0)]
)
self.model.load_weights(self.get_path_for_saving_weights())
def get_best_metric_value(self, metric_name):
return max(self.history.history[metric_name])
def get_best_strategies_value(self):
strategy_metrics = {}
for strategy in PredMatchesStrategy:
metric_name = f"val_{strategy.value}"
strategy_metrics.update({strategy: self.get_best_metric_value(metric_name)})
# print(f'{strategy.value}: {self.get_best_metric_value(metric_name)}')
# plot_many_metrics(self.history, [en.value for en in PredMatchesStrategy], True, True)
return strategy_metrics
def plot_confidence_threshold(self):
val_predictions = self.model.predict(self.x_val)
val_true = self.y_val
if self.x_test is not None:
test_predictions = self.model.predict(self.x_test)
test_true = self.y_test
confidence_values = np.linspace(0.001, 0.2, 2000)
val_profit = []
test_profit = []
for confidence in confidence_values:
val_profit.append(odds_profit_with_biggest_gap_over_threshold(confidence)(val_true, val_predictions).numpy())
if self.x_test is not None:
test_profit.append(odds_profit_with_biggest_gap_over_threshold(confidence)(test_true, test_predictions).numpy())
plt.plot(confidence_values.tolist(), val_profit)
if self.x_test is not None:
plt.plot(confidence_values.tolist(), test_profit)
plt.xlabel("Confidence threshold")
plt.ylabel("Profit")
if self.x_test is not None:
plt.legend(["Val Profit", 'Test Profit'])
else:
plt.legend(["Val Profit"])
plt.axhline(y=0, color='r')
plt.grid()
plt.savefig(f'./confidence_threshold_check/confidence_threshold{datetime.datetime.now().timestamp()}.png', dpi=900)
def hyper_tune_model(self):
tuner = CustomBayesianSearch(self.create_model,
objective=kt.Objective('val_profit', 'max'),
max_trials=300,
executions_per_trial=5,
num_initial_points=150,
directory='.\\hypertuning',
project_name=self.__class__.__name__,
overwrite=False,
beta=3.0)
tuner.search(x=[self.x_train[0], self.x_train[1], self.x_train[2]], y=self.y_train, epochs=1000, batch_size=256,
validation_batch_size=self.y_val.shape[0], shuffle=True,
verbose=2, callbacks=[EarlyStopping(patience=100, monitor='val_loss', mode='min', verbose=1, min_delta=0.0005)],
validation_data=([self.x_val[0], self.x_val[1], self.x_val[2]], self.y_val))
self.print_summary_after_tuning(tuner, 10)
return tuner
def evaluate_model(self, should_plot=True, should_print_train=True, hyperparams=None):
self.evaluate_model_with_threshold(should_plot, should_print_train, hyperparams)