Coder Social home page Coder Social logo

batch of suggestions about ax HOT 18 OPEN

Fa20 avatar Fa20 commented on July 28, 2024
batch of suggestions

from ax.

Comments (18)

mgrange1998 avatar mgrange1998 commented on July 28, 2024

Hi,

Is a batch trial what you are looking for? It allows you to attach multiple arms to one trial and deploy and evaluate them together. (See https://ax.dev/api/core.html#batchtrial for more details)

In terms of trials running together, the GenerationStrategy objects allows you to specify "max_parallelism" which controls the number of trials which can be running at once. See the generation strategy wiki here for more details on that https://ax.dev/tutorials/generation_strategy.html#1.-Quick-start-examples

Let me know if this helps or if you have any other questions.

from ax.

Fa20 avatar Fa20 commented on July 28, 2024

Thanks ffor your answer. yes attach mutiple arms to one trial. I have tried to use it with the follwoing example def create_experiment():
ax_client = AxClient()
ax_client.create_experiment(
name="multi_objective_optimization",
parameters=[
{"name": "x1", "type": "range", "bounds": [-1.0, 3.0]},
{"name": "x2", "type": "range", "bounds": [-2.0, 2.0]},
{"name": "x3", "type": "range", "bounds": [-2.0, 2.0]},
{"name": "x4", "type": "range", "bounds": [-20.0, 20.0]}
],
objectives={
"objective_1": ObjectiveProperties(minimize=True),
"objective_2": ObjectiveProperties(minimize=False)
},
overwrite_existing_experiment=True
)
return ax_client

def evaluate_batch_trial(ax_client, batch_trial):
for arm_name, arm in batch_trial.arms_by_name.items():
objective_1_result = arm.parameters['x1'] * 2
objective_2_result = 100 - arm.parameters['x2']

    ax_client.attach_trial_data(
        trial_index=batch_trial.index,
        arm_name=arm_name,
        results={
            "objective_1": (objective_1_result, 0.0),  
            "objective_2": (objective_2_result, 0.0)
        }
    )
batch_trial.mark_completed()  

ax_client = create_experiment()

batch_trial = ax_client.create_batch_trial()
for _ in range(10):
parameters = ax_client.get_next_trial().parameters
batch_trial.add_arm(parameters)

evaluate_batch_trial(ax_client, batch_trial) but it does not work correct? is there any tutorials which explain how mutiple arms can be used with example

from ax.

mgrange1998 avatar mgrange1998 commented on July 28, 2024

For a tutorial, you can follow the generation strategy tutorial https://ax.dev/tutorials/generation_strategy.html
Just set "use_batch_trials=True" when calling choose generation strategy.

For your example, could you please provide error logs and more details about why it is failing?

from ax.

Fa20 avatar Fa20 commented on July 28, 2024

AttributeError Traceback (most recent call last)
Cell In[57], line 43
40 ax_client = create_experiment()
42 # Request a batch of 10 trials at once
---> 43 batch_trial = ax_client.create_batch_trial() # Create a new batch trial
44 for _ in range(10): # Add 10 arms to the batch
45 parameters = ax_client.get_next_trial().parameters

AttributeError: 'AxClient' object has no attribute 'create_batch_trial' , I have tried in the above code to use batch trail to get batch ot trials and then evaluate them but it seems that I did it wrong

from ax.

mgrange1998 avatar mgrange1998 commented on July 28, 2024

See https://ax.dev/api/service.html#module-ax.service.ax_client
"Note: AxClient expects to only propose 1 arm (suggestion) per trial; support for use cases that require use of batches is coming soon."

AxClient as of yet does not support batch trial, you'll need to follow the generation strategy tutorial to test out batch trials.

from ax.

Fa20 avatar Fa20 commented on July 28, 2024

https://ax.dev/tutorials/generation_strategy.html but this toutorial I think for just one objective function ? can I use it for batch trials in case of Multi-objective functions?

from ax.

mgrange1998 avatar mgrange1998 commented on July 28, 2024

Yes, try the "use_batch_trials" flag in the "choose_generation_strategy" method call.

use_batch_trials: bool = False,

from ax.

Fa20 avatar Fa20 commented on July 28, 2024

gs = choose_generation_strategy(
search_space=get_branin_search_space(),
use_batch_trials=True,
)

Main optimization loop

for _ in range(2):
parameters, trial_index = ax_client.get_next_trial()
ax_client.complete_trial(trial_index=trial_index, raw_data=evaluate_parameters(parameters))
but how can run the main optmization to get the multiply sugesstion of the papameters values and not sequential based on the above code I'm still geeting one sugesstion which is evaluted and then the nex etc.

from ax.

Balandat avatar Balandat commented on July 28, 2024

but how can run the main optmization to get the multiply sugesstion of the papameters values and not sequential based on the above code I'm still geeting one sugesstion which is evaluted and then the nex etc.

You can just call .get_next_trial() multiple times without calling complete_trial() in between. AxClient will be aware that the previous trials are "pending" and account for that in subsequent candidate suggestion(s).

from ax.

Fa20 avatar Fa20 commented on July 28, 2024

@Balandat so in this case I do not need to use choose_generation_strateg and no need to set the use_batch_trials=True if I understand you correct?
my second question: is there any difference bewteen using get_next_trial() multiple times and then evalute the objective on them and use choose_generation_strategy(
search_space=get_branin_search_space(),
use_batch_trials=True,
) ? are both give same accuracy of sugesstions for the value of the parameters ?

from ax.

Balandat avatar Balandat commented on July 28, 2024

in this case I do not need to use choose_generation_strateg and no need to set the use_batch_trials=True if I understand you correct?

Yes.

are both give same accuracy of sugesstions for the value of the parameters ?

In a nutshell, yes. There are some subtleties about how exactly they are generated, but for all intents and purposes you can see them as equivalent in terms of generation.

The main discerning factor of a BatchTrial is how the arms are being evaluated. You can read more about this here: https://github.com/facebook/Ax/blob/main/ax/core/batch_trial.py?fbclid=IwAR2UtrpXZwIWmylR2zZrq54v50RTtvs_r6pcJDWcG1a85riyjYGK0fGDL1g#L103-L115. If that's not the setting you're in you are fine with just callingget_next_trial() repeatedly.

from ax.

Fa20 avatar Fa20 commented on July 28, 2024

for the of MOO (in my case differnt than the tutorial since I do not have the refernce point). should I follow the same steps since there are more detaials and also sobol sampling inclused etc. or should I follow

def create_experiment():
    ax_client = AxClient()
    ax_client.create_experiment(
        name="multi_objective_optimization",
        parameters=[
            {"name": "x1", "type": "range", "bounds": [-1.0, 3.0]},
            {"name": "x2", "type": "range", "bounds": [-2.0, 2.0]},
            {"name": "x3", "type": "range", "bounds": [-2.0, 2.0]},
            {"name": "x4", "type": "range", "bounds": [-20.0, 20.0]}
        ],
        objectives={
            "objective_1": ObjectiveProperties(minimize=True),
            "objective_2": ObjectiveProperties(minimize=False)
        },
        overwrite_existing_experiment=True
    )
    return ax_client

def evaluate_batch_trial(ax_client, batch_trial):
    for arm_name, arm in batch_trial.arms_by_name.items():
        objective_1_result = arm.parameters['x1'] * 2
        objective_2_result = 100 - arm.parameters['x2']
    
        ax_client.attach_trial_data(
            trial_index=batch_trial.index,
            arm_name=arm_name,
            results={
                "objective_1": (objective_1_result, 0.0),  
                "objective_2": (objective_2_result, 0.0)
            }
        )
    batch_trial.mark_completed()  

ax_client = create_experiment()

batch_trial = ax_client.create_batch_trial()
for _ in range(10):
    parameters = ax_client.get_next_trial().parameters
    batch_trial.add_arm(parameters)

which is easy to understand it but in this case I will be not able to evaluate the results using one of the two algoritms explained in the tutorial(https://ax.dev/versions/0.1.18/tutorials/multiobjective_optimization.html) and If I folllow this tutorial how can I define the primary and secondary objective functions

from ax.

Balandat avatar Balandat commented on July 28, 2024

You can follow the same steps for MOO, no need to use BatchTrials for MOO.

which is easy to understand it but in this case I will be not able to evaluate the results using one of the two algoritms explained in the tutorial

You mean b/c the tutorial uses the developer API to evaluate the different algorithms? It is possible to also use different algorithms than the defaults in AxClient by passing a GenerationStrategy to the AxClient() instantiation: https://ax.dev/tutorials/generation_strategy.html

You also want to look at the current version of the tutorial if you're on a newer version of Ax (0.1.18 as in the tutorial is ancient): https://ax.dev/tutorials/multiobjective_optimization.html

from ax.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.