123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721# 1 "src/base/neural/owl_neural_graph_sig.ml"(*
* OWL - OCaml Scientific Computing
* Copyright (c) 2016-2022 Liang Wang <liang@ocaml.xyz>
*)moduletypeSig=sigmoduleNeuron:Owl_neural_neuron_sig.SigopenNeuronopenNeuron.OptimiseopenNeuron.Optimise.Algodiff(** {5 Type definition} *)typenode={mutablename:string;mutableprev:nodearray;mutablenext:nodearray;mutableneuron:neuron;mutableoutput:toption;mutablenetwork:network;mutabletrain:bool}andnetwork={mutablennid:string;mutablesize:int;mutableroots:nodearray;mutableoutputs:nodearray;mutabletopo:nodearray}(** Type definition of a node and a neural network. *)(** {5 Manipulate networks} *)valmake_network:?nnid:string->int->nodearray->nodearray->network(** Create an empty neural network. *)valmake_node:?name:string->?train:bool->nodearray->nodearray->neuron->toption->network->node(** Create a node in a neural network. *)valget_roots:network->nodearray(** Get the roots of the neural network. *)valget_outputs:network->nodearray(** Get the outputs of the neural network. *)valget_node:network->string->node(** Get a node in a network with the given name. *)valget_network:?name:string->node->network(** Get the neural network of a given node associated with. *)valoutputs:?name:string->nodearray->network(** Get the neural network associated with the given output nodes. *)valget_network_name:network->string(** [get_network_name n] returns the name of the network [n]. *)valset_network_name:network->string->unit(** [set_network_name n s] sets the name of the network [n] to [s]. *)valcollect_output:nodearray->tarray(** Collect the output values of given nodes. *)valconnect_pair:node->node->unit(** Connect two nodes in a neural network. *)valconnect_to_parents:nodearray->node->unit(** Connect a node to a list of parents. *)valadd_node:?act_typ:Activation.typ->network->nodearray->node->node(** Add a node to the given network. *)valinput_shape:network->intarray(** Get input shape of a network (without batch dimension), i.e. shape of input neuron. *)valinput_shapes:network->intarrayarray(** Get input shapes of a network (without batch dimension), i.e. shape of input neurons. *)(** {5 Interface to optimisation engine} *)valinit:network->unit(** Initialise the network. *)valreset:network->unit(** Reset the network, i.e. all the parameters in the neurons. *)valmktag:int->network->unit(** Tag the neurons, used by [Algodiff] module. *)valmkpar:network->tarrayarray(** Collect the parameters of neurons, used by [Optimise] module. *)valmkpri:network->tarrayarray(** Collect the primal values of neurons, used by [Optimise] module. *)valmkadj:network->tarrayarray(** Collect the adjacent values of neurons, used by [Optimise] module. *)valupdate:network->tarrayarray->unit(** Update the parameters of neurons, used by [Optimise] module. *)valrun:t->network->t(** Execute the computations in all the neurons in a network with the given input. *)valrun_inputs:tarray->network->tarray(** Execute the computations in all the neurons in a network with the given inputs. *)valforward:network->t->t*tarrayarray(** Run the forward pass of a network. *)valforward_inputs:network->tarray->tarray*tarrayarray(** Run the forward pass of a network (multi-input/output version). *)valbackward:network->t->tarrayarray*tarrayarray(** Run the backward pass of a network. *)valcopy:network->network(** Make a deep copy of the given network. *)valmodel:network->A.arr->A.arr(** Make a deep copy of the given network, excluding the neurons marked with [training = true]. *)valmodel_inputs:network->A.arrarray->A.arrarray(** Make a deep copy of the given network, excluding the neurons marked with [training = true]. *)(** {5 Create Neurons} *)valinput:?name:string->intarray->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.
*)valinputs:?names:stringarray->intarrayarray->nodearray(**
[input shapes] creates an array of input nodes for input data.
Arguments:
* [shapes]: array of shapes of input data.
*)valactivation:?name:string->Activation.typ->node->node(**
Applies an activation function to an output.
Arguments:
* [activation]: name of activation function to use.
*)vallinear:?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.
*)vallinear_nobias:?name:string->?init_typ:Init.typ->?act_typ:Activation.typ->int->node->node(**
Similar to [linear], but does not use the bias vector.
*)valembedding:?name:string->?init_typ:Init.typ->?act_typ:Activation.typ->int->int->node->node(** Create a node for embedding neuron. *)valrecurrent:?name:string->?init_typ:Init.typ->act_typ:Activation.typ->int->int->node->node(** Create a node for recurrent neuron. *)vallstm:?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.
*)valgru:?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.
*)valconv1d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->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.
*)valconv2d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->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.
*)valconv3d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->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.
*)valdilated_conv1d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->intarray->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.
*)valdilated_conv2d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->intarray->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.
*)valdilated_conv3d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->intarray->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.
*)valtranspose_conv1d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->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.
*)valtranspose_conv2d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->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.
*)valtranspose_conv3d:?name:string->?padding:Owl_types.padding->?init_typ:Init.typ->?act_typ:Activation.typ->intarray->intarray->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.
*)valfully_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.
*)valmax_pool1d:?name:string->?padding:Owl_types.padding->?act_typ:Activation.typ->intarray->intarray->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.
*)valmax_pool2d:?name:string->?padding:Owl_types.padding->?act_typ:Activation.typ->intarray->intarray->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.
*)valavg_pool1d:?name:string->?padding:Owl_types.padding->?act_typ:Activation.typ->intarray->intarray->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.
*)valavg_pool2d:?name:string->?padding:Owl_types.padding->?act_typ:Activation.typ->intarray->intarray->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.
*)valglobal_max_pool1d:?name:string->?act_typ:Activation.typ->node->node(**
[global_max_pool1d] adds global max pooling operation for temporal data.
*)valglobal_max_pool2d:?name:string->?act_typ:Activation.typ->node->node(**
[global_max_poo2d] global max pooling operation for spatial data.
*)valglobal_avg_pool1d:?name:string->?act_typ:Activation.typ->node->node(**
[global_avg_pool1d] adds global average pooling operation for temporal data.
*)valglobal_avg_pool2d:?name:string->?act_typ:Activation.typ->node->node(**
[global_avg_poo2d] global average pooling operation for spatial data.
*)valupsampling2d:?name:string->?act_typ:Activation.typ->intarray->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.
*)valpadding2d:?name:string->?act_typ:Activation.typ->intarrayarray->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|]|].
*)valdropout:?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.
*)valgaussian_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.
*)valgaussian_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
*)valalpha_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
*)valnormalisation:?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.
*)valreshape:?name:string->intarray->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.
*)valflatten:?name:string->node->node(**
[flatten node] flattens the input. Does not affect the batch size.
*)valslice:?name:string->intlistlist->node->node(**
[slice node] slices the input. Does not affect the batch size.
*)vallambda:?name:string->?act_typ:Activation.typ->?out_shape:intarray->(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.
*)vallambda_array:?name:string->?act_typ:Activation.typ->intarray->(tarray->t)->nodearray->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.
*)valadd:?name:string->?act_typ:Activation.typ->nodearray->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).
*)valmul:?name:string->?act_typ:Activation.typ->nodearray->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).
*)valdot:?name:string->?act_typ:Activation.typ->nodearray->node(**
Node that computes a dot product between samples in two nodes.
*)valmax:?name:string->?act_typ:Activation.typ->nodearray->node(**
Node that computes the maximum (element-wise) a list of inputs.
*)valaverage:?name:string->?act_typ:Activation.typ->nodearray->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).
*)valconcatenate:?name:string->?act_typ:Activation.typ->int->nodearray->node(**
[concatenate axis nodes] concatenates a array of [nodes] and return as a single node.
Arguments:
* [axis]: Axis along which to concatenate.
*)(** {5 Helper functions} *)valto_string:network->string(** Convert a neural network to its string representation. *)valpp_network:Format.formatter->network->unit[@@ocaml.toplevel_printer](** Pretty printing function a neural network. *)valprint:network->unit(** Print the string representation of a neural network to the standard output. *)valsave:?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. *)valload:string->network(** Load the neural network from a file with the given name. *)valsave_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.
*)valload_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.
*)valmake_subnetwork:?copy:bool->?make_inputs:stringarray->network->stringarray->network(**
[get_subnetwork ?copy ?make_inputs network output_names] constructs a
subnetwork of nodes on which [output_names] depend, replacing nodes with
names in [make_inputs] with input nodes.
Arguments:
[copy]: Whether to copy or reference the original node weights. Defaults to true.
[make_inputs]: Names of nodes to use as inputs to the subnetwork. Defaults to [||], which uses the original inputs.
[nn]: The neural network from which the subnetwork is constructed.
[output_names]: Names of nodes to use as outputs.
*)(** {5 Train Networks} *)valtrain_generic:?state:Checkpoint.state->?params:Params.typ->?init_model:bool->network->t->t->Checkpoint.state(** Generic function of training a neural network. *)valtrain:?state:Checkpoint.state->?params:Params.typ->?init_model:bool->network->A.arr->A.arr->Checkpoint.state(** Train a neural network with various configurations. *)end