Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
Co-authored-by: Gustavo Viera López <[email protected]>
  • Loading branch information
jmorgadov and gvieralopez committed Jun 8, 2023
1 parent 2041f45 commit 57f5202
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/example_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
num_heads=4,
num_transformer_blocks=4,
optimizer=keras.optimizers.Adam(learning_rate=1e-4),
random_state=SEED,
)

# Split into train and test sets
Expand Down
22 changes: 16 additions & 6 deletions examples/example_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
LSTMModel,
RandomForestModel,
SVMModel,
TransformerModel, # TODO: Falta XGBoost
TransformerModel,
XGBoostModel,
)

SEED = 0 # TODO: Use this for reproducibility
SEED = 0 # Random seed for reproducibility

dataset = Dataset.mnist_stroke()
train, test = dataset.cut(60_000)
dataset = Dataset.uci_characters()
train, test = dataset.split(.8, random_state=SEED)

featurizer = featurizers.UniversalFeaturizer()
vectorized_models = [
Expand All @@ -24,6 +25,7 @@
bootstrap=False,
warm_start=True,
n_jobs=6,
random_state=SEED,
),
KNeighborsModel(
featurizer=featurizer,
Expand All @@ -32,9 +34,15 @@
DecisionTreeModel(
featurizer=featurizer,
max_depth=7,
random_state=SEED,
),
SVMModel(
featurizer=featurizer,
random_state=SEED,
),
XGBoostModel(
featurizer=featurizer,
random_state=SEED,
),
]

Expand All @@ -44,12 +52,14 @@
num_heads=4,
num_transformer_blocks=4,
optimizer=keras.optimizers.Adam(learning_rate=1e-4),
random_state=SEED,
)

lstm = LSTMModel(
loss="sparse_categorical_crossentropy",
optimizer="rmsprop",
metrics=["accuracy"],
random_state=SEED,
)

# Train and evaluate vectorized models
Expand All @@ -61,7 +71,7 @@

# Train and evaluate LSTM model
checkpoint = keras.callbacks.ModelCheckpoint(
"partially_trained_model_lstm_mnist_stroke.h5",
f"partially_trained_model_lstm_{dataset.name}.h5",
monitor="loss",
verbose=1,
save_best_only=True,
Expand All @@ -73,7 +83,7 @@

# Train and evaluate Transformer model
checkpoint = keras.callbacks.ModelCheckpoint(
"partially_trained_model_transformer_mnist_stroke.h5",
f"partially_trained_model_transformer_{dataset.name}.h5",
monitor="loss",
verbose=1,
save_best_only=True,
Expand Down
5 changes: 4 additions & 1 deletion examples/example_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
# Split the dataset into train and test
train, test = dataset.filter(
lambda traj, _: len(traj) >= 5 and traj.r.delta.norm.sum() > 0
).split(train_size=0.7, random_state=SEED)
).split(
train_size=0.7,
random_state=SEED,
)

# Select the desired features to be extracted from the trajectories
featurizer = featurizers.UniversalFeaturizer()
Expand Down
12 changes: 12 additions & 0 deletions pactus/models/lstm_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import datetime
import logging
import time
from pathlib import Path
from typing import Any, List, Tuple, Union

import numpy as np
import tensorflow as tf
from sklearn.preprocessing import LabelEncoder
from tensorflow import keras
from yupi import Trajectory
Expand Down Expand Up @@ -30,6 +33,7 @@ def __init__(
loss="sparse_categorical_crossentropy",
optimizer="rmsprop",
metrics=None,
random_state: Union[int, None] = None,
**kwargs,
):
super().__init__(NAME)
Expand All @@ -38,6 +42,7 @@ def __init__(
self.dataset: Union[Dataset, None] = None
self.model: keras.Secuential
self.max_len = 0
self.random_state = random_state
metrics = ["accuracy"] if metrics is None else metrics
self.units = [128, 64] if units is None else units
kwargs.update(dict(loss=loss, optimizer=optimizer, metrics=metrics))
Expand Down Expand Up @@ -111,6 +116,13 @@ def train(
callbacks: Union[list, None] = None,
checkpoint: Union[keras.callbacks.ModelCheckpoint, None] = None,
):
if self.random_state is not None:
tf.keras.utils.set_random_seed(self.random_state)
logging.warning(
f"Custom seed provided for {self.name} model. This "
"calls 'tf.keras.utils.set_random_seed' which sets a global "
"random state on python, numpy and tensorflow."
)
if cross_validation != 0:
logging.warning("Cross validation is not supported yet for lstm")
self.set_summary(epochs=epochs, validation_split=validation_split)
Expand Down
13 changes: 11 additions & 2 deletions pactus/models/transformer_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, List, Tuple, Union

import numpy as np
import tensorflow as tf
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from tensorflow import keras
Expand Down Expand Up @@ -39,9 +40,9 @@ def __init__(
max_traj_len: int = -1,
skip_long_trajs: bool = False,
mask_value=cfg.MASK_VALUE,
name=NAME,
random_state: Union[int, None] = None,
):
super().__init__(name)
super().__init__(NAME)
self.head_size = head_size
self.num_heads = num_heads
self.ff_dim = ff_dim
Expand All @@ -59,6 +60,7 @@ def __init__(
self.encoder: Union[LabelEncoder, None] = None
self.labels: Union[List[Any], None] = None
self.original_data: Union[Data, None] = None
self.random_state: Union[int, None] = random_state
self.set_summary(
head_size=self.head_size,
num_heads=self.num_heads,
Expand All @@ -85,6 +87,13 @@ def train(
callbacks: Union[list, None] = None,
checkpoint: Union[keras.callbacks.ModelCheckpoint, None] = None,
):
if self.random_state is not None:
tf.keras.utils.set_random_seed(self.random_state)
logging.warning(
f"Custom seed provided for {self.name} model. This "
"calls 'tf.keras.utils.set_random_seed' which sets a global "
"random state on python, numpy and tensorflow."
)
self.set_summary(
cross_validation=cross_validation,
epochs=epochs,
Expand Down

0 comments on commit 57f5202

Please sign in to comment.