-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMachine Learning su larga scala.lyx
587 lines (435 loc) · 10.8 KB
/
Machine Learning su larga scala.lyx
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
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass book
\use_default_options true
\master Appunti Machine Learning.lyx
\begin_modules
theorems-ams
eqs-within-sections
figs-within-sections
\end_modules
\maintain_unincluded_children false
\language italian
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Chapter
Machine Learning su larga scala
\end_layout
\begin_layout Section
Scelta dell'algoritmo
\end_layout
\begin_layout Standard
Per scegliere quale algoritmo usare in quale condizione non esiste un criterio
unico.
\end_layout
\begin_layout Standard
Delle linee guida da seguire però possono essere quelle di utilizzare la
regressione lineare o le SVM con kernel lineare nel caso di grande numero
di feature relativamente ai numeri di esempi, oppure quando il numero di
feature è piccolo ma il numero di esempi è molto grande (
\begin_inset Formula $50000+$
\end_inset
).
\end_layout
\begin_layout Standard
Le SVM con kernel gaussiano quando il numero di feature è piccolo e il numero
di esempi medio (
\begin_inset Formula $10-10000$
\end_inset
).
\end_layout
\begin_layout Standard
Le reti neurali si comportano bene in tutte le condizioni (con architetture
adeguate) ma solitamente sono più lente nell''apprendimento.
\end_layout
\begin_layout Section
Stochastic Gradient Descent
\end_layout
\begin_layout Standard
Lo Stochastic Gradient Descent è un algoritmo di ottimizzazione che permette
di lavorare con dataset di grandi dimensioni.
Il Gradient Descent classico viene chiamato, in contrasto, Batch Gradient
Descent.
\end_layout
\begin_layout Standard
A differenza del Batch Gradient Descent, nello Stochastic Gradeint Descent,
ad ogni passo non si aggiornano i parametri
\begin_inset Formula $\theta$
\end_inset
rispetto alla somma della derivata della funzione costo rispetto a tutti
gli esempi, ma solo uno alla volta.
Ciò permette, anche in presenza di grandi dataset, di effettuare piccoli
aggiornamenti dei parametri molto più veloci che permettono solitamente
di raggiungere la convergenza molto prima.
\end_layout
\begin_layout Standard
Scriviamo la funzione costo della regressione lineare come esempio, in una
nuova formulazione utile per lo Stochastic Gradient Descent:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
cost(\theta,(x^{(i)},y^{(i)}))=\frac{1}{2}\left(h_{\theta}(x^{(i)})-y^{(i)}\right)^{2}
\]
\end_inset
\begin_inset Formula
\[
J(\theta)=\frac{1}{m}\sum_{i=1}^{m}cost(\theta,(x^{(i)},y^{(i)}))
\]
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
\theta_{k}:=\theta_{k}-\alpha\frac{\partial}{\partial\theta_{k}}cost(\theta,(x^{(i)},y^{(i)}))
\]
\end_inset
\end_layout
\begin_layout Standard
Calcolando le derivate parziali, si ottengono le funzioni per aggiornare
i parametri (non è presente la sommatoria poiché si utilizza la derivata
della funzione
\family roman
\series medium
\shape up
\size normal
\emph off
\bar no
\strikeout off
\uuline off
\uwave off
\noun off
\color none
\lang english
\begin_inset Formula $cost(\theta,(x^{(i)},y^{(i)}))$
\end_inset
\family default
\series default
\shape default
\size default
\emph default
\bar default
\strikeout default
\uuline default
\uwave default
\noun default
\color inherit
\lang italian
):
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
\theta_{k}:=\theta_{k}-\alpha\frac{1}{m}\left(h_{\theta}(x^{(i)})-y^{(i)}\right)\cdot x_{k}^{(i)}
\]
\end_inset
\end_layout
\begin_layout Standard
L'intero processo è mostrato nell'Algoritmo
\begin_inset CommandInset ref
LatexCommand ref
reference "alg:Stochastic-Gradient-Descent"
\end_inset
.
Il numero di ripetizioni può variare a seconda del numero di esempi di
training: per numeri molto alti può bastare anche una sola ripetizione,
per numeri inferiori si arriva tendenzialmente non oltre le 10 ripetizioni.
\end_layout
\begin_layout Standard
\begin_inset Float algorithm
placement h
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Stochastic Gradient Descent
\begin_inset CommandInset label
LatexCommand label
name "alg:Stochastic-Gradient-Descent"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\noun on
Repeat
\noun default
{
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\noun on
for
\noun default
\begin_inset Formula $i:=1,\ldots,m$
\end_inset
{
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
\noun on
for
\noun default
\begin_inset Formula $j:=0,\ldots,n$
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
\begin_inset Formula $\theta_{j}:=\theta_{j}-\alpha\left(h_{\theta}(x^{(i)})-y^{(i)}\right)\cdot x_{j}^{(i)}$
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
}
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
}
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Section
Mini-batch Gradient Descent
\end_layout
\begin_layout Standard
Il Mini-batch Gradient Descent è una via di mezzo tra il Batch Gradient
Descent e lo Stochastic Gradient Descent.
Si basa su un parametro
\begin_inset Formula $b$
\end_inset
che denota il numero di esempi da utilizzare ad ogni iterazione.
Impostando
\begin_inset Formula $b=m$
\end_inset
l'algoritmo è equivalente al Batch Gradient Descent, mentre impostando
\begin_inset Formula $b=1$
\end_inset
l'algoritmo è equivalente allo Stochastic Gradient Descent.
Il vantaggio di questo algoritmo è velocizzare ulteriormente il tempo di
apprendimento se si implementa una buona vettorializzazione.
\end_layout
\begin_layout Standard
L'intero processo è mostrato nell'Algoritmo
\begin_inset CommandInset ref
LatexCommand ref
reference "alg:9.2 Mini-batch-Gradient-Descent"
\end_inset
.
Il numero di ripetizioni può variare a seconda del numero di esempi di
training: per numeri molto alti può bastare anche una sola ripetizione,
per numeri inferiori si arriva tendenzialmente non oltre le 10 ripetizioni.
\end_layout
\begin_layout Standard
\begin_inset Float algorithm
placement h
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Mini-batchGradient Descent
\begin_inset CommandInset label
LatexCommand label
name "alg:9.2 Mini-batch-Gradient-Descent"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\noun on
Repeat
\noun default
{
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\noun on
for
\noun default
\begin_inset Formula $i:=1,\ldots,m$
\end_inset
(increase size =
\begin_inset Formula $b$
\end_inset
) {
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
\noun on
for
\noun default
\begin_inset Formula $j:=0,\ldots,n$
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
\begin_inset Formula $\theta_{j}:=\theta_{j}-\alpha\frac{1}{b}\sum_{k=i}^{i+b-1}\left(h_{\theta}(x^{(k)})-y^{(k)}\right)\cdot x_{j}^{(k)}$
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
\begin_inset space \quad{}
\end_inset
}
\end_layout
\begin_layout Plain Layout
\begin_inset space \quad{}
\end_inset
}
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Section
Convergenza
\end_layout
\begin_layout Standard
Sia lo Stochastic Gradient Descent che il Batch Gradient Descent non hanno
una convergenza garantita ma continuano a muoversi nella zona di un minimo
locale.
Calcolare ad ogni passo
\begin_inset Formula $J(\theta)$
\end_inset
per vederne l'andamento non è una buona idea perchè per calcolarla bisogna
farlo su tutti gli esempi di training.
Più utile risulta essere calcolare
\begin_inset Formula $cost(\theta,(x^{(i)},y^{(i)}))$
\end_inset
e visualizzare ogni
\begin_inset Formula $t$
\end_inset
iterazioni la media di questa funzione, dove
\begin_inset Formula $t=1000$
\end_inset
o
\begin_inset Formula $t=5000$
\end_inset
o altri valori a seconda della capacità di visualizzare l'andamento desiderata.
\end_layout
\begin_layout Standard
Per facilitare la convergenza ed evitare di far muovere di molto i valori
di
\begin_inset Formula $\theta$
\end_inset
attorno al minimo trovato si può utilizzare un valore di
\begin_inset Formula $\alpha$
\end_inset
decrescente, come ad esempio:
\end_layout
\begin_layout Standard
\begin_inset Formula
\[
\alpha=\frac{cons1}{iterationNumber+const2}
\]
\end_inset
dove
\begin_inset Formula $const1$
\end_inset
e
\begin_inset Formula $const2$
\end_inset
sono dei valori da decidere in base al problema, di base possono essere
rispettivamente 1 e 0.
\end_layout
\begin_layout Section
Online Learning
\end_layout
\begin_layout Standard
Per Online Learning si intende l'abilità di un sistema di aggiornare i propri
parametri appresi ogni volta che un nuovo esempio si rende disponibile.
\end_layout
\begin_layout Standard
Ad esempio su un sito web si possono presentare dei degli item consigliati
per un utente e se quell'utente clicca su di uno di quegli item, viene
considerato un esempio positivo.
A quel punto si può intraprendere l'aggiornamento dei parametri del sistema
di apprendimento che ha restituito gli item in base al nuovo esempio, ad
esempio effettuando un passo di Stochastic Gradient Descent.
Sistemi di questo tipo permettono di adattarsi dinamicamente ai gusti degli
utenti e di essere efficaci fin da subito nell'utilizzo in uno scenario
applicativo reale.
\end_layout
\end_body
\end_document