-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary_building_ai_solutions_azure_ml.html
1276 lines (1071 loc) · 115 KB
/
summary_building_ai_solutions_azure_ml.html
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="generator" content="Pelican" />
<title>Summary: Building AI Solutions with Azure ML</title>
<link rel="stylesheet" href="/theme/css/main.css" />
<meta name="description" content="While studying for the Azure Data Scientist Associate certification, I took notes from Building AI Solution with Azure ML course. In this single..." />
</head>
<body id="index" class="home">
<header id="banner" class="body">
<h1><a href="/">Marco Santoni</a></h1>
<nav><ul>
<li><a href="/pages/about.html">about</a></li>
<li><a href="/pages/bookshelf.html">bookshelf</a></li>
<li class="active"><a href="/category/posts.html">posts</a></li>
</ul></nav>
</header><!-- /#banner -->
<section id="content" class="body">
<article>
<header>
<h1 class="entry-title">
<a href="/summary_building_ai_solutions_azure_ml.html" rel="bookmark"
title="Permalink to Summary: Building AI Solutions with Azure ML">Summary: Building AI Solutions with Azure ML</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2020-08-19T06:41:00+02:00">
Published: Wed 19 August 2020
</abbr>
<address class="vcard author">
By <a class="url fn" href="/author/marco-santoni.html">Marco Santoni</a>
</address>
<p>In <a href="/category/posts.html">posts</a>.</p>
</footer><!-- /.post-info --> <p>While studying for the <em>Azure Data Scientist Associate</em> certification, I took notes from <a href="https://docs.microsoft.com/en-us/learn/paths/build-ai-solutions-with-azure-ml-service/">Building AI Solution with Azure ML</a> course. In this single page, you'll find the entire content of the course (as of 18th August, 2020). This page is a small support for those preparing for earning the certification.</p>
<h1>Intro</h1>
<h2>Azure ML Workspace</h2>
<p>workspaces are azure resources. include:</p>
<ul>
<li>compute</li>
<li>notebooks</li>
<li>pipelines</li>
<li>data</li>
<li>experiments</li>
<li>models</li>
</ul>
<p>created alongside</p>
<ul>
<li>storage account: files by WS + data</li>
<li>application insights</li>
<li>key vault</li>
<li>vm</li>
<li>container registry</li>
</ul>
<p>permission: RBAC</p>
<p>edition
- basic (no graphic designer)
- enterprise</p>
<h2>Tools</h2>
<p>Azure ML Studio
- designer (no code ML model dev)
- automated ML</p>
<p>Azure ML SDK</p>
<p>Azure ML CLI Extensions</p>
<p>Compute Instances
- choose VM
- store notebooks independently of VMs</p>
<p>VS Code - Azure ML Extension</p>
<h2>Experiments</h2>
<p>Azure ML tracks run of experiments</p>
<div class="highlight"><pre><span></span><code><span class="o">...</span>
<span class="n">run</span> <span class="o">=</span> <span class="n">experiment</span><span class="o">.</span><span class="n">start_logging</span><span class="p">()</span>
<span class="o">...</span>
<span class="n">run</span><span class="o">.</span><span class="n">complete</span><span class="p">()</span>
</code></pre></div>
<ul>
<li>logging metrics. <code>run.log('name', value)</code>. You can review them via <code>RunDetails(run).show()</code></li>
<li>experiment output file. Example: trained models. <code>run.upload_file(..)</code>.</li>
</ul>
<p><strong>Script as an experiment</strong>. In the script, you can get the context: <code>run = Rune.get_context()</code>. To run it, you define:</p>
<ul>
<li>RunConfiguration: python environment</li>
<li>ScriptRunConfig: associates RunConfiguration with script</li>
</ul>
<h1>Train a ML model</h1>
<h2>Estimators</h2>
<p>Estimator: encapsulates a run configuration and a script configuration in a single object. Save trained model as pickle in <code>outputs</code> folder</p>
<div class="highlight"><pre><span></span><code><span class="n">estimator</span> <span class="o">=</span> <span class="n">Estimator</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'experiment'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'training.py'</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'local'</span><span class="p">,</span>
<span class="n">conda_packages</span><span class="o">=</span><span class="p">[</span><span class="s1">'scikit-learn'</span><span class="p">]</span>
<span class="p">)</span>
<span class="n">experiment</span> <span class="o">=</span> <span class="n">Experiment</span><span class="p">(</span><span class="n">workspace</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'train_experiment'</span><span class="p">)</span>
<span class="n">run</span> <span class="o">=</span> <span class="n">experiment</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">config</span><span class="o">=</span><span class="n">estimator</span><span class="p">)</span>
</code></pre></div>
<p>Framework-specific estimators simplify configurations</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">azureml.train.sklearn</span> <span class="kn">import</span> <span class="n">SKLearn</span>
<span class="n">estimator</span> <span class="o">=</span> <span class="n">SKLearn</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'experiment'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'training.py'</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'local'</span>
<span class="p">)</span>
</code></pre></div>
<h2>Script parameters</h2>
<p>Use <code>argparse</code> to read the parameters in a script (eg regularization rate). To pass a parameter to an <code>Estimator</code>:</p>
<div class="highlight"><pre><span></span><code><span class="n">estimator</span> <span class="o">=</span> <span class="n">SKLearn</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'experiment'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'training.py'</span><span class="p">,</span>
<span class="n">script_params</span><span class="o">=</span><span class="p">{</span><span class="s1">'--reg_rate'</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">}</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'local'</span>
<span class="p">)</span>
</code></pre></div>
<h2>Registering models</h2>
<p>Once the experiment <code>Run</code> has completed, you can retrieve its outputs (eg trained model).</p>
<div class="highlight"><pre><span></span><code><span class="n">run</span><span class="o">.</span><span class="n">download_file</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'outputs/models.pkl'</span><span class="p">,</span> <span class="n">output_file_path</span><span class="o">=</span><span class="s1">'model.pkl'</span><span class="p">)</span>
</code></pre></div>
<p>Registering a model allows to track multiple versions of a model.</p>
<div class="highlight"><pre><span></span><code><span class="n">model</span> <span class="o">=</span> <span class="n">Model</span><span class="o">.</span><span class="n">register</span><span class="p">(</span>
<span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span>
<span class="n">model_name</span><span class="o">=</span><span class="s1">'classification_model'</span><span class="p">,</span>
<span class="n">model_path</span><span class="o">=</span><span class="s1">'model.pkl'</span><span class="p">,</span> <span class="c1">#local path</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'a classification model'</span><span class="p">,</span>
<span class="n">tags</span><span class="o">=</span><span class="p">{</span><span class="s1">'dept'</span><span class="p">:</span> <span class="s1">'sales'</span><span class="p">},</span>
<span class="n">model_framework</span><span class="o">=</span><span class="n">Model</span><span class="o">.</span><span class="n">Framework</span><span class="o">.</span><span class="n">SCIKITLEARN</span><span class="p">,</span>
<span class="n">model_framework_version</span><span class="o">=</span><span class="s1">'0.20.3'</span>
<span class="p">)</span>
</code></pre></div>
<p>or register from run:</p>
<div class="highlight"><pre><span></span><code><span class="n">run</span><span class="o">.</span><span class="n">register_model</span><span class="p">(</span>
<span class="o">...</span>
<span class="n">model_path</span><span class="o">=</span><span class="s1">'outputs/model.pkl'</span>
<span class="o">...</span>
<span class="p">)</span>
</code></pre></div>
<h1>Datastores</h1>
<p>Abstractions of cloud data sources encapsulating the information required to connect.</p>
<p>You can register a data store</p>
<ul>
<li>via ML Studio</li>
<li>via SDK</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="n">ws</span> <span class="o">=</span> <span class="n">Workspace</span><span class="o">.</span><span class="n">from_config</span><span class="p">()</span>
<span class="n">blob</span> <span class="o">=</span> <span class="n">Datastore</span><span class="o">.</span><span class="n">register_azure_blob_container</span><span class="p">(</span>
<span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span>
<span class="n">datastore_name</span><span class="o">=</span><span class="s1">'blob_data'</span><span class="p">,</span>
<span class="n">container_name</span><span class="o">=</span><span class="s1">'data_container'</span><span class="p">,</span>
<span class="n">account_name</span><span class="o">=</span><span class="s1">'az_acct'</span><span class="p">,</span>
<span class="n">account_key</span><span class="o">=</span><span class="s1">'123456'</span>
<span class="p">)</span>
</code></pre></div>
<p>In the SDK, you can list data stores.</p>
<h2>Use datastores</h2>
<p>Most common: Azure blob and file</p>
<div class="highlight"><pre><span></span><code><span class="n">blob_ds</span><span class="o">.</span><span class="n">upload</span><span class="p">(</span>
<span class="n">src_dir</span><span class="o">=</span><span class="s1">'/files'</span><span class="p">,</span>
<span class="n">target_path</span><span class="o">=</span><span class="s1">'/data/files'</span><span class="p">,</span>
<span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span>
<span class="p">)</span>
<span class="n">blob_ds</span><span class="o">.</span><span class="n">download</span><span class="p">(</span>
<span class="n">target_path</span><span class="o">=</span><span class="s1">'downloads'</span><span class="p">,</span>
<span class="n">prefix</span><span class="o">=</span><span class="s1">'/data'</span>
<span class="p">)</span>
</code></pre></div>
<p>You pass a data reference to the script to use a datastore. Data access models</p>
<ul>
<li>download: contents downloaded to the compute context of experiment</li>
<li>upload: files generated by experiment are uploaded after run</li>
<li>mount: path of datastore mounted as remote storage (only on remote compute target)</li>
</ul>
<p>Pass reference as script parameter:</p>
<div class="highlight"><pre><span></span><code><span class="n">data_ref</span> <span class="o">=</span> <span class="n">blob_ds</span><span class="o">.</span><span class="n">path</span><span class="p">(</span><span class="s1">'data/files'</span><span class="p">)</span><span class="o">.</span><span class="n">as_download</span><span class="p">(</span><span class="n">path_on_compute</span><span class="o">=</span><span class="s1">'training_data'</span><span class="p">)</span>
<span class="n">estimator</span> <span class="o">=</span> <span class="n">SKLearn</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'experiment_folder'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'training_script.py'</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'local'</span><span class="p">,</span>
<span class="n">script_params</span><span class="o">=</span><span class="p">{</span><span class="s1">'--data_folder'</span><span class="p">:</span> <span class="n">data_ref</span><span class="p">}</span>
<span class="p">)</span>
</code></pre></div>
<p>Retrieve it in script and use it like local folder:</p>
<div class="highlight"><pre><span></span><code><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'--data_folder'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s1">'str'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'data_folder'</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="n">data_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">data_folder</span><span class="p">)</span>
</code></pre></div>
<h2>Datasets</h2>
<p>Datasets are versioned packaged data objects consumed in experiments and pipelines. Types</p>
<ul>
<li>tabular: read as table</li>
<li>file: list of file paths</li>
</ul>
<p>You can create dataset via Azure ML Studio or via SDK. File paths can have wildcards (<code>/files/*.csv</code>).</p>
<p>Once a dataset is created, you can <strong>register</strong> it in the workspace (available later too).</p>
<p>Tabular:</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">azureml.core</span> <span class="kn">import</span> <span class="n">Dataset</span>
<span class="n">blob_ds</span> <span class="o">=</span> <span class="n">we</span><span class="o">.</span><span class="n">get_default_datastore</span><span class="p">()</span>
<span class="n">csv_paths</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">(</span><span class="n">blob_ds</span><span class="p">,</span> <span class="s1">'data/files/current_data.csv'</span><span class="p">),</span>
<span class="p">(</span><span class="n">blob_ds</span><span class="p">,</span> <span class="s1">'data/files/archive/*.csv'</span><span class="p">)</span>
<span class="p">]</span>
<span class="n">tab_ds</span> <span class="o">=</span> <span class="n">Dataset</span><span class="o">.</span><span class="n">Tabular</span><span class="o">.</span><span class="n">from_delimited_files</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="n">csv_paths</span><span class="p">)</span>
<span class="n">tab_ds</span> <span class="o">=</span> <span class="n">tab_ds</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">workspace</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'csv_table'</span><span class="p">)</span>
</code></pre></div>
<p>File:</p>
<div class="highlight"><pre><span></span><code><span class="n">blob_ds</span> <span class="o">=</span> <span class="n">ws</span><span class="o">.</span><span class="n">get_default_datastore</span><span class="p">()</span>
<span class="n">file_ds</span> <span class="o">=</span> <span class="n">Dataset</span><span class="o">.</span><span class="n">File</span><span class="o">.</span><span class="n">from_files</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="p">(</span><span class="n">blob_ds</span><span class="p">,</span> <span class="s1">'data/files/images/*.jpg'</span><span class="p">))</span>
<span class="n">file_ds</span> <span class="o">=</span> <span class="n">file_ds</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'img_files'</span><span class="p">)</span>
</code></pre></div>
<p><strong>Retrieve</strong> a dataset</p>
<div class="highlight"><pre><span></span><code><span class="n">ws</span> <span class="o">=</span> <span class="n">Workspace</span><span class="o">.</span><span class="n">from_config</span><span class="p">()</span>
<span class="c1"># Get a dataset from workspace datasets collection</span>
<span class="n">ds1</span> <span class="o">=</span> <span class="n">ws</span><span class="o">.</span><span class="n">datasets</span><span class="p">[</span><span class="s1">'csv_table'</span><span class="p">]</span>
<span class="c1"># Get a dataset by name from the datasets class</span>
<span class="n">ds2</span> <span class="o">=</span> <span class="n">Dataset</span><span class="o">.</span><span class="n">get_by_name</span><span class="p">(</span><span class="n">ws</span><span class="p">,</span> <span class="s1">'img_files'</span><span class="p">)</span>
</code></pre></div>
<p>Datasets can be <strong>versioned</strong>. Create a new versioning by registering with same name and <code>create_new_version</code> property:</p>
<div class="highlight"><pre><span></span><code><span class="n">file_ds</span> <span class="o">=</span> <span class="n">file_ds</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'img_files'</span><span class="p">,</span> <span class="n">create_new_version</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre></div>
<p>Retrieve specific version:</p>
<div class="highlight"><pre><span></span><code><span class="n">img_ds</span> <span class="o">=</span> <span class="n">Dataset</span><span class="o">.</span><span class="n">get_by_name</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'img_files'</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</code></pre></div>
<h1>Compute Contexts</h1>
<p>The runtime context for each experiment consists of</p>
<ul>
<li><em>environment</em> for the script, which includes all packages</li>
<li><em>compute target</em> on which the environment will be deployed</li>
</ul>
<h2>Intro to Environments</h2>
<p>Python runs in virtual environments (eg <code>Conda</code>, <code>pip</code>). Azure creates a Docker container and creates the environment. You create environments by</p>
<ul>
<li><code>Conda</code> or <code>pip</code> yaml file and load it:</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="n">env</span> <span class="o">=</span> <span class="n">Environment</span><span class="o">.</span><span class="n">from_conda_specification</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'training_env'</span><span class="p">,</span> <span class="n">file_path</span><span class="o">=</span><span class="s1">'./conda.yml'</span><span class="p">)</span>
</code></pre></div>
<ul>
<li>from existing <code>Conda</code> environment:</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="n">env</span> <span class="o">=</span> <span class="n">Environment</span><span class="o">.</span><span class="n">from_conda_environment</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'training_env'</span><span class="p">,</span>
<span class="n">conda_environment_name</span><span class="o">=</span><span class="s1">'py_env'</span><span class="p">)</span>
</code></pre></div>
<ul>
<li>specifying packages:</li>
</ul>
<div class="highlight"><pre><span></span><code><span class="n">env</span> <span class="o">=</span> <span class="n">Environment</span><span class="p">(</span><span class="s1">'training_env'</span><span class="p">)</span>
<span class="n">deps</span> <span class="o">=</span> <span class="n">CondaDependencies</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">conda_packages</span><span class="o">=</span><span class="p">[</span><span class="s1">'pandas'</span><span class="p">,</span> <span class="s1">'numpy'</span><span class="p">]</span>
<span class="n">pip_packages</span><span class="o">=</span><span class="p">[</span><span class="s1">'azureml-defaults'</span><span class="p">])</span>
<span class="n">env</span><span class="o">.</span><span class="n">python</span><span class="o">.</span><span class="n">conda_dependencies</span> <span class="o">=</span> <span class="n">deps</span>
</code></pre></div>
<p>Once created, you can register the environment in the workspace.</p>
<div class="highlight"><pre><span></span><code><span class="n">env</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">)</span>
</code></pre></div>
<p>Retrieve and assign it to a <code>ScriptRunConfig</code> or an <code>Estimator</code></p>
<div class="highlight"><pre><span></span><code><span class="n">tr_env</span> <span class="o">=</span> <span class="n">Environment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'training_env'</span><span class="p">)</span>
<span class="n">estimator</span> <span class="o">=</span> <span class="n">Estimator</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'experiment_folder'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'training_script.py'</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'local'</span><span class="p">,</span>
<span class="n">environment_definition</span><span class="o">=</span><span class="n">tr_env</span>
<span class="p">)</span>
</code></pre></div>
<h2>Compute targets</h2>
<p>Compute targets are physical or virtual computer on which experiments are run. Types of compute</p>
<ul>
<li><em>local compute</em>: your workstation or a virtual machine</li>
<li><em>compute clusters</em>: multi-node clusters of VMs that automatically scale up or down</li>
<li><em>inference clusters</em>: to deploy models, they use containers to initiate computing</li>
<li><em>attached compute</em>: attach a VM or Databricks cluster that you already use</li>
</ul>
<p>You can create a compute target via AML studio or via SDK. A <strong>managed</strong> compute target is one managed by AML. Via SDK</p>
<div class="highlight"><pre><span></span><code><span class="n">ws</span> <span class="o">=</span> <span class="n">Workspace</span><span class="o">.</span><span class="n">from_config</span><span class="p">()</span>
<span class="n">compute_name</span> <span class="o">=</span> <span class="s1">'aml-cluster'</span>
<span class="n">compute_config</span> <span class="o">=</span> <span class="n">AmlCompute</span><span class="o">.</span><span class="n">provisioning_configuration</span><span class="p">(</span>
<span class="n">vm_size</span><span class="o">=</span><span class="s1">'STANDARD_DS12_V2'</span><span class="p">,</span>
<span class="n">min_nodes</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
<span class="n">max_nodes</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
<span class="n">vm_priority</span><span class="o">=</span><span class="s1">'dedicated'</span>
<span class="p">)</span>
<span class="n">aml_cluster</span> <span class="o">=</span> <span class="n">ComputeTarget</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">we</span><span class="p">,</span> <span class="n">compute_name</span><span class="p">,</span> <span class="n">compute_config</span><span class="p">)</span>
<span class="n">aml_cluster</span><span class="o">.</span><span class="n">wait_for_completion</span><span class="p">()</span>
</code></pre></div>
<p>An <strong>unmanaged</strong> compute target is defined and managed outside AML. You can attach it via SDK:</p>
<div class="highlight"><pre><span></span><code><span class="n">ws</span> <span class="o">=</span> <span class="n">Workspace</span><span class="o">.</span><span class="n">from_config</span><span class="p">()</span>
<span class="n">compute_name</span> <span class="o">=</span> <span class="s1">'db-cluster'</span>
<span class="n">db_workspace_name</span> <span class="o">=</span> <span class="s1">'db_workspace'</span>
<span class="n">db_resource_group</span> <span class="o">=</span> <span class="s1">'db_resource_group'</span>
<span class="n">db_access_token</span> <span class="o">=</span> <span class="s1">'aocsinaocnasoivn'</span>
<span class="n">db_config</span> <span class="o">=</span> <span class="n">DatabricksCompute</span><span class="o">.</span><span class="n">attach_configuration</span><span class="p">(</span>
<span class="n">resource_group</span><span class="o">=</span><span class="n">db_resource_group</span><span class="p">,</span>
<span class="n">workspace_name</span><span class="o">=</span><span class="n">db_workspace_name</span><span class="p">,</span>
<span class="n">access_token</span><span class="o">=</span><span class="n">db_access_token</span>
<span class="p">)</span>
<span class="n">db_cluster</span> <span class="o">=</span> <span class="n">ComputeTarget</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">we</span><span class="p">,</span> <span class="n">compute_name</span><span class="p">,</span> <span class="n">db_config</span><span class="p">)</span>
<span class="n">db_cluster</span><span class="o">.</span><span class="n">wait_for_completion</span><span class="p">()</span>
</code></pre></div>
<p>You can check if a compute target does not exist already:</p>
<div class="highlight"><pre><span></span><code><span class="n">compute_name</span> <span class="o">=</span> <span class="s1">'aml_cluster'</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">aml_cluster</span> <span class="o">=</span> <span class="n">ComputeTarget</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">compute_name</span><span class="p">)</span>
<span class="k">except</span> <span class="n">ComputeTargetException</span><span class="p">:</span>
<span class="c1"># create it</span>
<span class="o">...</span>
</code></pre></div>
<p>You can use a compute target in an experiment run by specifying it as a parameter</p>
<div class="highlight"><pre><span></span><code><span class="n">compute_name</span> <span class="o">=</span> <span class="s1">'aml_cluster'</span>
<span class="n">training_env</span> <span class="o">=</span> <span class="n">Environment</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'training_env'</span><span class="p">)</span>
<span class="n">estimator</span> <span class="o">=</span> <span class="n">Estimator</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'experiment_folder'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'training_script.py'</span><span class="p">,</span>
<span class="n">environment_definition</span><span class="o">=</span><span class="n">training_env</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="n">compute_name</span>
<span class="p">)</span>
<span class="c1"># or specify a ComputeTarget object</span>
<span class="n">training_cluster</span> <span class="o">=</span> <span class="n">ComputeTarget</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">compute_name</span><span class="p">)</span>
<span class="n">estimator</span> <span class="o">=</span> <span class="n">Estimator</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'experiment_folder'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'training_script.py'</span><span class="p">,</span>
<span class="n">environment_definition</span><span class="o">=</span><span class="n">training_env</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="n">training_cluster</span>
<span class="p">)</span>
</code></pre></div>
<h1>Orchestrating with Pipelines</h1>
<p>A <em>pipeline</em> is a workflow of ml tasks in which each tasks is implemented as a <em>step</em> (either sequential or parallel). You can combine different compute targets. Common types of step:</p>
<ul>
<li><em>PythonScriptStep</em></li>
<li><em>EstimatorStep</em>: runs an estimator</li>
<li><em>DataTransferStep</em>: uses ADF</li>
<li><em>DatabricksStep</em></li>
<li><em>AdlaStep</em>: runs a <code>U-SQL</code> job in Azure Data Lake Analytics</li>
</ul>
<p>Define steps:</p>
<div class="highlight"><pre><span></span><code><span class="n">step1</span> <span class="o">=</span> <span class="n">PythonScriptStep</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'prepare data'</span><span class="p">,</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'scripts'</span><span class="p">,</span>
<span class="n">script_name</span><span class="o">=</span><span class="s1">'data_prep.py'</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'aml-cluster'</span><span class="p">,</span>
<span class="n">runconfig</span><span class="o">=</span><span class="n">run_config</span>
<span class="p">)</span>
<span class="n">step2</span> <span class="o">=</span> <span class="n">EstimatorStep</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'train model'</span><span class="p">,</span>
<span class="n">estimator</span><span class="o">=</span><span class="n">sk_estimator</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'aml-cluster'</span>
<span class="p">)</span>
</code></pre></div>
<p>Assign steps to pipeline:</p>
<div class="highlight"><pre><span></span><code><span class="n">train_pipeline</span> <span class="o">=</span> <span class="n">Pipeline</span><span class="p">(</span>
<span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span>
<span class="n">steps</span><span class="o">=</span><span class="p">[</span><span class="n">step1</span><span class="p">,</span><span class="n">step2</span><span class="p">]</span>
<span class="p">)</span>
<span class="c1"># create experiment and run pipeline</span>
<span class="n">experiment</span> <span class="o">=</span> <span class="n">Experiment</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'training-pipeline'</span><span class="p">)</span>
<span class="n">pipeline_run</span> <span class="o">=</span> <span class="n">experiment</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">train_pipeline</span><span class="p">)</span>
</code></pre></div>
<h2>Pass data between steps</h2>
<p>The <code>PipelineData</code> object is a special kind of <code>DataReference</code> that</p>
<ul>
<li>reference a location in a store</li>
<li>creates a da dependency between pipelines</li>
</ul>
<p>To pass it</p>
<ul>
<li>define a <code>PipelineData</code> object that references a location in a data store</li>
<li>specify the object as input or output for the steps that use it</li>
<li>pass the <code>PipelineData</code> object as a script parameter in steps that run scripts</li>
</ul>
<p>Example</p>
<div class="highlight"><pre><span></span><code><span class="n">raw_ds</span> <span class="o">=</span> <span class="n">Dataset</span><span class="o">.</span><span class="n">get_by_name</span><span class="p">(</span><span class="n">ws</span><span class="p">,</span> <span class="s1">'raw_dataset'</span><span class="p">)</span>
<span class="c1"># Define object to pass data between steps</span>
<span class="n">data_store</span> <span class="o">=</span> <span class="n">ws</span><span class="o">.</span><span class="n">get_default_datastore</span><span class="p">()</span>
<span class="n">prepped_data</span> <span class="o">=</span> <span class="n">PipelineData</span><span class="p">(</span><span class="s1">'prepped'</span><span class="p">,</span> <span class="n">datastore</span><span class="o">=</span><span class="n">data_store</span><span class="p">)</span>
<span class="n">step1</span> <span class="o">=</span> <span class="n">PythonScriptStep</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'prepare data'</span><span class="p">,</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'scripts'</span><span class="p">,</span>
<span class="n">script_name</span><span class="o">=</span><span class="s1">'data_prep.py'</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'aml-cluster'</span><span class="p">,</span>
<span class="n">runconfig</span><span class="o">=</span><span class="n">run_config</span><span class="p">,</span>
<span class="c1"># specify dataset</span>
<span class="n">inputs</span> <span class="o">=</span> <span class="p">[</span><span class="n">raw_ds</span><span class="o">.</span><span class="n">as_named_input</span><span class="p">(</span><span class="s1">'raw_data'</span><span class="p">)],</span>
<span class="c1"># specify PipelineData as output</span>
<span class="n">outputs</span> <span class="o">=</span> <span class="p">[</span><span class="n">prepped_data</span><span class="p">],</span>
<span class="c1"># script reference</span>
<span class="n">arugments</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'--folder'</span><span class="p">,</span> <span class="n">prepped_data</span><span class="p">]</span>
<span class="p">)</span>
<span class="n">step2</span> <span class="o">=</span> <span class="n">EstimatorStep</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'train model'</span><span class="p">,</span>
<span class="n">estimator</span><span class="o">=</span><span class="n">sk_estimator</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="s1">'aml-cluster'</span>
<span class="c1"># specify PipelineData</span>
<span class="n">inputs</span> <span class="o">=</span> <span class="p">[</span><span class="n">prepped_data</span><span class="p">],</span>
<span class="c1"># pass reference to estimator script</span>
<span class="n">estimator_entry_script_arguments</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'--folder'</span><span class="p">,</span> <span class="n">prepped_data</span><span class="p">]</span>
<span class="p">)</span>
</code></pre></div>
<p>Inside the script, you can get reference to <code>PipelineData</code> object from the argument, and use it like a local folder.</p>
<div class="highlight"><pre><span></span><code><span class="n">parser</span> <span class="o">=</span> <span class="n">argpare</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'--folder'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'folder'</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="n">output_folder</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">folder</span>
<span class="c1"># ...</span>
<span class="c1"># save data to PipelineData location</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">output_folder</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">output_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output_folder</span><span class="p">,</span> <span class="s1">'prepped_data.csv'</span><span class="p">)</span>
<span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">output_path</span><span class="p">)</span>
</code></pre></div>
<h2>Reuse steps</h2>
<p>By default, the step output from a previous pipeline run is reused without rerunning the step (if script, source directory and other params have not changed). You can control this:</p>
<div class="highlight"><pre><span></span><code><span class="n">step1</span> <span class="o">=</span> <span class="n">PythonScriptStep</span><span class="p">(</span>
<span class="c1">#...</span>
<span class="n">allow_reuse</span><span class="o">=</span><span class="kc">False</span>
<span class="p">)</span>
</code></pre></div>
<p>You can force the steps to run regardless of individual configuration:</p>
<div class="highlight"><pre><span></span><code><span class="n">pipeline_run</span> <span class="o">=</span> <span class="n">experiment</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">train_pipeline</span><span class="p">,</span> <span class="n">regenerate_outputs</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre></div>
<h2>Publish pipelines</h2>
<p>You can publish a pipelien to create a REST endpoint through which the pipeline can be run on demand.</p>
<div class="highlight"><pre><span></span><code><span class="n">published_pipeline</span> <span class="o">=</span> <span class="n">pipeline</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'training_pipeline'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'Model training pipeline'</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s1">'1.0'</span>
<span class="p">)</span>
</code></pre></div>
<p>You can view it in ML Studio and get the endpoint:</p>
<div class="highlight"><pre><span></span><code><span class="n">published_pipeline</span><span class="o">.</span><span class="n">endpoint</span>
</code></pre></div>
<p>You start a published endpoint by making an HTTP request to it. You pass the authorisation header (with token) and a JSON payload specifying the experiment name. The pipeline is run asynchronously, you get the run ID as response.</p>
<h2>Pipeline parameters</h2>
<p>Create a <code>PipelineParameter</code> object for each parameter. Example:</p>
<div class="highlight"><pre><span></span><code><span class="n">reg_param</span> <span class="o">=</span> <span class="n">PipelineParameter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'reg_rate'</span><span class="p">,</span> <span class="n">default_value</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>
<span class="c1"># ...</span>
<span class="n">step2</span> <span class="o">=</span> <span class="n">EstimatorStep</span><span class="p">(</span>
<span class="c1"># ...</span>
<span class="n">estimator_entry_script_arguments</span><span class="o">=</span><span class="p">[</span>
<span class="s1">'--folder'</span><span class="p">,</span> <span class="n">prepped</span><span class="p">,</span>
<span class="s1">'--reg'</span><span class="p">,</span> <span class="n">reg_param</span>
<span class="p">]</span>
<span class="p">)</span>
</code></pre></div>
<p>After you publish a parametrised pipeline, you can pass parameter values in the JSON payload of the REST interface. Example</p>
<div class="highlight"><pre><span></span><code><span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span>
<span class="n">enpoint</span><span class="p">,</span>
<span class="n">headers</span><span class="o">=</span><span class="n">auth_header</span><span class="p">,</span>
<span class="n">json</span><span class="o">=</span><span class="p">{</span>
<span class="s1">'ExperimentName'</span><span class="p">:</span> <span class="s1">'run_training_pipeline'</span><span class="p">,</span>
<span class="s1">'ParameterAssignments'</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">'reg_rate'</span><span class="p">:</span> <span class="mf">0.1</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">)</span>
</code></pre></div>
<h2>Schedule pipelines</h2>
<p>Define a <code>ScheduleRecurrence</code> and use it to create a <code>Schedule</code>.</p>
<div class="highlight"><pre><span></span><code><span class="n">daily</span> <span class="o">=</span> <span class="n">ScheduleRecurrence</span><span class="p">(</span>
<span class="n">frequency</span><span class="o">=</span><span class="s1">'Day'</span><span class="p">,</span>
<span class="n">interval</span><span class="o">=</span><span class="mi">1</span>
<span class="p">)</span>
<span class="n">pipeline_schedule</span> <span class="o">=</span> <span class="n">Schedule</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="n">ws</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'Daily Training'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'train model every day'</span><span class="p">,</span>
<span class="n">pipeline_id</span><span class="o">=</span><span class="n">published_pipeline</span><span class="o">.</span><span class="n">id</span><span class="p">,</span>
<span class="n">experiment_name</span><span class="o">=</span><span class="s1">'Training_Pipeline'</span><span class="p">,</span>
<span class="n">recurrence</span><span class="o">=</span><span class="n">daily</span>
<span class="p">)</span>
</code></pre></div>
<p>To schedule a pipeline to run whenever <strong>data changes</strong>, you must create a <code>Schedule</code> that monitors a specific path on a datastore:</p>
<div class="highlight"><pre><span></span><code><span class="n">training_datastore</span> <span class="o">=</span> <span class="n">Datastore</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'blob_data'</span><span class="p">)</span>
<span class="n">pipeline_schedule</span> <span class="o">=</span> <span class="n">Schedule</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="c1"># ...</span>
<span class="n">datastore</span><span class="o">=</span><span class="n">training_datastore</span><span class="p">,</span>
<span class="n">path_on_datastore</span><span class="o">=</span><span class="s1">'data/training'</span>
<span class="p">)</span>
</code></pre></div>
<h1>Deploy ML Models</h1>
<p>You can deploy ass <strong>container</strong> to several compute targets</p>
<ul>
<li>Azure ML compute instance</li>
<li>Azure container instance</li>
<li>Azure function</li>
<li>Azure Kubernetes service</li>
<li>IoT module</li>
</ul>
<p>Steps</p>
<ol>
<li>register the model</li>
<li>inference configuration</li>
<li>deployment configuration</li>
<li>deploy model</li>
</ol>
<h2><a name="registermodel"></a>Register the model</h2>
<p>After training, you must register the model to Azure ML workspace.</p>
<div class="highlight"><pre><span></span><code><span class="n">classification_model</span> <span class="o">=</span> <span class="n">Model</span><span class="o">.</span><span class="n">register</span><span class="p">(</span>
<span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span>
<span class="n">model_name</span><span class="o">=</span><span class="s1">'classification_model'</span><span class="p">,</span>
<span class="n">model_path</span><span class="o">=</span><span class="s1">'model.pkl'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'A classification model'</span>
<span class="p">)</span>
</code></pre></div>
<p>Or you can use the reference to the run:</p>
<div class="highlight"><pre><span></span><code><span class="n">run</span><span class="o">.</span><span class="n">register_model</span><span class="p">(</span>
<span class="n">model_name</span><span class="o">=</span><span class="s1">'classification_model'</span><span class="p">,</span>
<span class="n">model_path</span><span class="o">=</span><span class="s1">'outputs/model.pkl'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'A classification model'</span>
<span class="p">)</span>
</code></pre></div>
<h2><a name="scoringscript"></a>Inference configuration</h2>
<p>The model will be deployed as a service consisting of</p>
<ul>
<li>a script to load the model and return predictions for submitted data</li>
<li>an environment in which the script will be run</li>
</ul>
<p>Create the <em>entry script</em> (or <em>scoring script</em>) as a Python file including 2 functions</p>
<ul>
<li><code>init()</code> called when service is initialised (load model from registry)</li>
<li><code>run(raw_data)</code> called when new data is submitted to the service (generate predictions)</li>
</ul>
<p>Example</p>
<div class="highlight"><pre><span></span><code><span class="k">def</span> <span class="nf">init</span><span class="p">():</span>
<span class="k">global</span> <span class="n">model</span>
<span class="n">model_path</span> <span class="o">=</span> <span class="n">Model</span><span class="o">.</span><span class="n">get_model_path</span><span class="p">(</span><span class="s1">'classification_model'</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">joblib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">model_path</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="n">raw_data</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">raw_data</span><span class="p">)[</span><span class="s1">'data'</span><span class="p">])</span>
<span class="n">predictions</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="c1"># return predictions as any JSON seriazable format</span>
<span class="k">return</span> <span class="n">predictions</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
</code></pre></div>
<p>You can configure the environment using Conda. You can use a <code>CondaDependencies</code> class to create a default environment (including <code>azureml-defaults</code> and other commonly-used) and add any other required packages. You then serialize the environment to a string and save it.</p>
<div class="highlight"><pre><span></span><code><span class="n">myenv</span> <span class="o">=</span> <span class="n">CondaDependencies</span><span class="p">()</span>
<span class="n">myenv</span><span class="o">.</span><span class="n">add_conda_package</span><span class="p">(</span><span class="s1">'scikit-learn'</span><span class="p">)</span>
<span class="n">env_file</span> <span class="o">=</span> <span class="s1">'service_files/env.yml'</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">env_file</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">myenv</span><span class="o">.</span><span class="n">serialize_to_string</span><span class="p">())</span>
</code></pre></div>
<p>After creating the script and the environment, you combine them in an <code>InferenceConfig</code>:</p>
<div class="highlight"><pre><span></span><code><span class="n">classifier_inference_config</span> <span class="o">=</span> <span class="n">InferenceConfig</span><span class="p">(</span>
<span class="n">runtime</span><span class="o">=</span><span class="s1">'python'</span><span class="p">,</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'service_files'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'score.py'</span><span class="p">,</span>
<span class="n">conda_file</span><span class="o">=</span><span class="s1">'env.yml'</span>
<span class="p">)</span>
</code></pre></div>
<h2>Deployment configuration</h2>
<p>Now that you have the entry script and the environment, you configure the compute service. If you deploy to an AKS cluster, you create it</p>
<div class="highlight"><pre><span></span><code><span class="n">cluster_name</span> <span class="o">=</span> <span class="s1">'aks-cluster'</span>
<span class="n">compute_config</span> <span class="o">=</span> <span class="n">AksCompute</span><span class="o">.</span><span class="n">provisioning_configuration</span><span class="p">(</span><span class="n">location</span><span class="o">=</span><span class="s1">'eastus'</span><span class="p">)</span>
<span class="n">production_cluster</span> <span class="o">=</span> <span class="n">ComputeTarget</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">ws</span><span class="p">,</span> <span class="n">cluster_name</span><span class="p">,</span> <span class="n">compute_config</span><span class="p">)</span>
<span class="n">production_cluster</span><span class="o">.</span><span class="n">wait_for_completion</span><span class="p">()</span>
</code></pre></div>
<p>You define the deployment configuration</p>
<div class="highlight"><pre><span></span><code><span class="n">classifier_deploy_config</span> <span class="o">=</span> <span class="n">AksWebservice</span><span class="o">.</span><span class="n">deploy_configuration</span><span class="p">(</span>
<span class="n">cpu_cores</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">memory_gb</span><span class="o">=</span><span class="mi">1</span>
<span class="p">)</span>
</code></pre></div>
<h2>Deploy the model</h2>
<div class="highlight"><pre><span></span><code><span class="n">model</span> <span class="o">=</span> <span class="n">ws</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="s1">'classification_model'</span><span class="p">]</span>
<span class="n">service</span> <span class="o">=</span> <span class="n">Model</span><span class="o">.</span><span class="n">deploy</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'classification-service'</span><span class="p">,</span>
<span class="n">models</span><span class="o">=</span><span class="p">[</span><span class="n">model</span><span class="p">],</span>
<span class="n">inference_config</span><span class="o">=</span><span class="n">classifier_inference_config</span><span class="p">,</span>
<span class="n">deploy_config</span><span class="o">=</span><span class="n">classifier_deploy_config</span><span class="p">,</span>
<span class="n">deployment_target</span><span class="o">=</span><span class="n">production_cluster</span>
<span class="p">)</span>
<span class="n">service</span><span class="o">.</span><span class="n">wait_for_deployment</span><span class="p">()</span>
</code></pre></div>
<h2>Consuming a real-time inferencing service</h2>
<p>For <strong>testing</strong>, you can use the AML SDK to call a web service through the <code>run</code> method of a <code>WebService</code> object. Typically, you send data to <code>run</code> method in a JSON like</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="s1">'data'</span><span class="p">:[</span>
<span class="p">[</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">3.4</span><span class="p">],</span>
<span class="p">[</span><span class="mf">0.9</span><span class="p">,</span> <span class="mf">8.2</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">],</span>
<span class="o">...</span>
<span class="p">]</span>
<span class="p">}</span>
</code></pre></div>
<p>The response is a JSON with a prediction for each case</p>
<div class="highlight"><pre><span></span><code><span class="n">response</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">input_data</span><span class="o">=</span><span class="n">json_data</span><span class="p">)</span>
<span class="n">predictions</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
</code></pre></div>
<p>In <strong>production</strong>, you use a REST endpoint. You find the endpoint of a deployed service in Azure ML studio, or by retrieving the <code>scoring_url</code> property of a <code>Webservice</code> object:</p>
<div class="highlight"><pre><span></span><code><span class="n">endpoint</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="n">scoring_uri</span>
</code></pre></div>
<p>There are 2 kinds of <strong>authentication</strong>:</p>
<ul>
<li>key: requests are authenticated by specifying the key associated with the service</li>
<li>token: requests are authenticated by providing a JSON Web Token (JWT)</li>
</ul>
<p>By default, authentication is disabled for Azure Container Instance service (set to key-based authentication for AKS).</p>
<p>To make an authenticate call to the REST endpoint, you include the oey or the token in the request header.</p>
<h2>Troubleshooting service deployment</h2>
<p>You can</p>
<ul>
<li>check the service state (should be <em>healty</em>): <code>service.state</code></li>
<li>review service logs: <code>service.get_logs()</code></li>
<li>deploy to local container</li>
</ul>
<h1>Batch inference pipelines</h1>
<p>Pipeline to read input data, load a registered model, predict labels, and write results.</p>
<ol>
<li><a href="#registermodel">Register</a> a model</li>
<li>Create a <a href="#scoringscript">scoring script</a>. The <code>run(mini_batch)</code> method makes the inference on each batch.</li>
<li>Create a pipeline with ParallelRunStep</li>
<li>Run the pipeline and retrieve the step output</li>
</ol>
<p>Azure ML provides a pipeline step performs parallel batch inference. Using <code>ParallelRunStep</code> class, you can read batches of files from a <code>File</code> dataset and write the output to a <code>PipelineData</code> reference. You can set the <code>output_action</code> to <em>"append_row"</em> (ensuring all instances of the step will collate the result to a single output file named <code>parallel_run_step.txt</code>).</p>
<div class="highlight"><pre><span></span><code><span class="n">batch_data_set</span> <span class="o">=</span> <span class="n">ws</span><span class="o">.</span><span class="n">datasets</span><span class="p">(</span><span class="s1">'batch-data'</span><span class="p">)</span>
<span class="c1"># output location</span>
<span class="n">default_ds</span> <span class="o">=</span> <span class="n">we</span><span class="o">.</span><span class="n">get_default_datastore</span><span class="p">()</span>
<span class="n">output_dir</span> <span class="o">=</span> <span class="n">PipelineData</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'inferences'</span><span class="p">,</span>
<span class="n">datastore</span><span class="o">=</span><span class="n">default_ds</span><span class="p">,</span>
<span class="n">output_path_on_compute</span><span class="o">=</span><span class="s1">'results'</span>
<span class="p">)</span>
<span class="n">parallel_run_config</span> <span class="o">=</span> <span class="n">ParallelRunConfig</span><span class="p">(</span>
<span class="n">source_directory</span><span class="o">=</span><span class="s1">'batch_scripts'</span><span class="p">,</span>
<span class="n">entry_script</span><span class="o">=</span><span class="s1">'batch_scoring_script.py'</span><span class="p">,</span>
<span class="n">mini_batch_size</span><span class="o">=</span><span class="s2">"5"</span><span class="p">,</span>
<span class="n">error_threshold</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">output_action</span><span class="o">=</span><span class="s2">"append_row"</span><span class="p">,</span>
<span class="n">environment</span><span class="o">=</span><span class="n">batch_env</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="n">aml_cluster</span><span class="p">,</span>
<span class="n">node_count</span><span class="o">=</span><span class="mi">4</span>
<span class="p">)</span>
<span class="n">parallelrun_step</span> <span class="o">=</span> <span class="n">ParallelRunStep</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s2">"batch-score"</span><span class="p">,</span>
<span class="n">parallel_run_config</span><span class="o">=</span><span class="n">parallel_run_config</span><span class="p">,</span>
<span class="n">inputs</span><span class="o">=</span><span class="p">[</span><span class="n">batch_data_set</span><span class="o">.</span><span class="n">as_named_input</span><span class="p">(</span><span class="s1">'batch_data'</span><span class="p">)],</span>
<span class="n">output</span><span class="o">=</span><span class="n">output_dir</span><span class="p">,</span>
<span class="n">arguments</span><span class="o">=</span><span class="p">[],</span>
<span class="n">allow_reuse</span><span class="o">=</span><span class="kc">True</span>
<span class="p">)</span>
<span class="n">pipeline</span> <span class="o">=</span> <span class="n">Pipeline</span><span class="p">(</span>
<span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span>
<span class="n">steps</span><span class="o">=</span><span class="p">[</span><span class="n">parallelrun_step</span><span class="p">]</span>
<span class="p">)</span>
</code></pre></div>
<p>Run the pipeline and retrieve output.</p>
<div class="highlight"><pre><span></span><code><span class="n">pipeline_run</span> <span class="o">=</span> <span class="n">Experiment</span><span class="p">(</span><span class="n">ws</span><span class="p">,</span> <span class="s1">'batch_prediction_pipeline'</span><span class="p">)</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">pipeline</span><span class="p">)</span>
<span class="n">pipeline_run</span><span class="o">.</span><span class="n">wait_for_completion</span><span class="p">()</span>
<span class="n">prediction_run</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">pipeline_run</span><span class="o">.</span><span class="n">get_children</span><span class="p">())</span>
<span class="n">prediction_output</span> <span class="o">=</span> <span class="n">prediction_run</span><span class="o">.</span><span class="n">get_output_data</span><span class="p">(</span><span class="s1">'inferences'</span><span class="p">)</span>
<span class="n">prediction_output</span><span class="o">.</span><span class="n">download</span><span class="p">(</span><span class="n">local_path</span><span class="o">=</span><span class="s1">'results'</span><span class="p">)</span>
</code></pre></div>
<h2>Publishing a batch inference pipeline</h2>
<p>You can publish it as a <strong>REST</strong> service.</p>
<div class="highlight"><pre><span></span><code><span class="n">published_pipeline</span> <span class="o">=</span> <span class="n">pipeline_run</span><span class="o">.</span><span class="n">publish_pipeline</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'Batch_Prediction_Pipeline'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'Batch Pipeline'</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s1">'1.0'</span>
<span class="p">)</span>
<span class="n">rest_endpoint</span> <span class="o">=</span> <span class="n">published_pipeline</span><span class="o">.</span><span class="n">endpoint</span>
</code></pre></div>
<p>Once published, you can use the endpoint to initiate a batch inferencing job.</p>
<p>You can also <strong>schedule</strong> the published pipeline to have it run automatically.</p>
<div class="highlight"><pre><span></span><code><span class="n">weekly</span> <span class="o">=</span> <span class="n">ScheduleRecurrence</span><span class="p">(</span><span class="n">frequency</span><span class="o">=</span><span class="s1">'Week'</span><span class="p">,</span> <span class="n">interval</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">pipeline_schedule</span> <span class="o">=</span> <span class="n">Schedule</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="n">ws</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'Weekly Predictions'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'batch inferencing'</span><span class="p">,</span>
<span class="n">pipeline_id</span><span class="o">=</span><span class="n">published_pipeline</span><span class="o">.</span><span class="n">id</span><span class="p">,</span>
<span class="n">experiment_name</span><span class="o">=</span><span class="s1">'Batch_Prediction'</span><span class="p">,</span>
<span class="n">recurrence</span><span class="o">=</span><span class="n">weekly</span>
<span class="p">)</span>
</code></pre></div>
<h1>Tuning hyperparameters</h1>
<p>Accomplished by training multiple models, using same algorithm and training data but different hyperparameter values. Then, evaluate for each the performance metric (eg accuracy), and the best-performing model is selected.</p>
<p>In Azure ML, you make an experiment that consist of a <em>hyperdrive</em> run, which initiates a child run for each hyperparameter. Each child run uses a training script with parametrised hyperparameter values to train a model, and logs the target performance metric achieved by the training model.</p>
<h2>Define a search space</h2>
<p>Depends on the type of hyperparameter:</p>
<ul>
<li><strong>discrete</strong>. Make a <code>choice</code> out of</li>
<li>an explicit python <code>list</code>: <code>choice([10, 20, 30])</code></li>
<li>a <code>range</code>: <code>choice(range(1,10))</code></li>
<li>select values from a discrete distribution: <em>qnormal, quniform, qlognormal, qloguniform</em></li>
<li><strong>continuous</strong>. Use any of these distribution: <em>normal, uniform, lognormal, loguniform</em></li>
</ul>
<p>Define a search space by creating a dictionary with parameter expressions for each hyperparameter.</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">azureml.train.hyperdrive</span> <span class="kn">import</span> <span class="n">choice</span><span class="p">,</span> <span class="n">normal</span>
<span class="n">param_space</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'--batch_size'</span><span class="p">:</span> <span class="n">choice</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">64</span><span class="p">),</span>
<span class="s1">'--learning_rate'</span><span class="p">:</span> <span class="n">normal</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<h2>Configuring sampling</h2>
<p>The values used in a tuning run depend on the type of <em>sampling</em> used.</p>
<p><strong>Grid sampling.</strong> Every possible combination when hyperparameters are discrete.</p>
<div class="highlight"><pre><span></span><code><span class="n">param_space</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'--batch_size'</span><span class="p">:</span> <span class="n">choice</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">64</span><span class="p">),</span>
<span class="s1">'--learning_rate'</span><span class="p">:</span> <span class="n">choice</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">param_sampling</span> <span class="o">=</span> <span class="n">GridParameterSampling</span><span class="p">(</span><span class="n">param_space</span><span class="p">)</span>
</code></pre></div>
<p><strong>Random sampling.</strong> Randomly select a value for each hyperparameter.</p>
<div class="highlight"><pre><span></span><code><span class="n">param_space</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'--batch_size'</span><span class="p">:</span> <span class="n">choice</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">64</span><span class="p">),</span>
<span class="s1">'--learning_rate'</span><span class="p">:</span> <span class="n">normal</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">param_sampling</span> <span class="o">=</span> <span class="n">RandomParameterSampling</span><span class="p">(</span><span class="n">param_space</span><span class="p">)</span>
</code></pre></div>
<p><strong>Bayesian sampling.</strong> Based on Bayesian optimisation algorithm that tries to select parameter combinations that will result in improved performance from the previous selection.</p>
<div class="highlight"><pre><span></span><code><span class="n">param_space</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'--batch_size'</span><span class="p">:</span> <span class="n">choice</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">64</span><span class="p">),</span>
<span class="s1">'--learning_rate'</span><span class="p">:</span> <span class="n">uniform</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">param_sampling</span> <span class="o">=</span> <span class="n">BayesianParameterSampling</span><span class="p">(</span><span class="n">param_space</span><span class="p">)</span>
</code></pre></div>
<p>Can only be used with <em>choice, uniform, quniform</em> distributions and can't be combined with <em>early termination</em>.</p>
<h2>Configuring an early termination</h2>
<p>Typically, you set a maximum number of iterations, but this could still result in a large number of runs that don't result in a better model than a combination that has already been tried.</p>
<p>To help preventing wasting time, you can set an <em>early termination</em> policy that abandons runs that are unlikely to produce a better result than previously completed runs. The policy is evaluated at an <em>evaluation interval</em> you specify, based on each time the target performance metric is logged. You can also set a <em>delay evaluation</em> parameter to avoid evaluating the policy until a minimum number of iterations have been completed.</p>
<p><strong>Note.</strong> Early termination is particularly useful for deep learning scenarios where a deep neural network is trained iteratively over a number of epochs. The training script can report the target metric after each epoch, and if the run is significantly underperforming previous runs after the same number of intervals, it can be abandoned.</p>
<p><strong>Bandit policy.</strong> Stop a run if the target performance metric underperforms the best run so far by a specified margin.</p>
<div class="highlight"><pre><span></span><code><span class="n">early_termination_policy</span> <span class="o">=</span> <span class="n">BanditPolicy</span><span class="p">(</span>
<span class="n">slack_amount</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="c1"># abandon runs when metric is 0.2 or more worse than best run after the same number of intervals</span>
<span class="n">evaluation_interval</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">delay_evaluation</span><span class="o">=</span><span class="mi">5</span>
<span class="p">)</span>
</code></pre></div>
<p>You can also use a slack <em>factor</em> comparing the metric as ration rather than an absolute value.</p>
<p><strong>Median stopping policy.</strong> Abandoning runs where the target performance metric is worse than the median of the running averages fo all runs.</p>
<div class="highlight"><pre><span></span><code><span class="n">early_termination_policy</span> <span class="o">=</span> <span class="n">MedianStoppingPolicy</span><span class="p">(</span>
<span class="n">evaluation_interval</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">delay_evaluation</span><span class="o">=</span><span class="mi">5</span>
<span class="p">)</span>
</code></pre></div>
<p><strong>Truncation selection policy.</strong> Cancelling the lower performing <em>X%%</em> of runs at each evaluation interval based on the <em>truncation_percentage</em> valu you specify for <em>X</em>.</p>
<div class="highlight"><pre><span></span><code><span class="n">early_termination_policy</span> <span class="o">=</span> <span class="n">TruncationSelectionPolicy</span><span class="p">(</span>
<span class="n">truncation_percentage</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">evaluation_interval</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">delay_evaluation</span><span class="o">=</span><span class="mi">5</span>
<span class="p">)</span>
</code></pre></div>
<h2>Running a hyperparameter tuning experiment</h2>
<p>In Azure ML, you tune hyper by running a <em>hyperdrive</em> experiment. You need to create a training script just the way you would do for any other training experiment, except that you <strong>must</strong>:</p>
<ul>
<li>include an argument for each hyperparameter</li>
<li>log the target performance metric.</li>
</ul>
<p>This example script trains a logistic regression using a <code>--regularization</code> argument (regularization rate), and logs the <em>accuracy</em>.</p>
<div class="highlight"><pre><span></span><code><span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'--regularization'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'reg_rate'</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="n">reg</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">reg_rate</span>
<span class="c1"># get experiment run context</span>
<span class="n">run</span> <span class="o">=</span> <span class="n">Run</span><span class="o">.</span><span class="n">get_context</span><span class="p">()</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">run</span><span class="o">.</span><span class="n">input_datasets</span><span class="p">[</span><span class="s1">'training_data'</span><span class="p">]</span><span class="o">.</span><span class="n">to_pandas_dataframe</span><span class="p">()</span>
<span class="n">X</span> <span class="o">=</span> <span class="n">data</span><span class="p">[[</span><span class="s1">'feature1'</span><span class="p">,</span> <span class="s1">'feature2'</span><span class="p">,</span> <span class="s1">'feature3'</span><span class="p">,</span> <span class="s1">'feature4'</span><span class="p">]]</span><span class="o">.</span><span class="n">values</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">'label'</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="n">test_size</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">LogisticRegression</span><span class="p">(</span><span class="n">C</span><span class="o">=</span><span class="mi">1</span><span class="o">/</span><span class="n">reg</span><span class="p">,</span> <span class="n">solver</span><span class="o">=</span><span class="s1">'liblinear'</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
<span class="c1"># calculate and log accuracy</span>
<span class="n">y_hat</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
<span class="n">acc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">average</span><span class="p">(</span><span class="n">y_hat</span> <span class="o">==</span> <span class="n">y_test</span><span class="p">)</span>
<span class="n">run</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">'Accuracy'</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">float</span><span class="p">(</span><span class="n">acc</span><span class="p">))</span>
<span class="c1"># save trained model</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s1">'outputs'</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">joblib</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="n">model</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'outputs/model.pkl'</span><span class="p">)</span>
<span class="n">run</span><span class="o">.</span><span class="n">complete</span><span class="p">()</span>
</code></pre></div>
<p>To prepare the hyperdrive experiment, you use a <code>HyperDriveConfig</code> object to configure the experiment run.</p>
<div class="highlight"><pre><span></span><code><span class="n">hyperdrive</span> <span class="o">=</span> <span class="n">HyperDriveConfig</span><span class="p">(</span>
<span class="n">estimator</span><span class="o">=</span><span class="n">sklearn_estimator</span><span class="p">,</span>
<span class="n">hyperparameter_sampling</span><span class="o">=</span><span class="n">param_sampling</span><span class="p">,</span>
<span class="n">policy</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">primary_metric_name</span><span class="o">=</span><span class="s1">'Accuracy'</span><span class="p">,</span>
<span class="n">primary_metricgoal</span><span class="o">=</span><span class="n">PrimaryMetricGoal</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">,</span>
<span class="n">max_total_runs</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
<span class="n">max_concurrent_runs</span><span class="o">=</span><span class="mi">4</span>
<span class="p">)</span>
<span class="n">experiment</span> <span class="o">=</span> <span class="n">Experiment</span><span class="p">(</span><span class="n">workspace</span><span class="o">=</span><span class="n">ws</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'hyperdrive_training'</span><span class="p">)</span>
<span class="n">hyperdrive_run</span> <span class="o">=</span> <span class="n">experiment</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">config</span><span class="o">=</span><span class="n">hyperdrive</span><span class="p">)</span>
</code></pre></div>
<p>You can monitor hyperdrive experiment in Azure ML studio. The experiment will initiate a child run for each hyperparameter combination to be tried</p>
<h1>Automate model selection</h1>
<p>Visual interface for automated ML in Azure ML Studio for <em>Enterprise</em> edition only.</p>
<p>You can use automated ML to train models for the tasks below. Azure ML supports common algorithms for these tasks:</p>
<ul>
<li>classification</li>
<li>logistic regression</li>
<li>light gradient boosting machine</li>
<li>decision tree</li>
<li>random forest</li>
<li>naive Bayes</li>
<li>linear SVM</li>
<li>XGBoost</li>
<li>DNN classifier</li>
<li>others...</li>
<li>regression</li>
<li>linear regression</li>
<li>light gradient boosting machine</li>
<li>decision tree</li>
<li>random forest</li>
<li>elastic net</li>
<li>LARS Lasso</li>
<li>XGBoost</li>
<li>Others</li>
<li>time series forecasting</li>
<li>linear regression</li>
<li>light gradient boosting machine</li>
<li>decision tree</li>
<li>random forest</li>
<li>elastic net</li>
<li>LARS Lasso</li>
<li>XGBoost</li>
<li>others</li>
</ul>
<p>By default, automated machine learning, will randomly select from the full range of algorithms for the specified task. You can choose to <strong>block</strong> individual algorithms from being selected.</p>
<h2>Preprocessing and featurization</h2>
<p>Automated ML (AutoML) can apply preprocessing transformations to your data.</p>
<ul>
<li><strong>scaling and normalization</strong> applied to numeric data <strong>automatically</strong></li>
<li><strong>optional featurization</strong></li>
<li>missing value imputation</li>
<li>categorical encoding</li>
<li>dropping high cardinality features (eg IDs)</li>
<li>feature engineering (eg date parts from DateTime)</li>
</ul>
<h2>Running AutoML experiment</h2>
<p>You can use Auzure ML Studio UI or use SDK (using <code>AutoMLConfig</code> class).</p>
<div class="highlight"><pre><span></span><code><span class="n">automl_run_config</span> <span class="o">=</span> <span class="n">RunConfiguration</span><span class="p">(</span><span class="n">framework</span><span class="o">=</span><span class="s1">'python'</span><span class="p">)</span>
<span class="n">automl_config</span> <span class="o">=</span> <span class="n">AutoMLConfig</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'auto ml experiment'</span><span class="p">,</span>
<span class="n">task</span><span class="o">=</span><span class="s1">'classification'</span><span class="p">,</span>
<span class="n">primary_metric</span><span class="o">=</span><span class="s1">'AUC_weighted'</span><span class="p">,</span>
<span class="n">compute_target</span><span class="o">=</span><span class="n">aml_compute</span><span class="p">,</span>
<span class="n">training_data</span><span class="o">=</span><span class="n">train_dataset</span><span class="p">,</span>
<span class="n">validation_data</span><span class="o">=</span><span class="n">test_dataset</span><span class="p">,</span>
<span class="n">label_column_name</span><span class="o">=</span><span class="s1">'label'</span><span class="p">,</span>
<span class="n">featurization</span><span class="o">=</span><span class="s1">'auto'</span><span class="p">,</span>
<span class="n">iterations</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span>
<span class="n">max_concurrent_iterations</span><span class="o">=</span><span class="mi">4</span>
<span class="p">)</span>
</code></pre></div>
<p>With Azure ML Studio, you can create or select an Azure ML <em>dataset</em> to be used as input for your AutoML experiment. When using the SDK, you can submit data by</p>
<ul>
<li>specify a dataset or dataframe of <em>training data</em> that includes features and label to be predicted</li>
<li>optionally, specify a second <em>validation data</em> dataset or dataframe. If this is not provided, Azure ML will apply cross-validation.</li>
</ul>
<p>Alternatively:</p>
<ul>
<li>specify a dataset, dataframe, or numpy array of <em>X</em> values containing features with a corresponding <em>y</em> array of label values</li>
</ul>
<p>One of the most important setting you specify is <strong>primary_metric</strong> (ie target performance metric). Azure ML supports a set of named metrics for each type of task.</p>
<div class="highlight"><pre><span></span><code><span class="n">get_primary_metrics</span><span class="p">(</span><span class="s1">'classification'</span><span class="p">)</span>