Skip to content

Commit

Permalink
feat: added evaluate_model method for test data evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmekiska committed May 12, 2024
1 parent c07898f commit fe5ecb7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ activation function type

- chore!: changed from temp library keras_core to keras > 3.0.0
- removed python 3.8 support to accomodate tensorflow and keras dependiencies
- increased major to 3.0.0 to align with keras major
- increased major to 3.0.0 to align with keras major
- added evaluate_model method to test model performance on test data
4 changes: 4 additions & 0 deletions imbrium/predictors/multivarhybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback_board],
shuffle=False,
)
else:
self.details = self.model.fit(
Expand All @@ -715,6 +716,7 @@ def fit_model(
validation_split=validation_split,
epochs=epochs,
verbose=show_progress,
shuffle=False,
)

else:
Expand All @@ -733,6 +735,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback, callback_board],
shuffle=False,
)
else:
callback = EarlyStopping(**callback_setting)
Expand All @@ -743,6 +746,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback],
shuffle=False,
)
return self.details

Expand Down
4 changes: 4 additions & 0 deletions imbrium/predictors/multivarpure.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback_board],
shuffle=False,
)
else:
self.details = self.model.fit(
Expand All @@ -675,6 +676,7 @@ def fit_model(
validation_split=validation_split,
epochs=epochs,
verbose=show_progress,
shuffle=False,
)

else:
Expand All @@ -693,6 +695,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback, callback_board],
shuffle=False,
)
else:
callback = EarlyStopping(**callback_setting)
Expand All @@ -703,6 +706,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback],
shuffle=False,
)
return self.details

Expand Down
4 changes: 4 additions & 0 deletions imbrium/predictors/univarhybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback_board],
shuffle=False,
)
else:
self.details = self.model.fit(
Expand All @@ -687,6 +688,7 @@ def fit_model(
validation_split=validation_split,
epochs=epochs,
verbose=show_progress,
shuffle=False,
)

else:
Expand All @@ -705,6 +707,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback, callback_board],
shuffle=False,
)
else:
callback = EarlyStopping(**callback_setting)
Expand All @@ -715,6 +718,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback],
shuffle=False,
)
return self.details

Expand Down
32 changes: 32 additions & 0 deletions imbrium/predictors/univarpure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import os
from typing import Tuple

from keras.callbacks import EarlyStopping, TensorBoard
from keras.saving import load_model
Expand Down Expand Up @@ -38,9 +39,21 @@ def _model_intake_prep(self, steps_past: int, steps_future: int) -> None:
self.input_x, self.input_y = sequence_prep_standard_uni(
temp_data, steps_past, steps_future
)
self.input_x, self.input_x_test = self._train_test_split(self.input_x)
self.input_y, self.input_y_test = self._train_test_split(self.input_y)
else:
pass

def _train_test_split(self, data: array, test_size=0.2) -> Tuple[array, array]:
"""Splits the time series data into training and testing sets."""
n_samples = len(data)
n_test = int(test_size * n_samples)

train_data = data[:-n_test]
test_data = data[-n_test:]

return train_data, test_data

def set_model_id(self, name: str):
"""Setter method to change model id field.
Parameters:
Expand Down Expand Up @@ -155,6 +168,10 @@ def create_mlp(
(self.input_x.shape[0], self.input_x.shape[1])
)

self.input_x_test = self.input_x_test.reshape(
(self.input_x_test.shape[0], self.input_x_test.shape[1])
)

optimizer_obj = get_optimizer(optimizer, optimizer_args)

self.model = mlp(
Expand Down Expand Up @@ -663,6 +680,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback_board],
shuffle=False,
)
else:
self.details = self.model.fit(
Expand All @@ -671,6 +689,7 @@ def fit_model(
validation_split=validation_split,
epochs=epochs,
verbose=show_progress,
shuffle=False,
)

else:
Expand All @@ -689,6 +708,7 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback, callback_board],
shuffle=False,
)
else:
callback = EarlyStopping(**callback_setting)
Expand All @@ -699,9 +719,17 @@ def fit_model(
epochs=epochs,
verbose=show_progress,
callbacks=[callback],
shuffle=False,
)
return self.details

def evaluate_model(self):
self.evaluation_details = self.model.evaluate(
x=self.input_x_test, y=self.input_y_test
)

return self.evaluation_details

def model_blueprint(self):
"""Prints a summary of the models layer structure."""
self.model.summary()
Expand All @@ -710,6 +738,10 @@ def show_performance(self):
"""Returns performance details."""
return self.details

def show_evaluation(self):
"""Returns performance details on test data."""
return self.evaluation_details

def predict(self, data: array) -> array:
"""Takes in a sequence of values and outputs a forecast.
Parameters:
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
packages=find_packages(include=["imbrium", "imbrium.*"]),
install_requires=[
"setuptools >= 41.0.0",
#"tensorflow >=2.16.0, < 3.0.0",
"tf-nightly-cpu==2.16.0.dev20231130", # temporary solution for testing, waiting for release
"keras >=3.0.0, < 4.0.0",
"tensorflow>=2.16.0",
],
classifiers=[
"Programming Language :: Python :: 3.9",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_predictors/test_univarpure.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@

y = np.array([[3.422], [2.697], [2.992]])

shape_x = (494, 2, 1)
shape_y = (494, 3, 1)
shape_x = (396, 2, 1)
shape_y = (396, 3, 1)

model_id = "LSTM"
optimizer = "adam"
Expand Down

0 comments on commit fe5ecb7

Please sign in to comment.