Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
babenek committed Oct 1, 2024
1 parent 6f102c5 commit 1cbddfe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
42 changes: 15 additions & 27 deletions experiment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ def main(cred_data_location: str, jobs: int, use_tuner: bool = False) -> str:
mode="min",
restore_best_weights=True,
verbose=1)
tuner.search(x=[x_train_line, x_train_variable, x_train_value, x_train_features],
y=y_train,
epochs=3,
batch_size=batch_size,
callbacks=[search_early_stopping],
validation_data=([x_test_line, x_test_variable, x_test_value, x_test_features], y_test),
verbose=2,
)
tuner.search(
x=[x_train_line, x_train_variable, x_train_value, x_train_features],
y=y_train,
epochs=3,
batch_size=batch_size,
callbacks=[search_early_stopping],
validation_data=([x_test_line, x_test_variable, x_test_value, x_test_features], y_test),
verbose=2,
)
print("Best Hyperparameters:")
for k, v in tuner.get_best_hyperparameters()[0].values.items():
print(f"{k}: {v}")
Expand All @@ -175,8 +176,8 @@ def main(cred_data_location: str, jobs: int, use_tuner: bool = False) -> str:
batch_size=batch_size,
epochs=max_epochs,
verbose=2,
validation_data=([x_test_line, x_test_variable, x_test_value, x_test_features],
y_test),
validation_data=([x_test_line, x_test_variable, x_test_value,
x_test_features], y_test),
class_weight=class_weight,
callbacks=[early_stopping, model_checkpoint],
use_multiprocessing=True)
Expand All @@ -187,32 +188,23 @@ def main(cred_data_location: str, jobs: int, use_tuner: bool = False) -> str:
keras_model.save(model_file_name, include_optimizer=False)

print(f"Validate results on the train subset. Size: {len(y_train)} {np.mean(y_train):.4f}")
evaluate_model(thresholds,
keras_model,
[x_train_line, x_train_variable, x_train_value, x_train_features],
y_train)
evaluate_model(thresholds, keras_model, [x_train_line, x_train_variable, x_train_value, x_train_features], y_train)
del x_train_line
del x_train_variable
del x_train_value
del x_train_features
del y_train

print(f"Validate results on the test subset. Size: {len(y_test)} {np.mean(y_test):.4f}")
evaluate_model(thresholds,
keras_model,
[x_test_line, x_test_variable, x_test_value, x_test_features],
y_test)
evaluate_model(thresholds, keras_model, [x_test_line, x_test_variable, x_test_value, x_test_features], y_test)
del x_test_line
del x_test_variable
del x_test_value
del x_test_features
del y_test

print(f"Validate results on the full set. Size: {len(y_full)} {np.mean(y_full):.4f}")
evaluate_model(thresholds,
keras_model,
[x_full_line, x_full_variable, x_full_value, x_full_features],
y_full)
evaluate_model(thresholds, keras_model, [x_full_line, x_full_variable, x_full_value, x_full_features], y_full)
del x_full_line
del x_full_variable
del x_full_value
Expand Down Expand Up @@ -261,11 +253,7 @@ def main(cred_data_location: str, jobs: int, use_tuner: bool = False) -> str:
default=4,
dest="jobs",
metavar="POSITIVE_INT")
parser.add_argument("-t",
"--tuner",
help="use keras tuner",
dest="use_tuner",
action="store_true")
parser.add_argument("-t", "--tuner", help="use keras tuner", dest="use_tuner", action="store_true")
args = parser.parse_args()

fixed_seed = 42 # int(datetime.now().timestamp())
Expand Down
18 changes: 10 additions & 8 deletions experiment/src/ml_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
class MlModel(kt.HyperModel):
d_type = "float32"

def __init__(self,
line_shape: tuple,
variable_shape: tuple,
value_shape: tuple,
feature_shape: tuple,
):
def __init__(
self,
line_shape: tuple,
variable_shape: tuple,
value_shape: tuple,
feature_shape: tuple,
):
self.line_shape = line_shape
self.variable_shape = variable_shape
self.value_shape = value_shape
Expand All @@ -43,14 +44,15 @@ def build(self, hp=None) -> Model:
variable_input = Input(shape=(None, self.variable_shape[2]), name="variable_input", dtype=self.d_type)
variable_lstm = LSTM(units=self.variable_shape[1], dtype=self.d_type)
variable_bidirectional = Bidirectional(layer=variable_lstm, name="variable_bidirectional")
variable_lstm_branch = Dropout(dropout_variable, name="variable_dropout")(variable_bidirectional(variable_input))
variable_lstm_branch = Dropout(dropout_variable,
name="variable_dropout")(variable_bidirectional(variable_input))

value_input = Input(shape=(None, self.value_shape[2]), name="value_input", dtype=self.d_type)
value_lstm = LSTM(units=self.value_shape[1], dtype=self.d_type)
value_bidirectional = Bidirectional(layer=value_lstm, name="value_bidirectional")
value_lstm_branch = Dropout(dropout_value, name="value_dropout")(value_bidirectional(value_input))

feature_input = Input(shape=(self.feature_shape[1],), name="feature_input", dtype=self.d_type)
feature_input = Input(shape=(self.feature_shape[1], ), name="feature_input", dtype=self.d_type)

joined_features = Concatenate()([line_lstm_branch, variable_lstm_branch, value_lstm_branch, feature_input])

Expand Down

0 comments on commit 1cbddfe

Please sign in to comment.