Comments (5)
Thank you @Cesar-Cardoso. I will try it out and post updates here.
from ax.
Hi @seabull! Could you provide a minimal reproducible example for this issue. From the stack trace it's not obvious that the issue is directly related to the decoding logic. In particular the error is pointing out that your data_ingestor
object is missing the property __dict__
you're trying to reference; but it's hard to get much further without the rest of the code.
from ax.
Thanks for the response.
Here is the __init__
that generates the error in the stack trace. I hope this provides some context:
class GeneralFactorialMetric(Metric):
def __init__(
self,
data_ingestor: SimulateTreatmentData,
name="barry",
*args,
**kwargs,
):
super().__init__(name=name, *args, **kwargs)
self.data_ingestor = data_ingestor
self.experiment_values = {}
print(f"{data_ingestor=}")
print(f"{data_ingestor.__dict__=}")
As shown in the stack trace, the print(f"{data_ingestor.__dict__=}")
statement complains that data_ingestor
is a dict, not an object (of SimulateTreatmentData
class). If the decoder works, we would expect data_ingestor.__dict__
to print successfully.
from ax.
I know this example below is a little long (sorry!) but that is the minimal I could reproduce the error.
# %%
from typing import Any, Dict, Optional
from ax import (
Arm,
ChoiceParameter,
Experiment,
Metric,
Objective,
OptimizationConfig,
ParameterType,
Runner,
SearchSpace,
)
from ax.storage.json_store.load import load_experiment
from ax.storage.json_store.registry import CORE_DECODER_REGISTRY, CORE_ENCODER_REGISTRY
from ax.storage.json_store.save import save_experiment
from ax.storage.registry_bundle import RegistryBundle
from ax.utils.common.serialization import SerializationMixin, serialize_init_args
from ax.utils.common.typeutils import checked_cast
class SimulateTreatmentData(SerializationMixin):
def __init__(
self,
search_space_parameters: Optional[list[list[str]]] = None,
num_minibatches_per_update: int = 3,
):
self.search_space_parameters = search_space_parameters
self.num_minibatches_per_update = num_minibatches_per_update
@classmethod
# pyre-fixme [2]: Parameter `obj` must have a type other than `Any``
def serialize_init_args(cls, obj: Any) -> Dict[str, Any]:
sim = checked_cast(SimulateTreatmentData, obj)
properties = serialize_init_args(obj=sim)
properties["search_space_parameters"] = sim.search_space_parameters
properties["num_minibatches_per_update"] = sim.num_minibatches_per_update
return properties
class GeneralFactorialMetric(Metric):
def __init__(
self,
data_ingestor: SimulateTreatmentData,
name="barry",
*args,
**kwargs,
):
super().__init__(name=name, *args, **kwargs)
self.data_ingestor = data_ingestor
print(f"{data_ingestor.__dict__=}") # it is empty
my_treatments = ["treatment-0", "treatment-1", "treatment-2"]
class MyRunner(Runner):
def run(self, trial):
trial_metadata = {"name": str(trial.index)}
return trial_metadata
search_space = SearchSpace(
parameters=[
ChoiceParameter(
name="arm_name", # "factor" + str(ifactor),
parameter_type=ParameterType.STRING,
values=my_treatments,
is_ordered=False,
sort_values=False,
),
]
)
data_simulator = SimulateTreatmentData(
search_space_parameters=[par.values for par in search_space.parameters.values()],
)
exp = Experiment(
name="my_factorial_closed_loop_experiment",
search_space=search_space,
optimization_config=OptimizationConfig(
objective=Objective(
metric=GeneralFactorialMetric(
data_ingestor=data_simulator,
name="success_metric",
)
)
),
runner=MyRunner(),
)
arms = [
Arm(parameters={"arm_name": treatment}, name=treatment)
for treatment in my_treatments
]
trial = exp.new_batch_trial().add_arms_and_weights(arms=arms, weights=[0.4, 0.3, 0.3])
def treatment_data_to_dict(sim: SimulateTreatmentData) -> Dict[str, Any]:
"""Convert SimulateTreatmentData to a dictionary."""
properties = sim.serialize_init_args(obj=sim)
properties["__type"] = sim.__class__.__name__
return properties
core_encoders = {
**CORE_ENCODER_REGISTRY,
SimulateTreatmentData: treatment_data_to_dict,
}
# class djfslkj:
# def __init__(self, *args, **kwargs):
# raise ValueError("Foo **********nfdklajgkladjkljflkjalkjflkjlkdj")
decoders = {
**CORE_DECODER_REGISTRY,
# "SimulateBinomialTreatmentData": djfslkj,
# "SimulateTreatmentData": djfslkj,
"SimulateTreatmentData": SimulateTreatmentData,
}
bundle = RegistryBundle(
runner_clss={
MyRunner: None,
},
metric_clss={
GeneralFactorialMetric: None,
},
json_encoder_registry=core_encoders,
json_decoder_registry=decoders,
)
filepath = "temp/my_experiment.json"
save_experiment(exp, filepath, encoder_registry=bundle.encoder_registry)
exp = load_experiment(filepath=filepath, decoder_registry=bundle.decoder_registry)
from ax.
Thanks for the examples! The issue happens when decoding the GeneralFactorialMetric
object. In particular the data_ingestor
property gets loaded as a dict
, which causes the issue. A solution is to override deserialize_init_args
for GeneralFactorialMetric
as such:
class GeneralFactorialMetric(Metric):
def __init__(
self,
data_ingestor: SimulateTreatmentData,
name="barry",
*args,
**kwargs,
):
super().__init__(name=name, *args, **kwargs)
self.data_ingestor = data_ingestor
print(f"{data_ingestor.__dict__=}") # it is empty
@classmethod
def deserialize_init_args(
cls,
args: Dict[str, Any],
decoder_registry: Optional[TDecoderRegistry] = None,
class_decoder_registry: Optional[TClassDecoderRegistry] = None):
return {
"data_ingestor": SimulateTreatmentData(
**SimulateTreatmentData.deserialize_init_args(
args=args['data_ingestor'],
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry)),
"name": args.get("name") or "barry"
}
With this change I managed to encode/decode an experiment using your code. Let me know if this solves your issue. Cheers!
from ax.
Related Issues (20)
- cannot import name 'TrainingData' HOT 2
- applying complex constrains HOT 2
- Ax is not not starting as many workers as I'd like to; sometimes, get_next_trials returns 0 new trials HOT 4
- Evaluating custom candidates HOT 2
- Input Feature Selection - Does the relevant code exist? HOT 6
- [Feature Request] support constraints on `ChoiceParameters` HOT 4
- Extending Models.THOMPSON with an extra parameter HOT 1
- There are some questions when i use the Ax HOT 7
- Space characters in the objective name AND specifying a threshold leads to an error message: "AssertionError: Outcome constraint should be of form `metric_name >= x" HOT 1
- Pandas deprecation warning when deserializing AxClient JSON HOT 2
- AX seems to get stuck with Ray
- `StandardizeY` transform requires non-empty data." when using SAASBO
- Plotting outside of a notebook HOT 1
- Setting search space step size in Ax Service API HOT 10
- Problem when Sobol falls back to HitAndRunPolytopeSampler HOT 3
- Arms from previous batch keep appearing in new batches HOT 5
- EHVI & NEHVI break with more than 7 objectives HOT 4
- Multi-objective experiments generate duplicated data HOT 5
- Question: Transforming objective when passing `best_f` to `ProbabilityOfImprovement`, etc. HOT 3
- [Question] Multiobjective optimization where one target has no optimization direction (but required range)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ax.