Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add json_encoder_registry in RegistryBundle to save to JSON file #2032

Closed
seabull opened this issue Nov 30, 2023 · 0 comments
Closed

Comments

@seabull
Copy link

seabull commented Nov 30, 2023

I am creating a class that inherits from Metric. That class has an attribute which is a data ingestor. I'm looking to serialize and deserialize the Experiment in case of system restarts. To do this, I added the custom Metric class to json_encoder_registry via RegistryBundle but it failed with the following error when trying to save_experiment.

JSONEncodeError: Object <my_module.MyDataIngestor object at 0x19cafe110> passed to object_to_json (of type <class 'my_module.MyDataIngestor'>, module: my_module) is not registered with a corresponding encoder in ENCODER_REGISTRY.

How do I handle serialization and deserialization for the data ingestor class MyDataIngestor?

Here is outline of the code:

from ax import Arm, Metric, Experiment, GeneratorRun, Models, Objective, OptimizationConfig, Runner
from ax.utils.common.serialization import SerializationMixin
from ax.storage.json_store.load import load_experiment
from ax.storage.json_store.save import save_experiment
from ax.storage.registry_bundle import RegistryBundle


class MyMetric(Metric):
  def __init__(
            self,
            data_ingestor: MyDataIngestor,
            name="barry",
            *args,
            **kwargs,
        ):
            super().__init__(name=name, *args, **kwargs)
            self.data_ingestor = data_ingestor

class MyDataIngestor(MyDataIngestorBase):
    def __init__(self, my_snr, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.my_snr = my_snr


bundle = RegistryBundle(runner_clss={MyRunner: None,}, 
                        metric_clss={
                            MyMetric: None, 
                            }                            )
filepath = "temp/my_experiment.json"

...
save_experiment(exp, filepath, encoder_registry=bundle.encoder_registry)

@seabull seabull closed this as completed Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant