package owl-base

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file owl_neural_graph_sig.ml

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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# 1 "src/base/neural/owl_neural_graph_sig.ml"
(*
 * OWL - OCaml Scientific and Engineering Computing
 * Copyright (c) 2016-2020 Liang Wang <liang.wang@cl.cam.ac.uk>
 *)

module type Sig = sig
  module Neuron : Owl_neural_neuron_sig.Sig

  open Neuron
  open Neuron.Optimise
  open Neuron.Optimise.Algodiff

  (** {6 Type definition} *)

  type node =
    { mutable name : string
    ; mutable prev : node array
    ; mutable next : node array
    ; mutable neuron : neuron
    ; mutable output : t option
    ; mutable network : network
    ; mutable train : bool
    }

  and network =
    { mutable nnid : string
    ; mutable size : int
    ; mutable roots : node array
    ; mutable outputs : node array
    ; mutable topo : node array
    }
  (** Type definition of a node and a neural network. *)

  (** {6 Manipulate networks} *)

  val make_network : ?nnid:string -> int -> node array -> node array -> network
  (** Create an empty neural network. *)

  val make_node
    :  ?name:string
    -> ?train:bool
    -> node array
    -> node array
    -> neuron
    -> t option
    -> network
    -> node
  (** Create a node in a neural network. *)

  val get_roots : network -> node array
  (** Get the roots of the neural network. *)

  val get_outputs : network -> node array
  (** Get the outputs of the neural network. *)

  val get_node : network -> string -> node
  (** Get a node in a network with the given name. *)

  val get_network : ?name:string -> node -> network
  (** Get the neural network of a given node associated with. *)

  val outputs : ?name:string -> node array -> network
  (** Get the neural network associated with the given output nodes. *)

  val get_network_name : network -> string
  (** ``get_network_name n`` returns the name of the network ``n``. *)

  val set_network_name : network -> string -> unit
  (** ``set_network_name n s`` sets the name of the network ``n`` to ``s``. *)

  val collect_output : node array -> t array
  (** Collect the output values of given nodes. *)

  val connect_pair : node -> node -> unit
  (** Connect two nodes in a neural network. *)

  val connect_to_parents : node array -> node -> unit
  (** Connect a node to a list of parents. *)

  val add_node : ?act_typ:Activation.typ -> network -> node array -> node -> node
  (** Add a node to the given network. *)

  val input_shape : network -> int array
  (** Get input shape of a network (without batch dimension), i.e. shape of input neuron. *)

  val input_shapes : network -> int array array
  (** Get input shapes of a network (without batch dimension), i.e. shape of input neurons. *)

  (** {6 Interface to optimisation engine} *)

  val init : network -> unit
  (** Initialise the network. *)

  val reset : network -> unit
  (** Reset the network, i.e. all the paramters in the neurons. *)

  val mktag : int -> network -> unit
  (** Tag the neurons, used by ``Algodiff`` module. *)

  val mkpar : network -> t array array
  (** Collect the parameters of neurons, used by ``Optimise`` module. *)

  val mkpri : network -> t array array
  (** Collect the primal values of neurons, used by ``Optimise`` module. *)

  val mkadj : network -> t array array
  (** Collect the adjacent values of neurons, used by ``Optimise`` module. *)

  val update : network -> t array array -> unit
  (** Update the parameters of neurons, used by ``Optimise`` module. *)

  val run : t -> network -> t
  (** Execute the computations in all the neurons in a network with the given input. *)

  val run_inputs : t array -> network -> t array
  (** Execute the computations in all the neurons in a network with the given inputs. *)

  val forward : network -> t -> t * t array array
  (** Run the forward pass of a network. *)

  val forward_inputs : network -> t array -> t array * t array array
  (** Run the forward pass of a network (multi-input/output version). *)

  val backward : network -> t -> t array array * t array array
  (** Run the backward pass of a network. *)

  val copy : network -> network
  (** Make a deep copy of the given network. *)

  val model : network -> A.arr -> A.arr
  (** Make a deep copy of the given network, excluding the neurons marked with ``training = true``. *)

  val model_inputs : network -> A.arr array -> A.arr array
  (** Make a deep copy of the given network, excluding the neurons marked with ``training = true``. *)

  (** {6 Create Neurons} *)

  val input : ?name:string -> int array -> node
  (**
``input shape`` creates an input node for input data. Note that if your network
has multiple inputs, you should use ``inputs`` instead.

Arguments:
  * ``shape``: shape of input data.
  *)

  val inputs : ?names:string array -> int array array -> node array
  (**
``input shapes`` creates an array of input nodes for input data.

Arguments:
  * ``shapes``: array of shapes of input data.
  *)

  val activation : ?name:string -> Activation.typ -> node -> node
  (**
Applies an activation function to an output.

Arguments:
  * ``activation``: name of activation function to use.
  *)

  val linear
    :  ?name:string
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int
    -> node
    -> node
  (**
``linear ?act_typ units node`` adds the regular densely-connected NN node to
``node``.

Arguments:
  * ``units``: Positive integer, dimensionality of the output space.
  * ``act_typ``: Activation function to use.
  *)

  val linear_nobias
    :  ?name:string
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int
    -> node
    -> node
  (**
Similar to ``linear``, but does not use the bias vector.
  *)

  val embedding
    :  ?name:string
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int
    -> int
    -> node
    -> node
  (** Create a node for embedding neuron. *)

  val recurrent
    :  ?name:string
    -> ?init_typ:Init.typ
    -> act_typ:Activation.typ
    -> int
    -> int
    -> node
    -> node
  (** Create a node for recurrent neuron. *)

  val lstm : ?name:string -> ?init_typ:Init.typ -> int -> node -> node
  (**
``lstm units node`` adds a LSTM node on previous ``node``.

Arguments:
  * ``units``: Positive integer, dimensionality of the output space.
  *)

  val gru : ?name:string -> ?init_typ:Init.typ -> int -> node -> node
  (**
``gru units node`` adds a Gated Recurrent Unit node on previous ``node``.

Arguments:
  * ``units``: Positive integer, dimensionality of the output space.
  *)

  val conv1d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``conv1d kernel stride node`` adds a 1D convolution node (e.g. temporal
convolution) on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``h, i, o``. ``h`` specifies the dimension of the 1D convolution window. ``i`` and ``o`` are the dimensionalities of the input and output space.
  * ``stride``: int array of 1 integer.
  *)

  val conv2d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``conv2d kernel stride node`` adds a 2D convolution node (e.g. spatial convolution over images) on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``w, h, i, o``. ``w`` and ``h`` specify the width and height of the 2D convolution window. ``i`` and ``o`` are the dimensionality of the input and output space.
  * ``stride``: int array of 2 integers.
  *)

  val conv3d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``conv3d kernel stride node`` adds a 3D convolution node (e.g. spatial
convolution over volumes) on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``w, h, d, i, o``. ``w``, ``h``, and ``d`` specify the 3 dimensionality of the 3D convolution window. ``i`` and ``o`` are the dimensionality of the input and output space.
  * ``stride``: int array of 3 integers.
  *)

  val dilated_conv1d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> int array
    -> node
    -> node
  (**
``dilated_conv1d kernel stride rate node`` adds a 1D dilated convolution node (e.g. temporal convolution) on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``h, i, o``. ``h`` specifies the dimension of the 1D convolution window. ``i`` and ``o`` are the dimensionalities of the input and output space.
  * ``stride``: int array of 1 integer.
  * ``rate``: int array of 1 integer.
  *)

  val dilated_conv2d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> int array
    -> node
    -> node
  (**
``dilated_conv2d kernel stride rate node`` adds a 2D dilated convolution node (e.g. spatial convolution over images) on previous ``node``.

Arguments:
  * ``kernel`: int array consists of ``w, h, i, o``. ``w`` and ``h`` specify the width and height of the 2D convolution window. ``i`` and ``o`` are the dimensionality of the input and output space.
  * ``stride``: int array of 2 integers.
  * ``rate``: int array of 2 integers.
  *)

  val dilated_conv3d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> int array
    -> node
    -> node
  (**
``dilated_conv3d kernel stride rate node`` adds a 3D dilated convolution node (e.g. spatial convolution over volumes) on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``w, h, d, i, o``. ``w``, ``h``, and ``d`` specify the 3 dimensionality of the 3D convolution window. ``i`` and ``o`` are the dimensionality of the input and output space.
  * ``stride``: int array of 3 integers.
  * ``rate``: int array of 3 integers.
  *)

  val transpose_conv1d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``transpose_conv1d kernel stride node`` adds a 1D transpose convolution node (e.g. temporal convolution) on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``h, i, o``. ``h`` specifies the dimension of the 1D convolution window. ``i`` and ``o`` are the dimensionalities of the input and output space.
  * ``stride``: int array of 1 integer.
  *)

  val transpose_conv2d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``transpose_conv2d kernel stride node`` adds a 2D transpose convolution node on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``w, h, i, o``. ``w`` and ``h`` specify the width and height of the 2D convolution window. ``i`` and ``o`` are the dimensionality of the input and output space.
  * ``stride``: int array of 2 integers.
  *)

  val transpose_conv3d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``transpose_conv3d kernel stride node`` adds a 3D transpose convolution node (e.g. spatial convolution over volumes) on previous ``node``.

Arguments:
  * ``kernel``: int array consists of ``w, h, d, i, o``. ``w``, ``h``, and ``d`` specify the 3 dimensionality of the 3D convolution window. ``i`` and ``o`` are the dimensionality of the input and output space.
  * ``stride``: int array of 3 integers.
  *)

  val fully_connected
    :  ?name:string
    -> ?init_typ:Init.typ
    -> ?act_typ:Activation.typ
    -> int
    -> node
    -> node
  (**
``fully_connected outputs node`` adds a fully connected node to ``node``.

Arguments:
  * ``outputs``: integer, the number of output units in the node.
  *)

  val max_pool1d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``max_pool1d ~padding ~act_typ pool_size stride node`` adds a max pooling
operation for temporal data to ``node``.

Arguments:
  * ``pool_size``: Array of one integer, size of the max pooling windows.
  * ``stride``: Array of one integer, factor by which to downscale.
  *)

  val max_pool2d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``max_pool2d ~padding ~act_typ pool_size stride node`` adds a max pooling
operation for spatial data to ``node``.

Arguments:
  * ``pool_size``: Array of 2 integers, size of the max pooling windows.
  * ``stride``: Array of 2 integers, factor by which to downscale.
  *)

  val avg_pool1d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``avg_pool1d ~padding ~act_typ pool_size stride node`` adds a average pooling
operation for temporal data to ``node``.

Arguments:
  * ``pool_size``: Array of one integer, size of the max pooling windows.
  * ``stride``: Array of one integer, factor by which to downscale.
  *)

  val avg_pool2d
    :  ?name:string
    -> ?padding:Owl_types.padding
    -> ?act_typ:Activation.typ
    -> int array
    -> int array
    -> node
    -> node
  (**
``avg_pool2d ~padding ~act_typ pool_size stride node`` adds a average pooling operation for spatial data to ``node``.

Arguments:
  * ``pool_size``: Array of 2 integers, size of the max pooling windows.
  * ``stride``: Array of 2 integers, factor by which to downscale.
  *)

  val global_max_pool1d : ?name:string -> ?act_typ:Activation.typ -> node -> node
  (**
``global_max_pool1d`` adds global max pooling operation for temporal data.
  *)

  val global_max_pool2d : ?name:string -> ?act_typ:Activation.typ -> node -> node
  (**
``global_max_poo2d`` global max pooling operation for spatial data.
  *)

  val global_avg_pool1d : ?name:string -> ?act_typ:Activation.typ -> node -> node
  (**
``global_avg_pool1d`` adds global average pooling operation for temporal data.
  *)

  val global_avg_pool2d : ?name:string -> ?act_typ:Activation.typ -> node -> node
  (**
``global_avg_poo2d`` global average pooling operation for spatial data.
  *)

  val upsampling2d : ?name:string -> ?act_typ:Activation.typ -> int array -> node -> node
  (**
``upsampling2d ~act_typ size node`` adds a upsampling operation for spatial data to ``node``.

Arguments:
  * ``size``: array of two integers, namely the upsampling factors for columns and rows.
  *)

  val padding2d
    :  ?name:string
    -> ?act_typ:Activation.typ
    -> int array array
    -> node
    -> node
  (**
``padding2d ~act_typ padding node`` adds rows and columns of zeros at the top, bottom, left and right side of an image tensor.

Arguments:
  * ``padding``: array of 2 arrays of 2 integers, interpreted as  [| [|top_pad; bottom_pad|]; [|left_pad; right_pad|]|].
  *)

  val dropout : ?name:string -> float -> node -> node
  (**
``dropout rate node`` applies Dropout to the input to prevent overfitting.

Arguments:
  * ``rate``: float between 0 and 1. Fraction of the input units to drop.
  *)

  val gaussian_noise : ?name:string -> float -> node -> node
  (**
``gaussian_noise stddev node`` applies additive zero-centered Gaussian noise.

Arguments:
  * ``stddev``: float, standard deviation of the noise distribution.
  *)

  val gaussian_dropout : ?name:string -> float -> node -> node
  (**
``gaussian_dropout rate node`` applies multiplicative 1-centered Gaussian noise.
Only active at training time.

Arguments:
  * ``rates``: float, drop probability
  *)

  val alpha_dropout : ?name:string -> float -> node -> node
  (**
``alpha_dropout rate node`` applies Alpha Dropout to the input ``node``.
Only active at training time.

Arguments:
  * ``rates``: float, drop probability
  *)

  val normalisation
    :  ?name:string
    -> ?axis:int
    -> ?training:bool
    -> ?decay:float
    -> ?mu:A.arr
    -> ?var:A.arr
    -> node
    -> node
  (**
``normalisation axis node`` normalise the activations of the previous node at
each batch.

Arguments:
  * ``axis``:  Integer, the axis that should be normalised (typically the features axis). Default value is 0.
  *)

  val reshape : ?name:string -> int array -> node -> node
  (**
``reshape target_shape node`` reshapes an output to a certain shape.

Arguments:
  * ``target_shape``: target shape. Array of integers. Does not include the batch axis.
  *)

  val flatten : ?name:string -> node -> node
  (**
``flatten node`` flattens the input. Does not affect the batch size.
  *)

  val lambda
    :  ?name:string
    -> ?act_typ:Activation.typ
    -> ?out_shape:int array
    -> (t -> t)
    -> node
    -> node
  (**
``lambda ?target_shape func node`` wraps arbitrary expression as a Node object.

Arguments:
  * ``func``: The function to be evaluated. Takes input tensor as first argument.
  * ``target_shape``: the shape of the tensor returned by ``func``; set to the same as input shape if not specified.
  *)

  val lambda_array
    :  ?name:string
    -> ?act_typ:Activation.typ
    -> int array
    -> (t array -> t)
    -> node array
    -> node
  (**
``lambda_array target_shape func node`` wraps arbitrary expression as a Node object.

Arguments:
  * ``target_shape``: the shape of the tensor returned by ``func``.
  * ``func``: The function to be evaluated. Takes input tensor array as first argument.
  *)

  val add : ?name:string -> ?act_typ:Activation.typ -> node array -> node
  (**
Node that adds a list of inputs.

It takes as input an array of nodes, all of the same shape, and returns a
single node (also of the same shape).
  *)

  val mul : ?name:string -> ?act_typ:Activation.typ -> node array -> node
  (**
Node that multiplies (element-wise) a list of inputs.

It takes as input an array of nodes, all of the same shape, and returns a
single node (also of the same shape).
  *)

  val dot : ?name:string -> ?act_typ:Activation.typ -> node array -> node
  (**
  Node that computes a dot product between samples in two nodes.
  *)

  val max : ?name:string -> ?act_typ:Activation.typ -> node array -> node
  (**
Node that computes the maximum (element-wise) a list of inputs.
  *)

  val average : ?name:string -> ?act_typ:Activation.typ -> node array -> node
  (**
Node that averages a list of inputs.

It takes as input an array of nodes, all of the same shape, and returns a
single node (also of the same shape).
  *)

  val concatenate : ?name:string -> ?act_typ:Activation.typ -> int -> node array -> node
  (**
``concatenate axis nodes`` concatenates a array of ``nodes`` and return as a single node.

Arguments:
  * ``axis``: Axis along which to concatenate.
  *)

  (** {6 Helper functions} *)

  val to_string : network -> string
  (** Convert a neural network to its string representation. *)

  val pp_network : Format.formatter -> network -> unit
    [@@ocaml.toplevel_printer]
  (** Pretty printing function a neural network. *)

  val print : network -> unit
  (** Print the string representation of a neural network to the standard output. *)

  val save : ?unsafe:bool -> network -> string -> unit
  (** Serialise a network and save it to the a file with the given name.
  Set the unsafe flag to true if network contains Lambda layer. *)

  val load : string -> network
  (** Load the neural network from a file with the given name. *)

  val save_weights : network -> string -> unit
  (**
Save all the weights in a neural network to a file. The weights and the name of
their associated neurons are saved as key-value pairs in a hash table.
  *)

  val load_weights : network -> string -> unit
  (**
Load the weights from a file of the given name. Note that the weights and the
name of their associated neurons are saved as key-value pairs in a hash table.
  *)

  val get_subnetwork : ?make_inputs:string array -> node -> network
  (**
   Constructs a subnetwork of nodes on which ``node`` depends, replacing
   nodes with names in ``make_inputs`` with input nodes. If ``make_inputs``
   is empty or unspecified, the original input nodes are used.
   
   Note: the weights in the new network are the references of those in
   the old one.
   *)

  (** {6 Train Networks} *)

  val train_generic
    :  ?state:Checkpoint.state
    -> ?params:Params.typ
    -> ?init_model:bool
    -> network
    -> t
    -> t
    -> Checkpoint.state
  (** Generic function of training a neural network. *)

  val train
    :  ?state:Checkpoint.state
    -> ?params:Params.typ
    -> ?init_model:bool
    -> network
    -> A.arr
    -> A.arr
    -> Checkpoint.state
  (** Train a neural network with various configurations. *)
end
OCaml

Innovation. Community. Security.