Skip to content

Commit 107bb4d

Browse files
Merge pull request #230 from cognizant-ai-labs/model_ensembles
Model ensembles
2 parents cd1efc3 + d505d2a commit 107bb4d

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

covid_xprize/examples/predictors/conditional_lstm/conditional_xprize_predictor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def train(self,
5151
nb_training_geos: int = NB_TRAINING_DAYS,
5252
nb_testing_geos: int = NB_TESTING_GEOS,
5353
nb_trials: int = NUM_TRIALS,
54-
nb_epochs: int = NUM_EPOCHS,) -> Union[Model, tuple[Model, dict]]:
54+
nb_epochs: int = NUM_EPOCHS,
55+
return_all_trials: bool = False) -> Union[Model, tuple[Model, dict]]:
5556
best_model, results_df = train_predictor(
5657
training_data=self.df,
5758
nb_lookback_days=NB_LOOKBACK_DAYS,
@@ -62,6 +63,7 @@ def train(self,
6263
nb_trials=nb_trials,
6364
nb_epochs=nb_epochs,
6465
lstm_size=LSTM_SIZE,
66+
return_all_trials=return_all_trials
6567
)
6668
if return_results:
6769
return best_model, results_df

covid_xprize/examples/predictors/conditional_lstm/train_predictor.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def train_predictor(training_data: pd.DataFrame,
4040
nb_trials: int,
4141
nb_epochs: int,
4242
lstm_size: int,
43+
return_all_trials: bool = False,
4344
verbose = False) -> tuple[Model, pd.DataFrame]:
4445
"""Trains a prediction model using the given hyperparameter arguments.
4546
:param nb_lookback_days: This option is not fully implemented yet. Completing implementation
@@ -65,6 +66,7 @@ def train_predictor(training_data: pd.DataFrame,
6566
:param context_column: Which column in the data df to use as context and outcome.
6667
:param arch: Which predictor architecture to use.
6768
Current options are 'conditional' and 'independent'.
69+
:param return_all_trials: If set to True, then this function returns all trials as a list of trained models.
6870
:param verbose: Verbosity level for model.fit() when training the predictor.
6971
:returns: (best_model, results_df)
7072
"""
@@ -181,10 +183,13 @@ def train_predictor(training_data: pd.DataFrame,
181183
'test_loss': test_losses,
182184
'test_case_mae': test_case_maes})
183185

184-
# Select best model
185-
print("Best test case mae:", np.min(test_case_maes))
186-
best_model = models[np.argmin(test_case_maes)]
187-
return best_model, results_df
186+
if return_all_trials:
187+
return models, results_df
188+
else:
189+
# Select best model
190+
print("Best test case mae:", np.min(test_case_maes))
191+
best_model = models[np.argmin(test_case_maes)]
192+
return best_model, results_df
188193

189194

190195
# Shuffling data prior to train/val split

covid_xprize/examples/predictors/lstm/xprize_predictor.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,12 @@ def _convert_ratios_to_total_cases(self,
214214
def _smooth_case_list(case_list, window):
215215
return pd.Series(case_list).rolling(window).mean().to_numpy()
216216

217-
def train(self, num_trials=NUM_TRIALS, num_epochs=NUM_EPOCHS):
217+
def train(self, num_trials=NUM_TRIALS, num_epochs=NUM_EPOCHS, return_all_trials=False):
218+
"""Trains the weights of the predictor model on a prediction loss.
219+
:param num_trials: The number of LSTM models to train. The top performer is selected.
220+
:param num_epochs: The number of iterations through the training data performed.
221+
:param return_all_trials: If set to True, then this function returns all trials as a list.
222+
"""
218223
print("Creating numpy arrays for Keras for each country...")
219224
geos = self._most_affected_geos(self.df, MAX_NB_COUNTRIES, NB_LOOKBACK_DAYS)
220225
country_samples = create_country_samples(self.df, geos, CONTEXT_COLUMN, NB_TEST_DAYS, NB_LOOKBACK_DAYS)
@@ -277,6 +282,10 @@ def train(self, num_trials=NUM_TRIALS, num_epochs=NUM_EPOCHS):
277282
print('Val Loss:', val_loss)
278283
print('Test Loss:', test_loss)
279284

285+
if return_all_trials:
286+
# Shortcut to avoid model evaluation & winner selection.
287+
return models
288+
280289
# Gather test info
281290
country_indeps = []
282291
country_predss = []

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ numpy==1.24.2
55
notebook==6.5.3
66
scikit-learn==1.2.2
77
scipy==1.10.1
8-
tensorflow==2.11.1
9-
keras==2.11.0
8+
tensorflow==2.13.0
9+
keras==2.13.1
1010
neat-python==0.92
1111
h5py==3.8.0
1212

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
from setuptools import setup, find_packages
99

10-
LIBRARY_VERSION = '2.0.2'
10+
LIBRARY_VERSION = '2.0.3'
1111

1212
CURRENT_PYTHON = sys.version_info[:2]
1313
REQUIRED_PYTHON = (3, 10)
@@ -61,14 +61,14 @@ def read(fname):
6161
]
6262
},
6363
install_requires=[
64-
'keras==2.11.0',
6564
'neat-python==0.92',
6665
'numpy==1.24.2',
6766
'pandas==1.5.3',
6867
'scikit-learn==1.2.2',
6968
'scipy==1.10.1',
7069
'setuptools==67.6.0',
71-
'tensorflow==2.11.1',
70+
'tensorflow==2.13.0',
71+
'keras==2.13.1',
7272
'h5py==3.8.0'
7373
],
7474
description='Contains sample code and notebooks '

0 commit comments

Comments
 (0)