srini98 commited on
Commit
19fb056
1 Parent(s): 1385ab6
Files changed (1) hide show
  1. chapter3.ipynb +428 -0
chapter3.ipynb ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "\n",
10
+ "from fastai.vision.all import *\n",
11
+ "import gradio as gr\n",
12
+ "import timm"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 3,
18
+ "metadata": {},
19
+ "outputs": [
20
+ {
21
+ "data": {
22
+ "text/plain": [
23
+ "['convnext_atto',\n",
24
+ " 'convnext_atto_ols',\n",
25
+ " 'convnext_base',\n",
26
+ " 'convnext_base_384_in22ft1k',\n",
27
+ " 'convnext_base_in22ft1k',\n",
28
+ " 'convnext_base_in22k',\n",
29
+ " 'convnext_femto',\n",
30
+ " 'convnext_femto_ols',\n",
31
+ " 'convnext_large',\n",
32
+ " 'convnext_large_384_in22ft1k',\n",
33
+ " 'convnext_large_in22ft1k',\n",
34
+ " 'convnext_large_in22k',\n",
35
+ " 'convnext_nano',\n",
36
+ " 'convnext_nano_ols',\n",
37
+ " 'convnext_pico',\n",
38
+ " 'convnext_pico_ols',\n",
39
+ " 'convnext_small',\n",
40
+ " 'convnext_small_384_in22ft1k',\n",
41
+ " 'convnext_small_in22ft1k',\n",
42
+ " 'convnext_small_in22k',\n",
43
+ " 'convnext_tiny',\n",
44
+ " 'convnext_tiny_384_in22ft1k',\n",
45
+ " 'convnext_tiny_hnf',\n",
46
+ " 'convnext_tiny_in22ft1k',\n",
47
+ " 'convnext_tiny_in22k',\n",
48
+ " 'convnext_xlarge_384_in22ft1k',\n",
49
+ " 'convnext_xlarge_in22ft1k',\n",
50
+ " 'convnext_xlarge_in22k']"
51
+ ]
52
+ },
53
+ "execution_count": 3,
54
+ "metadata": {},
55
+ "output_type": "execute_result"
56
+ }
57
+ ],
58
+ "source": [
59
+ "timm.list_models('convnext*')"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": 4,
65
+ "metadata": {},
66
+ "outputs": [
67
+ {
68
+ "ename": "NameError",
69
+ "evalue": "name 'dls' is not defined",
70
+ "output_type": "error",
71
+ "traceback": [
72
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
73
+ "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
74
+ "Cell \u001b[1;32mIn[4], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m learn \u001b[39m=\u001b[39m vision_learner(dls , \u001b[39m'\u001b[39m\u001b[39mconvnext_tiny_in22k\u001b[39m\u001b[39m'\u001b[39m , metrics \u001b[39m=\u001b[39m error_rate)\u001b[39m.\u001b[39mto_fp16()\n\u001b[0;32m 2\u001b[0m learn\u001b[39m.\u001b[39mfine_tune(\u001b[39m3\u001b[39m)\n",
75
+ "\u001b[1;31mNameError\u001b[0m: name 'dls' is not defined"
76
+ ]
77
+ }
78
+ ],
79
+ "source": []
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": 6,
84
+ "metadata": {},
85
+ "outputs": [
86
+ {
87
+ "data": {
88
+ "text/plain": [
89
+ "10.75"
90
+ ]
91
+ },
92
+ "execution_count": 6,
93
+ "metadata": {},
94
+ "output_type": "execute_result"
95
+ }
96
+ ],
97
+ "source": [
98
+ "def quad(a , b , c , x): return a*x**2 + b*x + c\n",
99
+ "\n",
100
+ "quad(3 , 2 , 1 , 1.5)\n",
101
+ "\n"
102
+ ]
103
+ },
104
+ {
105
+ "cell_type": "code",
106
+ "execution_count": 8,
107
+ "metadata": {},
108
+ "outputs": [
109
+ {
110
+ "data": {
111
+ "text/plain": [
112
+ "10.75"
113
+ ]
114
+ },
115
+ "execution_count": 8,
116
+ "metadata": {},
117
+ "output_type": "execute_result"
118
+ }
119
+ ],
120
+ "source": [
121
+ "from functools import partial\n",
122
+ "\n",
123
+ "def mk_quad(a , b , c): return partial(quad ,a , b , c)\n",
124
+ "\n",
125
+ "f = mk_quad(3 , 2 , 1)\n",
126
+ "\n",
127
+ "f(1.5)"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": 10,
133
+ "metadata": {},
134
+ "outputs": [],
135
+ "source": [
136
+ "import torch\n",
137
+ "import matplotlib.pyplot as plt \n",
138
+ "def plot_function(f, title=None, min=-2.1, max=2.1, color='r', ylim=None):\n",
139
+ " x = torch.linspace(min,max, 100)[:,None]\n",
140
+ " if ylim: plt.ylim(ylim)\n",
141
+ " plt.plot(x, f(x), color)\n",
142
+ " if title is not None: plt.title(title)"
143
+ ]
144
+ },
145
+ {
146
+ "cell_type": "code",
147
+ "execution_count": 9,
148
+ "metadata": {},
149
+ "outputs": [],
150
+ "source": [
151
+ "def mse(preds , acts): return ((preds - acts)**2).mean()"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": 13,
157
+ "metadata": {},
158
+ "outputs": [],
159
+ "source": [
160
+ "def noise(x, scale): return np.random.normal(scale=scale, size=x.shape)\n",
161
+ "def add_noise(x, mult, add): return x * (1+noise(x,mult)) + noise(x,add)"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "code",
166
+ "execution_count": 14,
167
+ "metadata": {},
168
+ "outputs": [],
169
+ "source": [
170
+ "import numpy as np\n",
171
+ "np.random.seed(42)\n",
172
+ "\n",
173
+ "x = torch.linspace(-2, 2, steps=20)[:,None]\n",
174
+ "y = add_noise(f(x), 0.15, 1.5)"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": 15,
180
+ "metadata": {},
181
+ "outputs": [
182
+ {
183
+ "data": {
184
+ "application/vnd.jupyter.widget-view+json": {
185
+ "model_id": "80a6fa4ee63e4e1c8cf49638c80adf50",
186
+ "version_major": 2,
187
+ "version_minor": 0
188
+ },
189
+ "text/plain": [
190
+ "interactive(children=(FloatSlider(value=1.5, description='a', max=4.5, min=-1.5), FloatSlider(value=1.5, descr…"
191
+ ]
192
+ },
193
+ "metadata": {},
194
+ "output_type": "display_data"
195
+ }
196
+ ],
197
+ "source": [
198
+ "from ipywidgets import interact\n",
199
+ "@interact(a=1.5 , b = 1.5 , c=1.5)\n",
200
+ "def plot_quad(a , b , c):\n",
201
+ " f = mk_quad(a , b , c)\n",
202
+ " plt.scatter(x , y)\n",
203
+ " loss = mse(f(x) , y)\n",
204
+ " plot_function(f , ylim=(-3 , 12) , title = f\"MSE: {loss:.2f}\")"
205
+ ]
206
+ },
207
+ {
208
+ "cell_type": "code",
209
+ "execution_count": 16,
210
+ "metadata": {},
211
+ "outputs": [],
212
+ "source": [
213
+ "def quad_mse(params):\n",
214
+ " f = mk_quad(*params)\n",
215
+ " return mse(f(x) , y)"
216
+ ]
217
+ },
218
+ {
219
+ "cell_type": "code",
220
+ "execution_count": 20,
221
+ "metadata": {},
222
+ "outputs": [
223
+ {
224
+ "data": {
225
+ "text/plain": [
226
+ "tensor([1.5000, 1.5000, 1.5000], requires_grad=True)"
227
+ ]
228
+ },
229
+ "execution_count": 20,
230
+ "metadata": {},
231
+ "output_type": "execute_result"
232
+ }
233
+ ],
234
+ "source": [
235
+ "abc = torch.tensor([1.5 , 1.5 , 1.5])\n",
236
+ "abc.requires_grad_()\n",
237
+ "\n"
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "code",
242
+ "execution_count": 21,
243
+ "metadata": {},
244
+ "outputs": [
245
+ {
246
+ "data": {
247
+ "text/plain": [
248
+ "tensor(5.4892, dtype=torch.float64, grad_fn=<MeanBackward0>)"
249
+ ]
250
+ },
251
+ "execution_count": 21,
252
+ "metadata": {},
253
+ "output_type": "execute_result"
254
+ }
255
+ ],
256
+ "source": [
257
+ "loss = quad_mse(abc)\n",
258
+ "\n",
259
+ "loss"
260
+ ]
261
+ },
262
+ {
263
+ "cell_type": "code",
264
+ "execution_count": 22,
265
+ "metadata": {},
266
+ "outputs": [],
267
+ "source": [
268
+ "loss.backward()"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": 23,
274
+ "metadata": {},
275
+ "outputs": [
276
+ {
277
+ "data": {
278
+ "text/plain": [
279
+ "tensor([-7.0908, 1.0602, -1.8620])"
280
+ ]
281
+ },
282
+ "execution_count": 23,
283
+ "metadata": {},
284
+ "output_type": "execute_result"
285
+ }
286
+ ],
287
+ "source": [
288
+ "abc.grad"
289
+ ]
290
+ },
291
+ {
292
+ "cell_type": "code",
293
+ "execution_count": 27,
294
+ "metadata": {},
295
+ "outputs": [
296
+ {
297
+ "name": "stdout",
298
+ "output_type": "stream",
299
+ "text": [
300
+ "LOSS IS tensor(2.6496, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
301
+ "LOSS IS tensor(3.3536, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
302
+ "LOSS IS tensor(4.1631, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
303
+ "LOSS IS tensor(4.7701, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
304
+ "LOSS IS tensor(4.9408, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
305
+ "LOSS IS tensor(4.5979, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
306
+ "LOSS IS tensor(3.8490, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
307
+ "LOSS IS tensor(2.9481, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
308
+ "LOSS IS tensor(2.2058, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
309
+ "LOSS IS tensor(1.8796, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
310
+ "LOSS IS tensor(2.0819, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
311
+ "LOSS IS tensor(2.7403, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
312
+ "LOSS IS tensor(3.6224, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
313
+ "LOSS IS tensor(4.4176, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
314
+ "LOSS IS tensor(4.8469, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
315
+ "LOSS IS tensor(4.7612, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
316
+ "LOSS IS tensor(4.1943, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
317
+ "LOSS IS tensor(3.3515, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
318
+ "LOSS IS tensor(2.5374, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
319
+ "LOSS IS tensor(2.0492, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
320
+ "LOSS IS tensor(2.0717, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
321
+ "LOSS IS tensor(2.6126, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
322
+ "LOSS IS tensor(3.4991, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
323
+ "LOSS IS tensor(4.4391, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
324
+ "LOSS IS tensor(5.1229, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
325
+ "LOSS IS tensor(5.3318, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
326
+ "LOSS IS tensor(5.0142, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
327
+ "LOSS IS tensor(4.3023, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
328
+ "LOSS IS tensor(3.4640, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
329
+ "LOSS IS tensor(2.8078, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
330
+ "LOSS IS tensor(2.5724, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
331
+ "LOSS IS tensor(2.8419, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
332
+ "LOSS IS tensor(3.5167, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
333
+ "LOSS IS tensor(4.3478, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
334
+ "LOSS IS tensor(5.0262, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
335
+ "LOSS IS tensor(5.2927, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
336
+ "LOSS IS tensor(5.0303, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
337
+ "LOSS IS tensor(4.3074, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
338
+ "LOSS IS tensor(3.3546, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
339
+ "LOSS IS tensor(2.4847, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
340
+ "LOSS IS tensor(1.9837, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
341
+ "LOSS IS tensor(2.0101, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
342
+ "LOSS IS tensor(2.5399, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
343
+ "LOSS IS tensor(3.3748, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
344
+ "LOSS IS tensor(4.2127, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
345
+ "LOSS IS tensor(4.7532, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
346
+ "LOSS IS tensor(4.8036, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
347
+ "LOSS IS tensor(4.3463, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
348
+ "LOSS IS tensor(3.5442, dtype=torch.float64, grad_fn=<MeanBackward0>)\n",
349
+ "LOSS IS tensor(2.6829, dtype=torch.float64, grad_fn=<MeanBackward0>)\n"
350
+ ]
351
+ }
352
+ ],
353
+ "source": [
354
+ "for i in range(50):\n",
355
+ " loss = quad_mse(abc)\n",
356
+ " loss.backward()\n",
357
+ " with torch.no_grad():\n",
358
+ " abc -= abc.grad * 0.01\n",
359
+ " print(\"LOSS IS\" , loss)"
360
+ ]
361
+ },
362
+ {
363
+ "cell_type": "code",
364
+ "execution_count": 28,
365
+ "metadata": {},
366
+ "outputs": [
367
+ {
368
+ "data": {
369
+ "text/plain": [
370
+ "tensor([2.8198, 0.7940, 0.9271], requires_grad=True)"
371
+ ]
372
+ },
373
+ "execution_count": 28,
374
+ "metadata": {},
375
+ "output_type": "execute_result"
376
+ }
377
+ ],
378
+ "source": [
379
+ "abc"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": 29,
385
+ "metadata": {},
386
+ "outputs": [],
387
+ "source": [
388
+ "def rectified_linear(m , b , x):\n",
389
+ " y = m*x + b\n",
390
+ " return torch.clip(y , 0.0)\n"
391
+ ]
392
+ },
393
+ {
394
+ "cell_type": "code",
395
+ "execution_count": null,
396
+ "metadata": {},
397
+ "outputs": [],
398
+ "source": []
399
+ }
400
+ ],
401
+ "metadata": {
402
+ "kernelspec": {
403
+ "display_name": "diffusers",
404
+ "language": "python",
405
+ "name": "python3"
406
+ },
407
+ "language_info": {
408
+ "codemirror_mode": {
409
+ "name": "ipython",
410
+ "version": 3
411
+ },
412
+ "file_extension": ".py",
413
+ "mimetype": "text/x-python",
414
+ "name": "python",
415
+ "nbconvert_exporter": "python",
416
+ "pygments_lexer": "ipython3",
417
+ "version": "3.8.16"
418
+ },
419
+ "orig_nbformat": 4,
420
+ "vscode": {
421
+ "interpreter": {
422
+ "hash": "d26bfdf1b33eed3ee2dd554c3e2dd92c45e09514ce35d65c074828f753b40123"
423
+ }
424
+ }
425
+ },
426
+ "nbformat": 4,
427
+ "nbformat_minor": 2
428
+ }