-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickstart.html
465 lines (419 loc) · 60.4 KB
/
quickstart.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Quickstart
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Atılım Güneş Baydin, Don Syme, Barak A. Pearlmutter, Jeffrey Siskind, and DiffSharp contributors">
<meta name="description" content="DiffSharp is a tensor library with support for differentiable programming. It is designed for use in machine learning, probabilistic programming, optimization and other domains.">
<script src="https://code.jquery.com/jquery-1.8.0.js"></script>
<script src="https://code.jquery.com/ui/1.8.23/jquery-ui.js"></script>
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="https://diffsharp.github.io/content/fsdocs-default.css" />
<script src="https://diffsharp.github.io/content/fsdocs-tips.js" type="text/javascript"></script>
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
<!-- END SEARCH BOX: this adds support for the search box -->
</head>
<body>
<div class="container">
<!-- <div class="masthead">
<ul class="nav nav-pills pull-right">
<li><a href="https://fsharp.org">fsharp.org</a></li>
</ul>
<h3 class="muted">DiffSharp</h3>
</div> -->
<!-- <hr /> -->
<div class="row">
<div class="col-xs-12" style="height:10px;"></div>
</div>
<div class="row">
<div class="span3" id="fsdocs-nav">
<a href="index.html"><img class="logo" src="https://diffsharp.github.io/img/diffsharp-logo-text.png"/></a>
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
<div id="header">
<div class="searchbox">
<label for="search-by">
<i class="fas fa-search"></i>
</label>
<input data-search-input="" id="search-by" type="search" placeholder="Search..." />
<span data-search-clear="">
<i class="fas fa-times"></i>
</span>
</div>
</div>
<!-- END SEARCH BOX: this adds support for the search box -->
<ul class="nav nav-list" id="menu">
<!-- <li class="nav-header">DiffSharp</li> -->
<!-- <li class="divider"></li> -->
<li><a href="https://diffsharp.github.io/index.html">Home</a></li>
<li><a href="https://github.com/DiffSharp/DiffSharp/">GitHub</a></li>
<li><a href="https://github.com/DiffSharp/DiffSharp/blob/dev/LICENSE">License (BSD)</a></li>
<li class="nav-header">Getting Started</li>
<!-- <li class="divider"></li> -->
<li><a href="https://diffsharp.github.io/install.html">Install</a></li>
<li><a href="https://diffsharp.github.io/quickstart.html">Quickstart</a></li>
<!-- <li><a href="https://diffsharp.github.io/tensors.html">Tensors</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/differentiable-programming.html">Differentiable Programming</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/nested-derivatives.html">Nested Derivatives</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/models.html">Models</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/optimization.html">Optimization</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/probability-distributions.html">Probability Distributions</a></li> -->
<li class="nav-header">Tutorials</li>
<!-- <li class="divider"></li> -->
<!-- <li><a href="https://diffsharp.github.io/tutorial-classifier.html">Classifier</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/tutorial-gan.html">GAN</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/tutorial-vae.html">VAE</a></li> -->
<!-- <li><a href="https://diffsharp.github.io/tutorial-language.html">Language Models</a></li> -->
<li><a href="https://github.com/DiffSharp/DiffSharp/tree/dev/examples">More Examples</a></li>
<li class="nav-header">API Documentation</li>
<li><a href="https://diffsharp.github.io/reference/index.html">API Reference</a></li>
<li><a href="https://diffsharp.github.io/extensions.html">Extensions</a></li>
<!-- <li class="nav-header">Examples</li> -->
<!-- <li class="divider"></li> -->
<!-- <li class="nav-header">Machine Learning</li> -->
<!-- <li><a href="https://diffsharp.github.io/examples-topic1.html">Topic 1</a></li> -->
<!-- <li class="divider"></li>
<li class="nav-header">Authors</li>
<li><a href="http://www.robots.ox.ac.uk/~gunes/">Atılım Güneş Baydin</a></li>
<li><a href="http://www.bcl.hamilton.ie/~barak/">Barak A. Pearlmutter</a></li>
<li><a href="https://www.microsoft.com/en-us/research/people/dsyme/">Don Syme</a></li> -->
</ul>
</div>
<div class="span9" id="fsdocs-content">
<p><a href="https://colab.research.google.com/github/DiffSharp/diffsharp.github.io/blob/master/quickstart.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Binder" /></a> 
<a href="https://mybinder.org/v2/gh/diffsharp/diffsharp.github.io/master?filepath=quickstart.ipynb"><img src="img/badge-binder.svg" alt="Binder" /></a> 
<a href="quickstart.fsx"><img src="img/badge-script.svg" alt="Script" /></a> 
<a href="quickstart.ipynb"><img src="img/badge-notebook.svg" alt="Script" /></a></p>
<h1><a name="Quickstart" class="anchor" href="#Quickstart">Quickstart</a></h1>
<p>Here we cover some key tasks involved in a typical machine learning pipeline and how these can be implemented with DiffSharp. Note that a significant part of DiffSharp's design has been influenced by <a href="https://pytorch.org/">PyTorch</a> and you would feel mostly at home if you have familiarity with PyTorch.</p>
<h2><a name="Datasets-and-Data-Loaders" class="anchor" href="#Datasets-and-Data-Loaders">Datasets and Data Loaders</a></h2>
<p>DiffSharp provides the <a href="https://diffsharp.github.io/reference/diffsharp-data-dataset.html">Dataset</a> type that represents a data source and the <a href="https://diffsharp.github.io/reference/diffsharp-data-dataloader.html">DataLoader</a> type that handles the loading of data from datasets and iterating over <a href="https://en.wikipedia.org/wiki/Stochastic_gradient_descent#Iterative_method">minibatches</a> of data.</p>
<p>See the <a href="/reference/diffsharp-data.html">DiffSharp.Data</a> namespace for the full API reference.</p>
<h3><a name="Datasets" class="anchor" href="#Datasets">Datasets</a></h3>
<p>DiffSharp has ready-to-use types that cover main datasets typically used in machine learning, such as <a href="https://diffsharp.github.io/reference/diffsharp-data-mnist.html">MNIST</a>, <a href="https://diffsharp.github.io/reference/diffsharp-data-cifar10.html">CIFAR10</a>, <a href="https://diffsharp.github.io/reference/diffsharp-data-cifar100.html">CIFAR100</a>, and also more generic dataset types such as <a href="https://diffsharp.github.io/reference/diffsharp-data-tensordataset.html">TensorDataset</a> or <a href="https://diffsharp.github.io/reference/diffsharp-data-imagedataset.html">ImageDataset</a>.</p>
<p>The following loads the <a href="http://yann.lecun.com/exdb/mnist/">MNIST</a> dataset and shows one image entry and the corresponding label.</p>
<pre class="fssnip highlighted"><code lang="fsharp"><span class="k">open</span> <span onmouseout="hideTip(event, 'fs1', 12)" onmouseover="showTip(event, 'fs1', 12)" class="id">DiffSharp</span>
<span class="k">open</span> <span onmouseout="hideTip(event, 'fs1', 13)" onmouseover="showTip(event, 'fs1', 13)" class="id">DiffSharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs8', 14)" onmouseover="showTip(event, 'fs8', 14)" class="id">Data</span>
<span class="c">// First ten images in MNIST training set</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs9', 15)" onmouseover="showTip(event, 'fs9', 15)" class="id">dataset</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs10', 16)" onmouseover="showTip(event, 'fs10', 16)" class="fn">MNIST</span><span class="pn">(</span><span class="s">"../data"</span><span class="pn">,</span> <span class="fn">train</span><span class="o">=</span><span class="k">true</span><span class="pn">,</span> <span class="fn">transform</span><span class="o">=</span><span onmouseout="hideTip(event, 'fs11', 17)" onmouseover="showTip(event, 'fs11', 17)" class="fn">id</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs12', 18)" onmouseover="showTip(event, 'fs12', 18)" class="fn">n</span><span class="o">=</span><span class="n">10</span><span class="pn">)</span>
<span class="c">// Inspect a single image and label</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs13', 19)" onmouseover="showTip(event, 'fs13', 19)" class="id">data</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs14', 20)" onmouseover="showTip(event, 'fs14', 20)" class="id">label</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs9', 21)" onmouseover="showTip(event, 'fs9', 21)" class="id">dataset</span><span class="pn">[</span><span class="n">7</span><span class="pn">]</span>
<span class="c">// Save image to file</span>
<span onmouseout="hideTip(event, 'fs13', 22)" onmouseover="showTip(event, 'fs13', 22)" class="id">data</span><span class="pn">.</span><span class="id">saveImage</span><span class="pn">(</span><span class="s">"test.png"</span><span class="pn">)</span>
</code></pre>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcEAYAAAAinQPXAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB/0lEQVR4nO3XO2sVQRiH8UeJhRaClhYSEWzUxk5QY+EFROwEwbtioShJJSaFWCipAkLAK/oZFEEQQQWxEcGAaS1EC7FU0HghR3hgGMiy7M7urDGH/JqFPfsO+9/ZM+/OAOr16FMD9LnFgAvdYsCmNgiWCXYIbghmVT3OQ8EhwS/Vv49sATcKTggOCpYK1igG66l63AOCW4IRwVdV12cLOC7YJ7I7JrgneKXqumwBn6oY8IvgvmCJijO4VTAksskW8KbggeL534LPKq9fKZhWfKWDMO4b1b+vbAH/CD4qvX6vYJWKv38S/FT9cee9TYTV8YxguYrXXVb6+P884GHBqGC9YjuZa0rxVU+VLeCg4Khgl4rXbVN5mwjL/yXBY8EPpd9X64CbFRvyWjUf76Xgjmgt2wyG5T8cy4TGX/Yls1+x3YQZbKp1wHeCnYIjgieCGZXXnxZcENllm8EPgmuqX3dFCyBgU6H/dSU5YFjO9wieKX2VOyW4LjpTO+B2wZhgt2Cdqr9gVisuHhOCFYrXhQfVtC3MVTvgpGCT4vmLgm8qrw8PZIuKffCF4jftc9Fa6//gWaXXhV3GI8GwqlfdVLUDnhScFxxXdd17wXfFRn5Xsc10pXbAt4JzgteCq4q7gLCtCfvD8IVTtV3qSvIrGrYrtxWP/6t574Nd6/uAfwGuockofAHj/gAAAABJRU5ErkJggg==" style="width: 64px; height: auto"/>
<pre class="fssnip highlighted"><code lang="fsharp"><span class="c">// Inspect data as ASCII and show label</span>
<span onmouseout="hideTip(event, 'fs16', 24)" onmouseover="showTip(event, 'fs16', 24)" class="fn">printfn</span> <span class="s">"Data: </span><span class="pf">%A</span><span class="s">\nLabel: </span><span class="pf">%A</span><span class="s">"</span> <span class="pn">(</span><span onmouseout="hideTip(event, 'fs13', 25)" onmouseover="showTip(event, 'fs13', 25)" class="id">data</span><span class="pn">.</span><span class="id">toImageString</span><span class="pn">(</span><span class="pn">)</span><span class="pn">)</span> <span onmouseout="hideTip(event, 'fs14', 26)" onmouseover="showTip(event, 'fs14', 26)" class="id">label</span>
</code></pre>
<table class="pre"><tr><td><pre><code>Data: "
~-}@#####Z
-j*W########J'
O############i
[##Mxxxxo####i
::^ 'W##Z
|&##f
(o###'
(q%###d.
"uaaa####8}:
_m########O
_*####@####?
"v<____f##?
`##?
|##?
?. 1&##?
iQ#: `)8##&!
p##txxxxb###o\
p#########MC.
+J#####wdt_
}B#Z}^
"
Label: tensor(3,dtype=Int32)</code></pre></td></tr></table>
<h3><a name="Data-Loaders" class="anchor" href="#Data-Loaders">Data Loaders</a></h3>
<p>A data loader handles tasks such as constructing minibatches from an underlying dataset on-the-fly, shuffling the data, and moving the data tensors between devices. In the example below we show a single batch of six MNIST images and their corresponding classification labels.</p>
<pre class="fssnip highlighted"><code lang="fsharp"><span class="k">let</span> <span onmouseout="hideTip(event, 'fs17', 27)" onmouseover="showTip(event, 'fs17', 27)" class="id">loader</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs18', 28)" onmouseover="showTip(event, 'fs18', 28)" class="fn">DataLoader</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs9', 29)" onmouseover="showTip(event, 'fs9', 29)" class="id">dataset</span><span class="pn">,</span> <span class="fn">shuffle</span><span class="o">=</span><span class="k">true</span><span class="pn">,</span> <span class="fn">batchSize</span><span class="o">=</span><span class="n">6</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs19', 30)" onmouseover="showTip(event, 'fs19', 30)" class="id">batch</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs20', 31)" onmouseover="showTip(event, 'fs20', 31)" class="id">labels</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs17', 32)" onmouseover="showTip(event, 'fs17', 32)" class="id">loader</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs21', 33)" onmouseover="showTip(event, 'fs21', 33)" class="id">batch</span><span class="pn">(</span><span class="pn">)</span>
<span onmouseout="hideTip(event, 'fs16', 34)" onmouseover="showTip(event, 'fs16', 34)" class="fn">printfn</span> <span class="s">"</span><span class="pf">%A</span><span class="s">\nLabels: </span><span class="pf">%A</span><span class="s">"</span> <span class="pn">(</span><span onmouseout="hideTip(event, 'fs19', 35)" onmouseover="showTip(event, 'fs19', 35)" class="id">batch</span><span class="pn">.</span><span class="id">toImageString</span><span class="pn">(</span><span class="pn">)</span><span class="pn">)</span> <span onmouseout="hideTip(event, 'fs20', 36)" onmouseover="showTip(event, 'fs20', 36)" class="id">labels</span>
</code></pre>
<table class="pre"><tr><td><pre><code>"
<J#J+
```uzO:Y@%u +8###8 u#@!
";)CX#####*Z#&h! <W##8M#> 1&$#!
+8########$)\\>~ .i*###b|##v u$$#!
`m#####k0%& U######1o#Y lB$p".
/C[##d' -C <8##ar#W_/@X iW$$)
' C#( +8##0'?v, #&+ C##o
j#a ~Y#Mp| ##Y ,#$Bl
'a#I O#8I,: ##h "d#$u
;&*J[ >##! ##h }$#Q`
\8##c^ k#a @#h /8$a^
_o##L: ?%#] ##t "###J
`)##o |#M^ z#o' L$$$~
$#$! |#* n#*I +m$$Z
_nQ##d |#f +Y#Z B$$h'
~tW###$0 |#* r8#U #$$(
^rm####b/ |#$t+:|O*#*Y> J@##"
^lq####k\ |###Ww###hn +W#%j.
`Xm####h/. :k#####Mf !$#m
>ZW####&x' ^n###j~ !$#m
z###qzx` ^a#m
lM~ '^{v
!\ c0~ ;Lp###t
uU Cp~ >tp##r|t> ~L&##*p#M~
mU :#U |M##op###X `L8###w"~##i
mU Q#u >&#a! '0##r 1####w: ~##i
_%U k#> 1##Q' )##*, 0##mY" /##i
c#U ^M#: x##t' w##/ :n>^ {##i
J#c U#w` u#%O. ./%#n J##i
J#l '|O$#( `M#O ;b##X' ]vvvb#h
J#| _+rfL&&B0&#~ ^##"^ck&##$/ .<0##@##W;
L#8ddd##$8kf(: M$ `M#####WW#M <W#&WX&##Mc
cOOOOO1> }#m >B#wz-^a#f /##ui `p####|
X#z !@#[ z##0` ,b#%nZ##Ql++
X#> I##, z#&[` <k#w! 'IU&##*
X#> ##, }#&(`?X&#u: (00~
X@) I##, M#%dw###u
X#1 [##, d####Or;
X#C _@#, ')fv^
X@C w#>
1#C 1#o-
'Q#X'
't#-
"
Labels: tensor([5., 0., 1., 4., 9., 2.])</code></pre></td></tr></table>
<p>In practice a data loader is typically used to iterate over all minibatches in a given dataset in order to feed each minibatch through a machine learning model. One full iteration over the dataset would be called an "epoch". Typically you would perform multiple such epochs of iterations during the training of a model.</p>
<pre class="fssnip highlighted"><code lang="fsharp"><span class="k">for</span> <span onmouseout="hideTip(event, 'fs22', 37)" onmouseover="showTip(event, 'fs22', 37)" class="fn">epoch</span> <span class="o">=</span> <span class="n">1</span> <span class="k">to</span> <span class="n">10</span> <span class="k">do</span>
<span class="k">for</span> <span onmouseout="hideTip(event, 'fs23', 38)" onmouseover="showTip(event, 'fs23', 38)" class="fn">i</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs13', 39)" onmouseover="showTip(event, 'fs13', 39)" class="fn">data</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs20', 40)" onmouseover="showTip(event, 'fs20', 40)" class="fn">labels</span> <span class="k">in</span> <span onmouseout="hideTip(event, 'fs17', 41)" onmouseover="showTip(event, 'fs17', 41)" class="id">loader</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs24', 42)" onmouseover="showTip(event, 'fs24', 42)" class="id">epoch</span><span class="pn">(</span><span class="pn">)</span> <span class="k">do</span>
<span onmouseout="hideTip(event, 'fs16', 43)" onmouseover="showTip(event, 'fs16', 43)" class="fn">printfn</span> <span class="s">"Epoch </span><span class="pf">%A</span><span class="s">, minibatch </span><span class="pf">%A</span><span class="s">"</span> <span onmouseout="hideTip(event, 'fs22', 44)" onmouseover="showTip(event, 'fs22', 44)" class="fn">epoch</span> <span class="pn">(</span><span onmouseout="hideTip(event, 'fs23', 45)" onmouseover="showTip(event, 'fs23', 45)" class="fn">i</span><span class="o">+</span><span class="n">1</span><span class="pn">)</span>
<span class="c">// Process the minibatch</span>
<span class="c">// ...</span>
</code></pre>
<h2><a name="Models" class="anchor" href="#Models">Models</a></h2>
<p>Many machine learning models are differentiable functions whose parameters can be tuned via <a href="https://en.wikipedia.org/wiki/Gradient_descent">gradient-based optimization</a>, finding an optimum for an objective function that quantifies the fit of the model to a given set of data. These models are typically built as compositions non-linear functions and ready-to-use building blocks such as linear, recurrent, and convolutional layers.</p>
<p>DiffSharp provides the most commonly used model building blocks including convolutions, transposed convolutions, batch normalization, dropout, recurrent and other architectures.</p>
<p>See the <a href="/reference/diffsharp-model.html">DiffSharp.Model</a> namespace for the full API reference.</p>
<h3><a name="Constructing-models-PyTorch-style" class="anchor" href="#Constructing-models-PyTorch-style">Constructing models, PyTorch style</a></h3>
<p>If you have experience with <a href="https://pytorch.org/">PyTorch</a>, you would find the following way of model definition familiar. Let's look at an example of a <a href="https://arxiv.org/abs/1406.2661">generative adversarial network (GAN)</a> architecture.</p>
<pre class="fssnip highlighted"><code lang="fsharp"><span class="k">open</span> <span onmouseout="hideTip(event, 'fs1', 46)" onmouseover="showTip(event, 'fs1', 46)" class="id">DiffSharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs25', 47)" onmouseover="showTip(event, 'fs25', 47)" class="id">Model</span>
<span class="k">open</span> <span onmouseout="hideTip(event, 'fs1', 48)" onmouseover="showTip(event, 'fs1', 48)" class="id">DiffSharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs26', 49)" onmouseover="showTip(event, 'fs26', 49)" class="m">Compose</span>
<span class="c">// PyTorch style</span>
<span class="c">// Define a model class inheriting the base</span>
<span class="k">type</span> <span onmouseout="hideTip(event, 'fs27', 50)" onmouseover="showTip(event, 'fs27', 50)" class="rt">Generator</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs28', 51)" onmouseover="showTip(event, 'fs28', 51)" class="fn">nz</span><span class="pn">:</span> <span onmouseout="hideTip(event, 'fs29', 52)" onmouseover="showTip(event, 'fs29', 52)" class="vt">int</span><span class="pn">)</span> <span class="o">=</span>
<span class="k">inherit</span> <span onmouseout="hideTip(event, 'fs30', 53)" onmouseover="showTip(event, 'fs30', 53)" class="rt">Model</span><span class="pn">(</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs31', 54)" onmouseover="showTip(event, 'fs31', 54)" class="fn">fc1</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 55)" onmouseover="showTip(event, 'fs32', 55)" class="fn">Linear</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs28', 56)" onmouseover="showTip(event, 'fs28', 56)" class="fn">nz</span><span class="pn">,</span> <span class="n">256</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs33', 57)" onmouseover="showTip(event, 'fs33', 57)" class="fn">fc2</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 58)" onmouseover="showTip(event, 'fs32', 58)" class="fn">Linear</span><span class="pn">(</span><span class="n">256</span><span class="pn">,</span> <span class="n">512</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs34', 59)" onmouseover="showTip(event, 'fs34', 59)" class="fn">fc3</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 60)" onmouseover="showTip(event, 'fs32', 60)" class="fn">Linear</span><span class="pn">(</span><span class="n">512</span><span class="pn">,</span> <span class="n">1024</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs35', 61)" onmouseover="showTip(event, 'fs35', 61)" class="fn">fc4</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 62)" onmouseover="showTip(event, 'fs32', 62)" class="fn">Linear</span><span class="pn">(</span><span class="n">1024</span><span class="pn">,</span> <span class="n">28</span><span class="o">*</span><span class="n">28</span><span class="pn">)</span>
<span class="k">do</span> <span class="k">base</span><span class="pn">.</span><span class="id">addModel</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs31', 63)" onmouseover="showTip(event, 'fs31', 63)" class="fn">fc1</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs33', 64)" onmouseover="showTip(event, 'fs33', 64)" class="fn">fc2</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs34', 65)" onmouseover="showTip(event, 'fs34', 65)" class="fn">fc3</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs35', 66)" onmouseover="showTip(event, 'fs35', 66)" class="fn">fc4</span><span class="pn">)</span>
<span class="k">override</span> <span onmouseout="hideTip(event, 'fs36', 67)" onmouseover="showTip(event, 'fs36', 67)" class="fn">self</span><span class="pn">.</span><span class="fn">forward</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs37', 68)" onmouseover="showTip(event, 'fs37', 68)" class="fn">x</span><span class="pn">)</span> <span class="o">=</span>
<span onmouseout="hideTip(event, 'fs37', 69)" onmouseover="showTip(event, 'fs37', 69)" class="fn">x</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 70)" onmouseover="showTip(event, 'fs2', 70)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs38', 71)" onmouseover="showTip(event, 'fs38', 71)" class="id">view</span><span class="pn">(</span><span class="pn">[</span><span class="o">-</span><span class="n">1</span><span class="pn">;</span><span onmouseout="hideTip(event, 'fs28', 72)" onmouseover="showTip(event, 'fs28', 72)" class="fn">nz</span><span class="pn">]</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs31', 73)" onmouseover="showTip(event, 'fs31', 73)" class="fn">fc1</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 74)" onmouseover="showTip(event, 'fs39', 74)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 75)" onmouseover="showTip(event, 'fs2', 75)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 76)" onmouseover="showTip(event, 'fs40', 76)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs33', 77)" onmouseover="showTip(event, 'fs33', 77)" class="fn">fc2</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 78)" onmouseover="showTip(event, 'fs39', 78)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 79)" onmouseover="showTip(event, 'fs2', 79)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 80)" onmouseover="showTip(event, 'fs40', 80)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs34', 81)" onmouseover="showTip(event, 'fs34', 81)" class="fn">fc3</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 82)" onmouseover="showTip(event, 'fs39', 82)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 83)" onmouseover="showTip(event, 'fs2', 83)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 84)" onmouseover="showTip(event, 'fs40', 84)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs35', 85)" onmouseover="showTip(event, 'fs35', 85)" class="fn">fc4</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 86)" onmouseover="showTip(event, 'fs39', 86)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 87)" onmouseover="showTip(event, 'fs2', 87)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs41', 88)" onmouseover="showTip(event, 'fs41', 88)" class="id">tanh</span>
<span class="c">// Define a model class inheriting the base</span>
<span class="k">type</span> <span onmouseout="hideTip(event, 'fs42', 89)" onmouseover="showTip(event, 'fs42', 89)" class="rt">Discriminator</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs28', 90)" onmouseover="showTip(event, 'fs28', 90)" class="fn">nz</span><span class="pn">:</span><span onmouseout="hideTip(event, 'fs29', 91)" onmouseover="showTip(event, 'fs29', 91)" class="vt">int</span><span class="pn">)</span> <span class="o">=</span>
<span class="k">inherit</span> <span onmouseout="hideTip(event, 'fs30', 92)" onmouseover="showTip(event, 'fs30', 92)" class="rt">Model</span><span class="pn">(</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs31', 93)" onmouseover="showTip(event, 'fs31', 93)" class="fn">fc1</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 94)" onmouseover="showTip(event, 'fs32', 94)" class="fn">Linear</span><span class="pn">(</span><span class="n">28</span><span class="o">*</span><span class="n">28</span><span class="pn">,</span> <span class="n">1024</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs33', 95)" onmouseover="showTip(event, 'fs33', 95)" class="fn">fc2</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 96)" onmouseover="showTip(event, 'fs32', 96)" class="fn">Linear</span><span class="pn">(</span><span class="n">1024</span><span class="pn">,</span> <span class="n">512</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs34', 97)" onmouseover="showTip(event, 'fs34', 97)" class="fn">fc3</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 98)" onmouseover="showTip(event, 'fs32', 98)" class="fn">Linear</span><span class="pn">(</span><span class="n">512</span><span class="pn">,</span> <span class="n">256</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs35', 99)" onmouseover="showTip(event, 'fs35', 99)" class="fn">fc4</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs32', 100)" onmouseover="showTip(event, 'fs32', 100)" class="fn">Linear</span><span class="pn">(</span><span class="n">256</span><span class="pn">,</span> <span class="n">1</span><span class="pn">)</span>
<span class="k">do</span> <span class="k">base</span><span class="pn">.</span><span class="id">addModel</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs31', 101)" onmouseover="showTip(event, 'fs31', 101)" class="fn">fc1</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs33', 102)" onmouseover="showTip(event, 'fs33', 102)" class="fn">fc2</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs34', 103)" onmouseover="showTip(event, 'fs34', 103)" class="fn">fc3</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs35', 104)" onmouseover="showTip(event, 'fs35', 104)" class="fn">fc4</span><span class="pn">)</span>
<span class="k">override</span> <span onmouseout="hideTip(event, 'fs43', 105)" onmouseover="showTip(event, 'fs43', 105)" class="fn">self</span><span class="pn">.</span><span class="fn">forward</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs37', 106)" onmouseover="showTip(event, 'fs37', 106)" class="fn">x</span><span class="pn">)</span> <span class="o">=</span>
<span onmouseout="hideTip(event, 'fs37', 107)" onmouseover="showTip(event, 'fs37', 107)" class="fn">x</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 108)" onmouseover="showTip(event, 'fs2', 108)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs38', 109)" onmouseover="showTip(event, 'fs38', 109)" class="id">view</span><span class="pn">(</span><span class="pn">[</span><span class="o">-</span><span class="n">1</span><span class="pn">;</span><span class="n">28</span><span class="o">*</span><span class="n">28</span><span class="pn">]</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs31', 110)" onmouseover="showTip(event, 'fs31', 110)" class="fn">fc1</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 111)" onmouseover="showTip(event, 'fs39', 111)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 112)" onmouseover="showTip(event, 'fs2', 112)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 113)" onmouseover="showTip(event, 'fs40', 113)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 114)" onmouseover="showTip(event, 'fs2', 114)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs44', 115)" onmouseover="showTip(event, 'fs44', 115)" class="id">dropout</span><span class="pn">(</span><span class="n">0.3</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs33', 116)" onmouseover="showTip(event, 'fs33', 116)" class="fn">fc2</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 117)" onmouseover="showTip(event, 'fs39', 117)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 118)" onmouseover="showTip(event, 'fs2', 118)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 119)" onmouseover="showTip(event, 'fs40', 119)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 120)" onmouseover="showTip(event, 'fs2', 120)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs44', 121)" onmouseover="showTip(event, 'fs44', 121)" class="id">dropout</span><span class="pn">(</span><span class="n">0.3</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs34', 122)" onmouseover="showTip(event, 'fs34', 122)" class="fn">fc3</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 123)" onmouseover="showTip(event, 'fs39', 123)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 124)" onmouseover="showTip(event, 'fs2', 124)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 125)" onmouseover="showTip(event, 'fs40', 125)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 126)" onmouseover="showTip(event, 'fs2', 126)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs44', 127)" onmouseover="showTip(event, 'fs44', 127)" class="id">dropout</span><span class="pn">(</span><span class="n">0.3</span><span class="pn">)</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs35', 128)" onmouseover="showTip(event, 'fs35', 128)" class="fn">fc4</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs39', 129)" onmouseover="showTip(event, 'fs39', 129)" class="id">forward</span>
<span class="o">|></span> <span onmouseout="hideTip(event, 'fs2', 130)" onmouseover="showTip(event, 'fs2', 130)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs45', 131)" onmouseover="showTip(event, 'fs45', 131)" class="id">sigmoid</span>
<span class="c">// Instantiate the defined classes</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs28', 132)" onmouseover="showTip(event, 'fs28', 132)" class="id">nz</span> <span class="o">=</span> <span class="n">128</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs46', 133)" onmouseover="showTip(event, 'fs46', 133)" class="id">gen</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs27', 134)" onmouseover="showTip(event, 'fs27', 134)" class="fn">Generator</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs28', 135)" onmouseover="showTip(event, 'fs28', 135)" class="id">nz</span><span class="pn">)</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs47', 136)" onmouseover="showTip(event, 'fs47', 136)" class="id">dis</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs42', 137)" onmouseover="showTip(event, 'fs42', 137)" class="fn">Discriminator</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs28', 138)" onmouseover="showTip(event, 'fs28', 138)" class="id">nz</span><span class="pn">)</span>
<span onmouseout="hideTip(event, 'fs48', 139)" onmouseover="showTip(event, 'fs48', 139)" class="fn">print</span> <span onmouseout="hideTip(event, 'fs46', 140)" onmouseover="showTip(event, 'fs46', 140)" class="id">gen</span>
<span onmouseout="hideTip(event, 'fs48', 141)" onmouseover="showTip(event, 'fs48', 141)" class="fn">print</span> <span onmouseout="hideTip(event, 'fs47', 142)" onmouseover="showTip(event, 'fs47', 142)" class="id">dis</span>
</code></pre>
<table class="pre"><tr><td><pre><code>Model(Linear(128, 256), Linear(256, 512), Linear(512, 1024), Linear(1024, 784))
Model(Linear(784, 1024), Linear(1024, 512), Linear(512, 256), Linear(256, 1))</code></pre></td></tr></table>
<h3><a name="Constructing-models-DiffSharp-style" class="anchor" href="#Constructing-models-DiffSharp-style">Constructing models, DiffSharp style</a></h3>
<p>A key advantage of DiffSharp lies in the <a href="https://en.wikipedia.org/wiki/Functional_programming">functional programming</a> paradigm enabled by the F# language, where functions are first-class citizens, many algorithms can be constructed by applying and composing functions, and differentiation operations can be expressed as composable <a href="https://en.wikipedia.org/wiki/Higher-order_function">higher-order functions</a>. This allows very succinct (and beautiful) machine learning code to be expressed as a powerful combination of <a href="https://en.wikipedia.org/wiki/Lambda_calculus">lambda calculus</a> and <a href="https://en.wikipedia.org/wiki/Differential_calculus">differential calculus</a>.</p>
<p>For example, the following constructs the same GAN architecture (that we constructed in PyTorch style in the previous section) using DiffSharp's <code>--></code> composition operator, which allows you to seamlessly compose <code>Model</code> instances and differentiable <code>Tensor->Tensor</code> functions.</p>
<pre class="fssnip highlighted"><code lang="fsharp"><span class="c">// DiffSharp style</span>
<span class="c">// Model as a composition of models and Tensor->Tensor functions</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs49', 143)" onmouseover="showTip(event, 'fs49', 143)" class="id">generator</span> <span class="o">=</span>
<span onmouseout="hideTip(event, 'fs2', 144)" onmouseover="showTip(event, 'fs2', 144)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs38', 145)" onmouseover="showTip(event, 'fs38', 145)" class="id">view</span><span class="pn">(</span><span class="pn">[</span><span class="o">-</span><span class="n">1</span><span class="pn">;</span><span onmouseout="hideTip(event, 'fs28', 146)" onmouseover="showTip(event, 'fs28', 146)" class="id">nz</span><span class="pn">]</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 147)" onmouseover="showTip(event, 'fs32', 147)" class="fn">Linear</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs28', 148)" onmouseover="showTip(event, 'fs28', 148)" class="id">nz</span><span class="pn">,</span> <span class="n">256</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 149)" onmouseover="showTip(event, 'fs2', 149)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 150)" onmouseover="showTip(event, 'fs40', 150)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 151)" onmouseover="showTip(event, 'fs32', 151)" class="fn">Linear</span><span class="pn">(</span><span class="n">256</span><span class="pn">,</span> <span class="n">512</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 152)" onmouseover="showTip(event, 'fs2', 152)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 153)" onmouseover="showTip(event, 'fs40', 153)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 154)" onmouseover="showTip(event, 'fs32', 154)" class="fn">Linear</span><span class="pn">(</span><span class="n">512</span><span class="pn">,</span> <span class="n">1024</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 155)" onmouseover="showTip(event, 'fs2', 155)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 156)" onmouseover="showTip(event, 'fs40', 156)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 157)" onmouseover="showTip(event, 'fs32', 157)" class="fn">Linear</span><span class="pn">(</span><span class="n">1024</span><span class="pn">,</span> <span class="n">28</span><span class="o">*</span><span class="n">28</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 158)" onmouseover="showTip(event, 'fs2', 158)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs41', 159)" onmouseover="showTip(event, 'fs41', 159)" class="id">tanh</span>
<span class="c">// Model as a composition of models and Tensor->Tensor functions</span>
<span class="k">let</span> <span onmouseout="hideTip(event, 'fs50', 160)" onmouseover="showTip(event, 'fs50', 160)" class="id">discriminator</span> <span class="o">=</span>
<span onmouseout="hideTip(event, 'fs2', 161)" onmouseover="showTip(event, 'fs2', 161)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs38', 162)" onmouseover="showTip(event, 'fs38', 162)" class="id">view</span><span class="pn">(</span><span class="pn">[</span><span class="o">-</span><span class="n">1</span><span class="pn">;</span> <span class="n">28</span><span class="o">*</span><span class="n">28</span><span class="pn">]</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 163)" onmouseover="showTip(event, 'fs32', 163)" class="fn">Linear</span><span class="pn">(</span><span class="n">28</span><span class="o">*</span><span class="n">28</span><span class="pn">,</span> <span class="n">1024</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 164)" onmouseover="showTip(event, 'fs2', 164)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 165)" onmouseover="showTip(event, 'fs40', 165)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 166)" onmouseover="showTip(event, 'fs2', 166)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs44', 167)" onmouseover="showTip(event, 'fs44', 167)" class="id">dropout</span><span class="pn">(</span><span class="n">0.3</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 168)" onmouseover="showTip(event, 'fs32', 168)" class="fn">Linear</span><span class="pn">(</span><span class="n">1024</span><span class="pn">,</span> <span class="n">512</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 169)" onmouseover="showTip(event, 'fs2', 169)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 170)" onmouseover="showTip(event, 'fs40', 170)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 171)" onmouseover="showTip(event, 'fs2', 171)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs44', 172)" onmouseover="showTip(event, 'fs44', 172)" class="id">dropout</span><span class="pn">(</span><span class="n">0.3</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 173)" onmouseover="showTip(event, 'fs32', 173)" class="fn">Linear</span><span class="pn">(</span><span class="n">512</span><span class="pn">,</span> <span class="n">256</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 174)" onmouseover="showTip(event, 'fs2', 174)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs40', 175)" onmouseover="showTip(event, 'fs40', 175)" class="id">leakyRelu</span><span class="pn">(</span><span class="n">0.2</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 176)" onmouseover="showTip(event, 'fs2', 176)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs44', 177)" onmouseover="showTip(event, 'fs44', 177)" class="id">dropout</span><span class="pn">(</span><span class="n">0.3</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs32', 178)" onmouseover="showTip(event, 'fs32', 178)" class="fn">Linear</span><span class="pn">(</span><span class="n">256</span><span class="pn">,</span> <span class="n">1</span><span class="pn">)</span>
<span class="pn">--></span> <span onmouseout="hideTip(event, 'fs2', 179)" onmouseover="showTip(event, 'fs2', 179)" class="rt">dsharp</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs45', 180)" onmouseover="showTip(event, 'fs45', 180)" class="id">sigmoid</span>
<span onmouseout="hideTip(event, 'fs48', 181)" onmouseover="showTip(event, 'fs48', 181)" class="fn">print</span> <span onmouseout="hideTip(event, 'fs49', 182)" onmouseover="showTip(event, 'fs49', 182)" class="id">generator</span>
<span onmouseout="hideTip(event, 'fs48', 183)" onmouseover="showTip(event, 'fs48', 183)" class="fn">print</span> <span onmouseout="hideTip(event, 'fs50', 184)" onmouseover="showTip(event, 'fs50', 184)" class="id">discriminator</span>
</code></pre>
<table class="pre"><tr><td><pre><code>Model(Linear(128, 256), Linear(256, 512), Linear(512, 1024), Linear(1024, 784))
Model(Linear(784, 1024), Linear(1024, 512), Linear(512, 256), Linear(256, 1))</code></pre></td></tr></table>
<div class="fsdocs-tip" id="fs1">namespace DiffSharp</div>
<div class="fsdocs-tip" id="fs2">type dsharp =
static member abs: input: Tensor -> Tensor
static member acos: input: Tensor -> Tensor
static member add: a: Tensor * b: Tensor -> Tensor
static member arange: endVal: float * ?startVal: float * ?step: float * ?device: Device * ?dtype: Dtype * ?backend: Backend -> Tensor + 1 overload
static member arangeLike: input: Tensor * endVal: float * ?startVal: float * ?step: float * ?device: Device * ?dtype: Dtype * ?backend: Backend -> Tensor + 1 overload
static member argmax: input: Tensor -> int[] + 1 overload
static member argmin: input: Tensor -> int[] + 1 overload
static member asin: input: Tensor -> Tensor
static member atan: input: Tensor -> Tensor
static member backends: unit -> Backend list
...<br /><em><summary>
Tensor operations
</summary></em></div>
<div class="fsdocs-tip" id="fs3">static member DiffSharp.dsharp.config: unit -> DiffSharp.Device * DiffSharp.Dtype * DiffSharp.Backend * DiffSharp.Printer<br />static member DiffSharp.dsharp.config: configuration: (DiffSharp.Device * DiffSharp.Dtype * DiffSharp.Backend * DiffSharp.Printer) -> unit<br />static member DiffSharp.dsharp.config: ?device: DiffSharp.Device * ?dtype: DiffSharp.Dtype * ?backend: DiffSharp.Backend * ?printer: DiffSharp.Printer -> unit</div>
<div class="fsdocs-tip" id="fs4">Multiple items<br />module Backend
from DiffSharp<br /><em><summary>
Contains functions and settings related to backend specifications.
</summary></em><br /><br />--------------------<br />type Backend =
| Reference
| Torch
| Other of name: string * code: int
override ToString: unit -> string
member Name: string<br /><em><summary>
Represents a backend for DiffSharp tensors
</summary></em></div>
<div class="fsdocs-tip" id="fs5">union case DiffSharp.Backend.Reference: DiffSharp.Backend<br /><em><summary>
The reference backend
</summary></em></div>
<div class="fsdocs-tip" id="fs6">static member DiffSharp.dsharp.seed: ?seed: int -> unit</div>
<div class="fsdocs-tip" id="fs7">namespace DiffSharp.Util</div>
<div class="fsdocs-tip" id="fs8">namespace DiffSharp.Data</div>
<div class="fsdocs-tip" id="fs9">val dataset: MNIST</div>
<div class="fsdocs-tip" id="fs10">Multiple items<br />type MNIST =
inherit Dataset
new: path: string * ?urls: seq<string> * ?train: bool * ?transform: (Tensor -> Tensor) * ?targetTransform: (Tensor -> Tensor) * ?n: int -> MNIST
override item: i: int -> Tensor * Tensor
member classNames: string[]
member classes: int
override length: int<br /><br />--------------------<br />new: path: string * ?urls: seq<string> * ?train: bool * ?transform: (Tensor -> Tensor) * ?targetTransform: (Tensor -> Tensor) * ?n: int -> MNIST</div>
<div class="fsdocs-tip" id="fs11">val id: x: 'T -> 'T<br /><em><summary>The identity function</summary><br /><param name="x">The input value.</param><br /><returns>The same value.</returns><br /><example id="id-example"><code lang="fsharp">
id 12 // Evaulates to 12
id "abc" // Evaulates to "abc"
</code></example></em></div>
<div class="fsdocs-tip" id="fs12">argument n: int option</div>
<div class="fsdocs-tip" id="fs13">val data: Tensor</div>
<div class="fsdocs-tip" id="fs14">val label: Tensor</div>
<div class="fsdocs-tip" id="fs15">val pngToHtml: fileName: string -> widthPixels: int -> string<br /><em><summary>
Given a PNG image file name, returns an HTML image element with the image content included as a Base64 encoded string
</summary></em></div>
<div class="fsdocs-tip" id="fs16">val printfn: format: Printf.TextWriterFormat<'T> -> 'T<br /><em><summary>Print to <c>stdout</c> using the given format, and add a newline.</summary><br /><param name="format">The formatter.</param><br /><returns>The formatted result.</returns><br /><example>See <c>Printf.printfn</c> (link: <see cref="M:Microsoft.FSharp.Core.PrintfModule.PrintFormatLine``1" />) for examples.</example></em></div>
<div class="fsdocs-tip" id="fs17">val loader: DataLoader</div>
<div class="fsdocs-tip" id="fs18">Multiple items<br />type DataLoader =
new: dataset: Dataset * batchSize: int * ?shuffle: bool * ?dropLast: bool * ?device: Device * ?dtype: Dtype * ?backend: Backend * ?targetDevice: Device * ?targetDtype: Dtype * ?targetBackend: Backend -> DataLoader
member batch: ?batchSize: int -> Tensor * Tensor
member epoch: ?numBatches: int -> seq<int * Tensor * Tensor>
member length: int<br /><br />--------------------<br />new: dataset: Dataset * batchSize: int * ?shuffle: bool * ?dropLast: bool * ?device: Device * ?dtype: Dtype * ?backend: Backend * ?targetDevice: Device * ?targetDtype: Dtype * ?targetBackend: Backend -> DataLoader</div>
<div class="fsdocs-tip" id="fs19">val batch: Tensor</div>
<div class="fsdocs-tip" id="fs20">val labels: Tensor</div>
<div class="fsdocs-tip" id="fs21">member DataLoader.batch: ?batchSize: int -> Tensor * Tensor</div>
<div class="fsdocs-tip" id="fs22">val epoch: int</div>
<div class="fsdocs-tip" id="fs23">val i: int</div>
<div class="fsdocs-tip" id="fs24">member DataLoader.epoch: ?numBatches: int -> seq<int * Tensor * Tensor></div>
<div class="fsdocs-tip" id="fs25">namespace DiffSharp.Model</div>
<div class="fsdocs-tip" id="fs26">module Compose
from DiffSharp</div>
<div class="fsdocs-tip" id="fs27">Multiple items<br />type Generator =
inherit Model
new: nz: int -> Generator
override forward: x: Tensor -> Tensor<br /><br />--------------------<br />new: nz: int -> Generator</div>
<div class="fsdocs-tip" id="fs28">val nz: int</div>
<div class="fsdocs-tip" id="fs29">Multiple items<br />val int: value: 'T -> int (requires member op_Explicit)<br /><em><summary>Converts the argument to signed 32-bit integer. This is a direct conversion for all
primitive numeric types. For strings, the input is converted using <c>Int32.Parse()</c>
with InvariantCulture settings. Otherwise the operation requires an appropriate
static conversion method on the input type.</summary><br /><param name="value">The input value.</param><br /><returns>The converted int</returns><br /><example id="int-example"><code lang="fsharp"></code></example></em><br /><br />--------------------<br />[<Struct>]
type int = int32<br /><em><summary>An abbreviation for the CLI type <see cref="T:System.Int32" />.</summary><br /><category>Basic Types</category></em><br /><br />--------------------<br />type int<'Measure> =
int<br /><em><summary>The type of 32-bit signed integer numbers, annotated with a unit of measure. The unit
of measure is erased in compiled code and when values of this type
are analyzed using reflection. The type is representationally equivalent to
<see cref="T:System.Int32" />.</summary><br /><category>Basic Types with Units of Measure</category></em></div>
<div class="fsdocs-tip" id="fs30">Multiple items<br />namespace DiffSharp.Model<br /><br />--------------------<br />type Model = Model<Tensor,Tensor><br /><br />--------------------<br />new: ?f: ('In -> 'Out) * ?parameters: seq<Parameter> * ?buffers: seq<Parameter> * ?models: seq<ModelBase> -> Model<'In,'Out></div>
<div class="fsdocs-tip" id="fs31">val fc1: Linear</div>
<div class="fsdocs-tip" id="fs32">Multiple items<br />type Linear =
inherit Model
new: inFeatures: int * outFeatures: int * ?bias: bool -> Linear
override ToString: unit -> string
override forward: value: Tensor -> Tensor
member bias: Tensor
member weight: Tensor<br /><em><summary>A model that applies a linear transformation to the incoming data: \(y = xA^T + b\)</summary></em><br /><br />--------------------<br />new: inFeatures: int * outFeatures: int * ?bias: bool -> Linear</div>
<div class="fsdocs-tip" id="fs33">val fc2: Linear</div>
<div class="fsdocs-tip" id="fs34">val fc3: Linear</div>
<div class="fsdocs-tip" id="fs35">val fc4: Linear</div>
<div class="fsdocs-tip" id="fs36">val self: Generator</div>
<div class="fsdocs-tip" id="fs37">val x: Tensor</div>
<div class="fsdocs-tip" id="fs38">static member dsharp.view: shape: seq<int> -> (Tensor -> Tensor)<br />static member dsharp.view: shape: int -> (Tensor -> Tensor)<br />static member dsharp.view: input: Tensor * shape: int -> Tensor<br />static member dsharp.view: input: Tensor * shape: seq<int> -> Tensor</div>
<div class="fsdocs-tip" id="fs39">override Linear.forward: value: Tensor -> Tensor</div>
<div class="fsdocs-tip" id="fs40">static member dsharp.leakyRelu: ?negativeSlope: float -> (Tensor -> Tensor)<br />static member dsharp.leakyRelu: input: Tensor * ?negativeSlope: float -> Tensor</div>
<div class="fsdocs-tip" id="fs41">static member dsharp.tanh: input: Tensor -> Tensor</div>
<div class="fsdocs-tip" id="fs42">Multiple items<br />type Discriminator =
inherit Model
new: nz: int -> Discriminator
override forward: x: Tensor -> Tensor<br /><br />--------------------<br />new: nz: int -> Discriminator</div>
<div class="fsdocs-tip" id="fs43">val self: Discriminator</div>
<div class="fsdocs-tip" id="fs44">static member dsharp.dropout: ?p: double -> (Tensor -> Tensor)<br />static member dsharp.dropout: input: Tensor * ?p: double -> Tensor</div>
<div class="fsdocs-tip" id="fs45">static member dsharp.sigmoid: input: Tensor -> Tensor</div>
<div class="fsdocs-tip" id="fs46">val gen: Generator</div>
<div class="fsdocs-tip" id="fs47">val dis: Discriminator</div>
<div class="fsdocs-tip" id="fs48">val print: x: 'a -> unit<br /><em><summary>
Print the given value to the console using the '%A' printf format specifier
</summary></em></div>
<div class="fsdocs-tip" id="fs49">val generator: Model<Tensor,Tensor></div>
<div class="fsdocs-tip" id="fs50">val discriminator: Model<Tensor,Tensor></div>
</div>
</div>
<div class="row">
<div class="span3"></div>
<div class="span9">
<hr>
<p style="height:50px; display: table-cell; vertical-align: bottom;">© Copyright 2021, DiffSharp Contributors.</p>
<br>
</div>
</div>
</div>
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
<script type="text/javascript">var fsdocs_search_baseurl = 'https://diffsharp.github.io/'</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.3.8/lunr.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.min.js"></script>
<script type="text/javascript" src="https://diffsharp.github.io/content/fsdocs-search.js"></script>
<!-- END SEARCH BOX: this adds support for the search box -->
</body>
</html>