-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoder workflow moved to modularized files for better organization a…
…nd accessibility
- Loading branch information
Dawith
committed
Oct 28, 2025
1 parent
6403973
commit b3bfa1b
Showing
4 changed files
with
182 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,89 @@ | ||
| #-*- coding: utf-8 -*- | ||
|
|
||
| import time | ||
|
|
||
| import os | ||
| import keras | ||
|
|
||
| from model.model import CompoundModel | ||
| from visualize.visualize import confusion_matrix | ||
| from visualize.plot import roc_plot | ||
|
|
||
| def build_encoder(params, input_shape, n_classes): | ||
| model = CompoundModel( | ||
| input_shape, | ||
| params["head_size"], | ||
| params["num_heads"], | ||
| params["ff_dim"], | ||
| params["num_transformer_blocks"], | ||
| params["mlp_units"], | ||
| n_classes, | ||
| dropout=params["dropout"], | ||
| mlp_dropout=params["mlp_dropout"] | ||
| ) | ||
|
|
||
| model.compile( | ||
| optimizer=keras.optimizers.Adam(learning_rate=4e-4), | ||
| loss="categorical_crossentropy", | ||
| metrics=["categorical_accuracy", "categorical_accuracy"] | ||
| ) | ||
| if params["log_level"] == 1: | ||
| model.summary() | ||
|
|
||
| return model | ||
|
|
||
| def train_encoder(params, model, train_set, validation_set): | ||
| start = time.time() | ||
| model.fit( | ||
| x=train_set[0], y=train_set[1], | ||
| validation_data=(validation_set[0], validation_set[1]), | ||
| batch_size=params["batch_size"], | ||
| epochs=params["epochs"], | ||
| verbose=params["log_level"] | ||
| ) | ||
| end = time.time() | ||
| print("Training time: ", end - start) | ||
| return model | ||
|
|
||
| def test_encoder(params, model, test_set, categories, keys): | ||
| # Test model performance | ||
| test_loss, test_accuracy, _, _, _, _ = model.evaluate( | ||
| test_set[0], | ||
| test_set[1] | ||
| ) | ||
|
|
||
| test_predict = model.predict(test_set[0]) | ||
| print(f"Test loss: {test_loss}, test accuracy: {test_accuracy}") | ||
| return test_predict, test_loss, test_accuracy | ||
|
|
||
| def evaluate_encoder(params, test_predict, test_set, test_loss, test_accuracy, categories, keys): | ||
| for predict, groundtruth, key in zip(test_predict, test_set[1], keys): | ||
| confusion_matrix(predict, groundtruth, categories[key], key) | ||
| roc_plot(predict, groundtruth, key) | ||
| save_metric(params, test_loss, test_accuracy) | ||
|
|
||
| def save_metric(params, test_loss, test_accuracy): | ||
| """ | ||
| Save the hyperparameters and metric to csv | ||
| """ | ||
|
|
||
| metric = { | ||
| "head_size": params["head_size"], | ||
| "num_heads": params["num_heads"], | ||
| "ff_dim": params["ff_dim"], | ||
| "num_transformer_blocks": params["num_transformer_blocks"], | ||
| "mlp_units": params["mlp_units"][0], | ||
| "dropout": params["dropout"], | ||
| "mlp_dropout": params["mlp_dropout"], | ||
| "batch_size": params["batch_size"], | ||
| "epochs": params["epochs"], | ||
| "test_loss": test_loss, | ||
| "test_accuracy": test_accuracy | ||
| } | ||
| if not os.path.exists("/app/workdir/metrics.csv"): | ||
| with open("/app/workdir/metrics.csv", "w") as f: | ||
| f.write(",".join(metric.keys()) + "\n") | ||
| with open("/app/workdir/metrics.csv", "a") as f: | ||
| f.write(",".join([str(value) for value in metric.values()]) + "\n") | ||
|
|
||
| # EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.