File: ml_samples_sweep_configurations.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (316 lines) | stat: -rw-r--r-- 12,367 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: ml_samples_sweep_configurations.py
DESCRIPTION:
    These samples demonstrate different ways to configure hyperparameter sweep jobs.
USAGE:
    python ml_samples_sweep_configurations.py

"""

import os


class SweepConfigurationOptions(object):
    def ml_sweep_config(self):
        from azure.identity import DefaultAzureCredential

        from azure.ai.ml import MLClient

        subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
        resource_group = os.environ["RESOURCE_GROUP_NAME"]
        credential = DefaultAzureCredential()
        ml_client = MLClient(credential, subscription_id, resource_group, workspace_name="test-ws1")

        cpu_cluster = ml_client.compute.get("cpu-cluster")

        from azure.ai.ml.entities import Environment

        job_env = Environment(
            name="my-env",
            image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest",
        )
        job_env = ml_client.environments.create_or_update(job_env)

        # [START configure_sweep_job_choice_loguniform]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        from azure.ai.ml.sweep import Choice, LogUniform

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        job_for_sweep = job(
            kernel=LogUniform(min_value=-6, max_value=-1),
            penalty=Choice([0.9, 0.18, 0.36, 0.72]),
        )

        # [END configure_sweep_job_choice_loguniform]

        # [START configure_sweep_job_uniform]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        from azure.ai.ml.sweep import Uniform

        job_for_sweep = job(
            kernel=Uniform(min_value=0.0005, max_value=0.005),
            penalty=Uniform(min_value=0.9, max_value=0.99),
        )
        # [END configure_sweep_job_uniform]

        # [START configure_sweep_job_bandit_policy]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        from azure.ai.ml.sweep import Uniform

        job_for_sweep = job(
            kernel=Uniform(min_value=0.0005, max_value=0.005),
            penalty=Uniform(min_value=0.9, max_value=0.99),
        )

        from azure.ai.ml.sweep import BanditPolicy

        sweep_job = job_for_sweep.sweep(
            sampling_algorithm="random",
            primary_metric="best_val_acc",
            goal="Maximize",
            max_total_trials=8,
            max_concurrent_trials=4,
            early_termination_policy=BanditPolicy(slack_factor=0.15, evaluation_interval=1, delay_evaluation=10),
        )
        # [END configure_sweep_job_bandit_policy]

        # [START configure_sweep_job_median_stopping_policy]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        from azure.ai.ml.sweep import MedianStoppingPolicy, Uniform

        job_for_sweep = job(
            kernel=Uniform(min_value=0.0005, max_value=0.005),
            penalty=Uniform(min_value=0.9, max_value=0.99),
        )

        sweep_job = job_for_sweep.sweep(
            sampling_algorithm="random",
            primary_metric="best_val_acc",
            goal="Maximize",
            max_total_trials=8,
            max_concurrent_trials=4,
            early_termination_policy=MedianStoppingPolicy(delay_evaluation=5, evaluation_interval=2),
        )
        # [END configure_sweep_job_median_stopping_policy]

        # [START configure_sweep_job_bayesian_sampling_algorithm]
        from azure.ai.ml.entities import CommandJob
        from azure.ai.ml.sweep import BayesianSamplingAlgorithm, Objective, SweepJob, SweepJobLimits

        command_job = CommandJob(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        sweep = SweepJob(
            sampling_algorithm=BayesianSamplingAlgorithm(),
            trial=command_job,
            search_space={"ss": Choice(type="choice", values=[{"space1": True}, {"space2": True}])},
            inputs={"input1": {"file": "top_level.csv", "mode": "ro_mount"}},  # type:ignore
            compute="top_level",
            limits=SweepJobLimits(trial_timeout=600),
            objective=Objective(goal="maximize", primary_metric="accuracy"),
        )
        # [END configure_sweep_job_bayesian_sampling_algorithm]

        # [START configure_sweep_job_grid_sampling_algorithm]
        from azure.ai.ml.entities import CommandJob
        from azure.ai.ml.sweep import GridSamplingAlgorithm, SweepJob, SweepJobLimits

        command_job = CommandJob(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        sweep = SweepJob(
            sampling_algorithm=GridSamplingAlgorithm(),
            trial=command_job,
            search_space={"ss": Choice(type="choice", values=[{"space1": True}, {"space2": True}])},
            inputs={"input1": {"file": "top_level.csv", "mode": "ro_mount"}},  # type:ignore
            compute="top_level",
            limits=SweepJobLimits(trial_timeout=600),
        )
        # [END configure_sweep_job_grid_sampling_algorithm]

        # [START configure_sweep_job_random_sampling_algorithm]
        from azure.ai.ml.entities import CommandJob
        from azure.ai.ml.sweep import RandomSamplingAlgorithm, SweepJob, SweepJobLimits

        command_job = CommandJob(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        sweep = SweepJob(
            sampling_algorithm=RandomSamplingAlgorithm(seed=999, rule="sobol", logbase="e"),
            trial=command_job,
            search_space={"ss": Choice(type="choice", values=[{"space1": True}, {"space2": True}])},
            inputs={"input1": {"file": "top_level.csv", "mode": "ro_mount"}},  # type:ignore
            compute="top_level",
            limits=SweepJobLimits(trial_timeout=600),
        )
        # [END configure_sweep_job_random_sampling_algorithm]

        # [START configure_sweep_job_truncation_selection_policy]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        from azure.ai.ml.sweep import QUniform, TruncationSelectionPolicy, Uniform

        job_for_sweep = job(
            kernel=Uniform(min_value=0.0005, max_value=0.005),
            penalty=QUniform(min_value=0.05, max_value=0.75, q=1),
        )

        sweep_job = job_for_sweep.sweep(
            sampling_algorithm="random",
            primary_metric="best_val_acc",
            goal="Maximize",
            max_total_trials=8,
            max_concurrent_trials=4,
            early_termination_policy=TruncationSelectionPolicy(delay_evaluation=5, evaluation_interval=2),
        )
        # [END configure_sweep_job_truncation_selection_policy]

        # [START configure_sweep_job_randint_normal]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        from azure.ai.ml.sweep import Normal, Randint

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        job_for_sweep = job(
            penalty=Randint(upper=5),
            kernel=Normal(mu=2.0, sigma=1.0),
        )

        # [END configure_sweep_job_randint_normal]

        # [START configure_sweep_job_lognormal_qlognormal]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        from azure.ai.ml.sweep import LogNormal, QLogNormal

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        job_for_sweep = job(
            kernel=LogNormal(mu=0.0, sigma=1.0),
            penalty=QLogNormal(mu=5.0, sigma=2.0),
        )

        # [END configure_sweep_job_lognormal_qlognormal]

        # [START configure_sweep_job_qloguniform_qnormal]
        from azure.ai.ml import command

        job = command(
            inputs=dict(kernel="linear", penalty=1.0),
            compute=cpu_cluster,
            environment=f"{job_env.name}:{job_env.version}",
            code="./scripts",
            command="python scripts/train.py --kernel $kernel --penalty $penalty",
            experiment_name="sklearn-iris-flowers",
        )

        from azure.ai.ml.sweep import QLogUniform, QNormal

        # we can reuse an existing Command Job as a function that we can apply inputs to for the sweep configurations
        job_for_sweep = job(
            penalty=QNormal(mu=2.0, sigma=1.0, q=1),
            kernel=QLogUniform(min_value=1.0, max_value=5.0),
        )

        # [END configure_sweep_job_qloguniform_qnormal]


if __name__ == "__main__":
    sample = SweepConfigurationOptions()
    sample.ml_sweep_config()