-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_and_ml_tools.html
284 lines (258 loc) · 9.37 KB
/
python_and_ml_tools.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
<!DOCTYPE html>
<html lang="en">
<title>Tools and tenets for ML and Python</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script src="custom_themes/html_elements.js"></script>
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="custom_themes/sussex2.css" id="theme">
<div class="reveal">
<div class="slides">
<div id="background-template">
<footer>
<p>Tools and tenets for ML and Python
</footer>
</div>
<section class="dark-cyan">
<p>
<h2>Tools and tenets<br>for ML and Python</h2>
<p>
<p style="color: white">Predictive Analytics Lab<br>
University of Sussex
<p>
<p><img src="images/logos/University_of_Sussex_Logo.svg2000_white.png" style="width: 5rem;">
</section>
<section>
<h2>Contents</h2>
<p>Approach
<p>Python tools
<p>ML tools
<p>Further recommendations
</section>
<section>
<h3>Contents</h3>
<multi-col>
<one-col>
<h4>Core tenets</h4>
<ul>
<li>Libraries > Snippets
<li>Automation > Manual
</ul>
<h4>Python Tools</h4>
<ul>
<li>Poetry
<li>Hydra
<li>Ray
</ul>
</one-col>
<one-col>
<h4>ML Tools</h4>
<ul>
<li>PyTorch Lightning
<li>Weights & Biases
</ul>
<h4>Not Covered (but recommended)</h4>
<ul>
<li>BentoML (for model-serving)
<li>Optuna (for hyperparameter optimization) – has integration with Hydra
</ul>
</one-col>
</multi-col>
</section>
<section class="dark-cyan">
<p>
<p>
<p>
<p>
<h2>Core tenets</h2>
</section>
<section>
<h3>Core tenets</h3>
<section>
<multi-col>
<one-col>
<ul>
<li>Avoid boilerplate code
<ul>
<li>The best code is code you don't have to write.
</ul>
<li>Put shared code into libraries
<ul>
<li>EthicML
<li>PALkit
</ul>
<li>Automation > Manual
<ul>
<li>Try to make your model run end-to-end
<li>Keep hard-coding to a minimum
</ul>
</ul>
</one-col>
<one-col>
<ul>
<li>Type-annotate everything
<ul>
<li>readable and less error-prone code
<li>use static type checkers like pyright and mypy
</ul>
<li>Make it easy for others to run your code
<ul>
<li>Specify exact dependencies
<li>Don’t use notebooks
<li>Run and configure via the command line
</ul>
</ul>
</one-col>
</multi-col>
</section>
<section>
<multi-col>
<one-col>
<ul>
<li>Make it easy to collaborate
<ul>
<li>Use git
<li>Make PRs and ask for reviews
</ul>
</ul>
</one-col>
<one-col>
</one-col>
</multi-col>
</section>
</section>
<section class="dark-cyan">
<p>
<p>
<p>
<p>
<h2>Python Tools</h2>
</section>
<section>
<h3>Poetry — Simple, Conflict-free<br> Dependency Management</h3>
<section>
<multi-col>
<one-col>
<img src="images/poetry.png" alt="poetry logo">
<ul>
<li>Poetry is a tool for managing Python dependencies
</ul>
</one-col>
<one-col>
<ul>
<li>Alternative to <code>setup.py</code>
<li>Automatically resolves meta-dependencies (dependencies between dependencies)
<li>Maintains a lock file that ensures that all people working on the project are locked to the same versions of dependencies
<li>Also provides some protection if you forget to activate a venv
</ul>
</one-col>
</multi-col>
</section>
<section>
<p><code>pyproject.toml</code> replaces <code>setup.py</code> and also auxiliary <code>.cfg</code> files such as <code>black.cfg</code> and <code>.isort.cfg</code>
<p>Install poetry from the website or homebrew
<h4>Useful commands</h4>
<p><code>poetry install</code> – install dependencies
<p><code>poetry update</code> – check for dependency updates that won’t break your code
<p><code>poetry add <package></code> – installs and adds a new dependency (no need to manually code dependencies as required for requirements.txt/setup.py)
<p>Analogy (for rustaceans): cargo for python
</section>
</section>
<section>
<h3>Hydra — elegant and flexible configuration</h3>
<section>
<multi-col>
<one-col>
<p> <img src="images/Hydra-Readme-logo2.svg" alt="hydra logo">
<p>
<ul>
<li>Hydra is a tool for configuring complex applications
<li>“Complex” means something like more than 10 flags
</ul>
</one-col>
<one-col>
<ul>
<li>Hydra enables configuration via YAML files and allows overriding any configuration value on the commandline
<li>Hydra encourages modular configuration
<ul>
<li>E.g. data loading config and model config is separate
<li>Config modules can be swapped out
<li>Supports validation and variable interpolation
</ul>
</ul>
</one-col>
</multi-col>
</section>
<section>
<multi-col>
<one-col>
<ul>
<li>Hydra supports <em>multiruns</em> where multiple parameter values are run in a combinatorial fashion
<li>Hydra also has plugins that allow for hyperparameter sweeps to be conducted using popular HPO libraries (e.g. Optuna)
</ul>
</one-col>
<one-col>
<ul>
<li>Hydra can also instantiate Python objects based on configuration values
</ul>
</one-col>
</multi-col>
</section>
</section>
<section>
<h3>Ray — effortless parallelism</h3>
<multi-col>
<one-col>
<p><img src="images/ray_header_logo.png" alt="ray logo">
<ul>
<li>Ray is a tool for easy parallelisation of Python functions
<li>It can be used to a hyperparameter search over multiple GPUs and multiple machines
</ul>
</one-col>
<one-col>
<ul>
<li>Ray usually parallelises a single Python function, but combined with Hydra, it parallelises your whole application
<li>Ray can act as a queue for jobs
<li>Ray is GPU-aware and can distribute jobs over multiple machines according to how many GPUs are available on the machines
</ul>
</one-col>
</multi-col>
</section>
<section class="dark-cyan">
<p>
<p>
<p>
<p>
<h2>ML Tools</h2>
</section>
<section>
<h3>PyTorch Lightning — avoid boilerplate code</h3>
<multi-col>
<one-col>
<p style="text-align: center"><img src="images/PL_logo.svg" alt="pytorch lightning logo" style="width: 3rem">
<ul>
<li>Lightning provides a set of common abstractions that you see in PyTorch models
<li>Gives many things for free
<ul>
<li>Logging (e.g. to W&B)
<li>Distributed training
<!-- <ul> -->
<!-- <li>Model or Data parallel -->
<!-- </ul> -->
<li>Automatic LR and batch-size determination
</ul>
</ul>
</one-col>
<one-col>
<p>Lightning is made up of 3 key components: a DataModule, LightningModule, and a Trainer
<ul>
<li><strong>DataModule</strong> is a container for train, val and test dataloaders
<li><strong>LightningModule</strong> is a <code>nn.Module</code> but you also define the training, val and test steps
<li><strong>Trainer</strong> abstracts away the boilerplate code of the training loop
</ul>
</one-col>
</multi-col>
</section>
</div>
</div>
<script type="module" src="setup.js"></script>