1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "metadata" : {},
6+ "source" : [
7+ " Copyright (c) Microsoft Corporation. All rights reserved.\n " ,
8+ " \n " ,
9+ " Licensed under the MIT License."
10+ ]
11+ },
12+ {
13+ "cell_type" : " markdown" ,
14+ "metadata" : {},
15+ "source" : [
16+ " # 06. Logging APIs\n " ,
17+ " This notebook showcase various ways to use the Azure Machine Learning service run logging APIs, and view the results in the Azure portal."
18+ ]
19+ },
20+ {
21+ "cell_type" : " markdown" ,
22+ "metadata" : {},
23+ "source" : [
24+ " ## Prerequisites\n " ,
25+ " Make sure you go through the [00. Installation and Configuration](../../00.configuration.ipynb) Notebook first if you haven't. Also make sure you have tqdm and matplotlib installed in the current kernel.\n " ,
26+ " \n " ,
27+ " ```\n " ,
28+ " (myenv) $ conda install -y tqdm matplotlib\n " ,
29+ " ```"
30+ ]
31+ },
32+ {
33+ "cell_type" : " markdown" ,
34+ "metadata" : {},
35+ "source" : [
36+ " ## Validate Azure ML SDK installation and get version number for debugging purposes"
37+ ]
38+ },
39+ {
40+ "cell_type" : " code" ,
41+ "execution_count" : null ,
42+ "metadata" : {
43+ "tags" : [
44+ " install"
45+ ]
46+ },
47+ "outputs" : [],
48+ "source" : [
49+ " from azureml.core import Experiment, Run, Workspace\n " ,
50+ " import azureml.core\n " ,
51+ " import numpy as np\n " ,
52+ " \n " ,
53+ " # Check core SDK version number\n " ,
54+ " print(\" SDK version:\" , azureml.core.VERSION)"
55+ ]
56+ },
57+ {
58+ "cell_type" : " markdown" ,
59+ "metadata" : {},
60+ "source" : [
61+ " ## Initialize Workspace\n " ,
62+ " \n " ,
63+ " Initialize a workspace object from persisted configuration."
64+ ]
65+ },
66+ {
67+ "cell_type" : " code" ,
68+ "execution_count" : null ,
69+ "metadata" : {
70+ "tags" : [
71+ " create workspace"
72+ ]
73+ },
74+ "outputs" : [],
75+ "source" : [
76+ " ws = Workspace.from_config()\n " ,
77+ " print('Workspace name: ' + ws.name, \n " ,
78+ " 'Azure region: ' + ws.location, \n " ,
79+ " 'Subscription id: ' + ws.subscription_id, \n " ,
80+ " 'Resource group: ' + ws.resource_group, sep='\\ n')"
81+ ]
82+ },
83+ {
84+ "cell_type" : " markdown" ,
85+ "metadata" : {},
86+ "source" : [
87+ " ## Set experiment\n " ,
88+ " Create a new experiment (or get the one with such name)."
89+ ]
90+ },
91+ {
92+ "cell_type" : " code" ,
93+ "execution_count" : null ,
94+ "metadata" : {},
95+ "outputs" : [],
96+ "source" : [
97+ " exp = Experiment(workspace=ws, name='logging-api-test')"
98+ ]
99+ },
100+ {
101+ "cell_type" : " markdown" ,
102+ "metadata" : {},
103+ "source" : [
104+ " ## Log metrics\n " ,
105+ " We will start a run, and use the various logging APIs to record different types of metrics during the run."
106+ ]
107+ },
108+ {
109+ "cell_type" : " code" ,
110+ "execution_count" : null ,
111+ "metadata" : {},
112+ "outputs" : [],
113+ "source" : [
114+ " from tqdm import tqdm\n " ,
115+ " \n " ,
116+ " # start logging for the run\n " ,
117+ " run = exp.start_logging()\n " ,
118+ " \n " ,
119+ " # log a string value\n " ,
120+ " run.log(name='Name', value='Logging API run')\n " ,
121+ " \n " ,
122+ " # log a numerical value\n " ,
123+ " run.log(name='Magic Number', value=42)\n " ,
124+ " \n " ,
125+ " # Log a list of values. Note this will generate a single-variable line chart.\n " ,
126+ " run.log_list(name='Fibonacci', value=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89])\n " ,
127+ " \n " ,
128+ " # create a dictionary to hold a table of values\n " ,
129+ " sines = {}\n " ,
130+ " sines['angle'] = []\n " ,
131+ " sines['sine'] = []\n " ,
132+ " \n " ,
133+ " for i in tqdm(range(-10, 10)):\n " ,
134+ " # log a metric value repeatedly, this will generate a single-variable line chart.\n " ,
135+ " run.log(name='Sigmoid', value=1 / (1 + np.exp(-i)))\n " ,
136+ " angle = i / 2.0\n " ,
137+ " \n " ,
138+ " # log a 2 (or more) values as a metric repeatedly. This will generate a 2-variable line chart if you have 2 numerical columns.\n " ,
139+ " run.log_row(name='Cosine Wave', angle=angle, cos=np.cos(angle))\n " ,
140+ " \n " ,
141+ " sines['angle'].append(angle)\n " ,
142+ " sines['sine'].append(np.sin(angle))\n " ,
143+ " \n " ,
144+ " # log a dictionary as a table, this will generate a 2-variable chart if you have 2 numerical columns\n " ,
145+ " run.log_table(name='Sine Wave', value=sines)\n " ,
146+ " \n " ,
147+ " run.complete()"
148+ ]
149+ },
150+ {
151+ "cell_type" : " markdown" ,
152+ "metadata" : {},
153+ "source" : [
154+ " Even after the run is marked completed, you can still log things."
155+ ]
156+ },
157+ {
158+ "cell_type" : " markdown" ,
159+ "metadata" : {},
160+ "source" : [
161+ " ## Log an image\n " ,
162+ " This is how to log a _matplotlib_ pyplot object."
163+ ]
164+ },
165+ {
166+ "cell_type" : " code" ,
167+ "execution_count" : null ,
168+ "metadata" : {},
169+ "outputs" : [],
170+ "source" : [
171+ " %matplotlib inline\n " ,
172+ " import matplotlib.pyplot as plt\n " ,
173+ " angle = np.linspace(-3, 3, 50)\n " ,
174+ " plt.plot(angle, np.tanh(angle), label='tanh')\n " ,
175+ " plt.legend(fontsize=12)\n " ,
176+ " plt.title('Hyperbolic Tangent', fontsize=16)\n " ,
177+ " plt.grid(True)\n " ,
178+ " \n " ,
179+ " run.log_image(name='Hyperbolic Tangent', plot=plt)"
180+ ]
181+ },
182+ {
183+ "cell_type" : " markdown" ,
184+ "metadata" : {},
185+ "source" : [
186+ " ## Upload a file"
187+ ]
188+ },
189+ {
190+ "cell_type" : " markdown" ,
191+ "metadata" : {},
192+ "source" : [
193+ " You can also upload an abitrary file. First, let's create a dummy file locally."
194+ ]
195+ },
196+ {
197+ "cell_type" : " code" ,
198+ "execution_count" : null ,
199+ "metadata" : {},
200+ "outputs" : [],
201+ "source" : [
202+ " %%writefile myfile.txt\n " ,
203+ " \n " ,
204+ " This is a dummy file."
205+ ]
206+ },
207+ {
208+ "cell_type" : " markdown" ,
209+ "metadata" : {},
210+ "source" : [
211+ " Now let's upload this file into the run record as a run artifact, and display the properties after the upload."
212+ ]
213+ },
214+ {
215+ "cell_type" : " code" ,
216+ "execution_count" : null ,
217+ "metadata" : {},
218+ "outputs" : [],
219+ "source" : [
220+ " props = run.upload_file(name='myfile_in_the_cloud.txt', path_or_stream='./myfile.txt')\n " ,
221+ " props.serialize()"
222+ ]
223+ },
224+ {
225+ "cell_type" : " markdown" ,
226+ "metadata" : {},
227+ "source" : [
228+ " ## Examine the run"
229+ ]
230+ },
231+ {
232+ "cell_type" : " markdown" ,
233+ "metadata" : {},
234+ "source" : [
235+ " Now let's take a look at the run detail page in Azure portal. Make sure you checkout the various charts and plots generated/uploaded."
236+ ]
237+ },
238+ {
239+ "cell_type" : " code" ,
240+ "execution_count" : null ,
241+ "metadata" : {},
242+ "outputs" : [],
243+ "source" : [
244+ " run"
245+ ]
246+ },
247+ {
248+ "cell_type" : " markdown" ,
249+ "metadata" : {},
250+ "source" : [
251+ " You can get all the metrics in that run back."
252+ ]
253+ },
254+ {
255+ "cell_type" : " code" ,
256+ "execution_count" : null ,
257+ "metadata" : {},
258+ "outputs" : [],
259+ "source" : [
260+ " run.get_metrics()"
261+ ]
262+ },
263+ {
264+ "cell_type" : " markdown" ,
265+ "metadata" : {},
266+ "source" : [
267+ " You can also see the files uploaded for this run."
268+ ]
269+ },
270+ {
271+ "cell_type" : " code" ,
272+ "execution_count" : null ,
273+ "metadata" : {},
274+ "outputs" : [],
275+ "source" : [
276+ " run.get_file_names()"
277+ ]
278+ },
279+ {
280+ "cell_type" : " markdown" ,
281+ "metadata" : {},
282+ "source" : [
283+ " You can also download all the files locally."
284+ ]
285+ },
286+ {
287+ "cell_type" : " code" ,
288+ "execution_count" : null ,
289+ "metadata" : {},
290+ "outputs" : [],
291+ "source" : [
292+ " import os\n " ,
293+ " os.makedirs('files', exist_ok=True)\n " ,
294+ " \n " ,
295+ " for f in run.get_file_names():\n " ,
296+ " dest = os.path.join('files', f.split('/')[-1])\n " ,
297+ " print('Downloading file {} to {}...'.format(f, dest))\n " ,
298+ " run.download_file(f, dest) "
299+ ]
300+ }
301+ ],
302+ "metadata" : {
303+ "authors" : [
304+ {
305+ "name" : " haining"
306+ }
307+ ],
308+ "kernelspec" : {
309+ "display_name" : " Python 3.6" ,
310+ "language" : " python" ,
311+ "name" : " python36"
312+ },
313+ "language_info" : {
314+ "codemirror_mode" : {
315+ "name" : " ipython" ,
316+ "version" : 3
317+ },
318+ "file_extension" : " .py" ,
319+ "mimetype" : " text/x-python" ,
320+ "name" : " python" ,
321+ "nbconvert_exporter" : " python" ,
322+ "pygments_lexer" : " ipython3" ,
323+ "version" : " 3.6.6"
324+ }
325+ },
326+ "nbformat" : 4 ,
327+ "nbformat_minor" : 2
328+ }
0 commit comments