|
| 1 | +# Saving and Loading an Ax Experiment to JSON |
| 2 | + |
| 3 | +Ax provides a convenient way to save and load experiments in JSON format, making it easy to store and transport experiment data. In this recipe, we will walk through the steps of saving an Ax experiment to JSON using the AxClient. |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +Saving an experiment to JSON is useful when you want to store the experiment data in a lightweight and transportable format. This can be particularly useful for users who prefer a simple storage solution or need to share experiment data with others. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +We assume that you are already familiar with using Ax for experimentation and have an AxClient instance set up. |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +Before we begin, make sure you have an AxClient instance configured for your experiment. |
| 16 | + |
| 17 | +```python |
| 18 | +from ax import AxClient |
| 19 | + |
| 20 | +client = AxClient() |
| 21 | +``` |
| 22 | + |
| 23 | +## Steps |
| 24 | + |
| 25 | +1. Get the experiment object from the AxClient |
| 26 | +2. Save the experiment to JSON using the `save_experiment` function |
| 27 | + |
| 28 | +### 1. Get the experiment object from the AxClient |
| 29 | + |
| 30 | +First, we need to get the experiment object from the AxClient. We can do this by accessing the `_experiment` attribute of the AxClient instance. |
| 31 | + |
| 32 | +```python |
| 33 | +experiment = client._experiment |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Save the experiment to JSON |
| 37 | + |
| 38 | +Next, we use the `save_experiment` function from the `ax.storage.json_store.save` module to save the experiment to JSON. We need to specify the filepath where we want to save the experiment. |
| 39 | + |
| 40 | +```python |
| 41 | +from ax.storage.json_store.save import save_experiment |
| 42 | + |
| 43 | +filepath = "experiments/experiment.json" |
| 44 | +save_experiment(experiment, filepath) |
| 45 | +``` |
| 46 | + |
| 47 | +This will serialize the experiment (including attached data) and save it to the specified file. |
| 48 | + |
| 49 | +## Updating the Experiment |
| 50 | + |
| 51 | +To update a JSON-backed experiment, simply re-save the experiment to the same file. |
| 52 | + |
| 53 | +## Loading the Experiment |
| 54 | + |
| 55 | +To load an experiment from JSON, use the `load_experiment` function from the `ax.storage.json_store.load` module and specify the filepath again. |
| 56 | + |
| 57 | +```python |
| 58 | +from ax.storage.json_store.load import load_experiment |
| 59 | + |
| 60 | +loaded_experiment = load_experiment(filepath) |
| 61 | +``` |
| 62 | + |
| 63 | +## Customizing the Serialization Process |
| 64 | + |
| 65 | +If you have custom metrics or runners that you want to ensure are saved to JSON properly, you can create a `RegistryBundle` that bundles together encoding and decoding logic for use in the save/load functions. |
| 66 | + |
| 67 | +```python |
| 68 | +from ax.storage.registry_bundle import RegistryBundle |
| 69 | + |
| 70 | +bundle = RegistryBundle( |
| 71 | + runner_clss={MyRunner: None}, |
| 72 | + metric_clss={MyMetric: None}, |
| 73 | +) |
| 74 | + |
| 75 | +filepath = "experiments/experiment.json" |
| 76 | +save_experiment(experiment, filepath, encoder_registry=bundle.encoder_registry) |
| 77 | +loaded_experiment = load_experiment(filepath, decoder_registry=bundle.decoder_registry) |
| 78 | +``` |
| 79 | + |
| 80 | +## Learn more |
| 81 | + |
| 82 | +Take a look at these other recipes to continue your learning: |
| 83 | + |
| 84 | +- [Saving, Loading, and Updating an Ax Experiment from SQLite](#) |
0 commit comments