ray/doc/source/tune/doc_code/keras_hyperopt.py
Max Pumperla 372c620f58
[docs] Tune overhaul part II (#22656)
Co-authored-by: Antoni Baum <antoni.baum@protonmail.com>
2022-02-26 23:07:34 -08:00

31 lines
905 B
Python

# flake8: noqa
accuracy = 42
# __keras_hyperopt_start__
from ray import tune
from ray.tune.suggest.hyperopt import HyperOptSearch
import keras
# 1. Wrap a Keras model in an objective function.
def objective(config):
model = keras.models.Sequential()
model.add(keras.layers.Dense(784, activation=config["activation"]))
model.add(keras.layers.Dense(10, activation="softmax"))
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# model.fit(...)
# loss, accuracy = model.evaluate(...)
return {"accuracy": accuracy}
# 2. Define a search space and initialize the search algorithm.
search_space = {"activation": tune.choice(["relu", "tanh"])}
algo = HyperOptSearch()
# 3. Start a Tune run that maximizes accuracy.
analysis = tune.run(
objective, search_alg=algo, config=search_space, metric="accuracy", mode="max"
)
# __keras_hyperopt_end__