I am tuning a Keras model with ray.tune. It basically looks as follows:
import tensorflow as tf
from ray import tune
from ray.tune.search.hyperopt import HyperOptSearch
from ray.tune.schedulers import MedianStoppingRule
# model structure
def build_model(config):
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation="relu", input_shape=(156,)),
tf.keras.layers.Dense(1, activation="relu")
])
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=config["learning_rate"]),
loss='mse',
metrics='mse',
)
return model
# trainable
def train_model(config):
# Build and compile model
model = build_model(config)
# Train the model
model.fit(x_train, y_train,
validation_data=(x_val, y_val),
epochs=10,
callbacks = [ray.tune.integration.keras.TuneReportCallback(metrics={'val_mse':'val_mse'})]
)
# define search space
space = {'learning_rate': tune.choice([0.02, 0.2])}
# Define experiment configuration
hyperopt = HyperOptSearch()
median_stopping = MedianStoppingRule(grace_period=2)
analysis = tune.Tuner(
tune.with_resources(
train_model,
resources={"cpu": 1, "gpu": 0}
),
param_space=space,
tune_config=tune.TuneConfig(
metric="val_mse",
mode="min",
search_alg=hyperopt,
scheduler=median_stopping,
num_samples=3,
),
run_config=air.RunConfig(
local_dir = 'PATH',
name= 'output',
),
)
# run
ray.init()
result = analysis.fit()
This creates an output folder that contains events.out.tfevents, params.json, params.pkl, progress.csv and results.json. However, no checkpoints are created. If I do: result.get_best_result().best_checkpoints, it returns an empty list.
I need to save the best performing model and work with it outside of the ray architecture. Without checkpoints, I am not sure how to do it.