|
| 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 and loading an Ax experiment to JSON using the Client. |
| 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 | +## Setup |
| 10 | + |
| 11 | +Before we begin you must instantiate the `Client` and configure it for your |
| 12 | +experiment. |
| 13 | + |
| 14 | +```python |
| 15 | +client = Client() |
| 16 | + |
| 17 | +client.configure_experiment(...) |
| 18 | +client.configure_optimization(...) |
| 19 | +``` |
| 20 | + |
| 21 | +## Steps |
| 22 | + |
| 23 | +In this recipe, we plan to save a a snapshot of an Ax Client to JSON, reload the client from the JSON file, and validate its contents. |
| 24 | + |
| 25 | +* **Saving to JSON** |
| 26 | + 1. Initialize a Client and configure it with an experiment |
| 27 | + 2. View the state of the client and its associated experiment via `summarize()` |
| 28 | + 3. Call save_to_json_file to save a snapshot of the Client to JSON |
| 29 | +* **Loading from JSON** |
| 30 | + 1. Call load_from_json_file to initialize a new Client |
| 31 | + 2. Validate the state of the Client via `summarize()` |
| 32 | + |
| 33 | +### Saving to JSON |
| 34 | +#### 1. Initialize a Client and configure it with an experiment |
| 35 | + |
| 36 | +Instantiate a `Client` and configure it for your experiment. |
| 37 | + |
| 38 | +```python |
| 39 | +client = Client() |
| 40 | + |
| 41 | +client.configure_experiment(...) |
| 42 | +client.configure_optimization(...) |
| 43 | +``` |
| 44 | + |
| 45 | +#### 2. View the state of the experiment via `summarize()` |
| 46 | + |
| 47 | +You can inspect the state of a Client by leveraging the summarize() method, which returns a DataFrame containing information aobut your Client. |
| 48 | + |
| 49 | +```python |
| 50 | +client.summarize() |
| 51 | +``` |
| 52 | + |
| 53 | +#### 2. Call save_to_json_file to save a snapshot of the Client to JSON |
| 54 | + |
| 55 | +In order to save an experiment to JSON, we need to call the `save_to_json_file` method on the Client instance. This method takes a single optional argument `filepath`, which is the filepath where we want to save the JSON file (argument defaults to "ax_client_snapshot.json"). |
| 56 | + |
| 57 | +```python |
| 58 | +client.save_to_json_file() |
| 59 | +``` |
| 60 | + |
| 61 | +On success, this will save a snapshot of the Client's settings and state to the specified file. |
| 62 | + |
| 63 | +You will be able to view your experiment data in the `experiment` object of the JSON |
| 64 | + |
| 65 | +### Load an Experiment from JSON |
| 66 | +#### 1. Call load_from_json_file to initialize a new Client |
| 67 | + |
| 68 | +We will now load the previously saved Client snapshot into a new one. You can do this by calling `load_from_json_file` |
| 69 | + |
| 70 | +```python |
| 71 | +new_client = Client.load_from_json_file(filepath = "ax_client_snapshot.json") |
| 72 | +``` |
| 73 | + |
| 74 | +#### 2. Validate the state of the Experiment via `summarize()` |
| 75 | + |
| 76 | +We can now view the state of the experiment by calling `summarize()`, and validate that it is the same as the one we saved in the earlier section |
| 77 | + |
| 78 | +```python |
| 79 | +new_client.summarize() |
| 80 | +``` |
| 81 | + |
| 82 | +## Customizing the Serialization Process |
| 83 | + |
| 84 | +If you have custom metrics or runners that you want to ensure are saved to JSON properly, you can initialize the `Client` with a `StorageConfig` that contains a `RegistryBundle`, that bundles together encoding and decoding logic for use in the save/load functions. |
| 85 | + |
| 86 | +```python |
| 87 | +from ax.storage.registry_bundle import RegistryBundle |
| 88 | + |
| 89 | +storage_config = StorageConfig( |
| 90 | + registry_bundle = RegistryBundle( |
| 91 | + runner_clss={MyRunner: None}, |
| 92 | + metric_clss={MyMetric: None}, |
| 93 | + ) |
| 94 | +) |
| 95 | + |
| 96 | +client = Client(storage_config = storage_config) |
| 97 | +``` |
| 98 | + |
| 99 | +## Learn more |
| 100 | + |
| 101 | +Take a look at these other recipes to continue your learning: |
| 102 | + |
| 103 | +- [Saving, Loading, and Updating an Ax Experiment from SQLite](#) |
0 commit comments