</script><linkhref="https://fonts.googleapis.com/css?family=Lato|Roboto+Mono"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/fontawesome.min.css"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/solid.min.css"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/brands.min.css"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css"rel="stylesheet"type="text/css"/><script>documenterBaseURL="../.."</script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"data-main="../../assets/documenter.js"></script><scriptsrc="../../siteinfo.js"></script><scriptsrc="../../../versions.js"></script><linkhref="../../assets/flux.css"rel="stylesheet"type="text/css"/><linkclass="docs-theme-link"rel="stylesheet"type="text/css"href="../../assets/themes/documenter-dark.css"data-theme-name="documenter-dark"/><linkclass="docs-theme-link"rel="stylesheet"type="text/css"href="../../assets/themes/documenter-light.css"data-theme-name="documenter-light"data-theme-primary/><scriptsrc="../../assets/themeswap.js"></script></head><body><divid="documenter"><navclass="docs-sidebar"><divclass="docs-package-name"><spanclass="docs-autofit">Flux</span></div><formclass="docs-search"action="../../search/"><inputclass="docs-search-query"id="documenter-search-query"name="q"type="text"placeholder="Search docs"/></form><ulclass="docs-menu"><li><aclass="tocitem"href="../../">Home</a></li><li><spanclass="tocitem">Building Models</span><ul><li><aclass="tocitem"href="../basics/">Basics</a></li><li><aclass="tocitem"href="../recurrence/">Recurrence</a></li><li><aclass="tocitem"href="../regularisation/">Regularisation</a></li><liclass="is-active"><aclass="tocitem"href>Model Reference</a><ulclass="internal"><li><aclass="tocitem"href="#Basic-Layers-1"><span>Basic Layers</span></a></li><li><aclass="tocitem"href="#Convolution-and-Pooling-Layers-1"><span>Convolution and Pooling Layers</span></a></li><li><aclass="tocitem"href="#Recurrent-Layers-1"><span>Recurrent Layers</span></a></li><li><aclass="tocitem"href="#Other-General-Purpose-Layers-1"><span>Other General Purpose Layers</span></a></li><li><aclass="tocitem"href="#Normalisation-and-Regularisation-1"><span>Normalisation & Regularisation</span></a></li><li><aclass="tocitem"href="#Cost-Functions-1"><span>Cost Functions</span></a></li></ul></li><li><aclass="tocitem"href="../advanced/">Advanced Model Building</a></li><li><aclass="tocitem"href="../nnlib/">NNlib</a></li></ul></li><li><spanclass="tocitem">Handling Data</span><ul><li><aclass="tocitem"href="../../data/onehot/">One-Hot Encoding</a></li><li><aclass="tocitem"href="../../data/dataloader/">DataLoader</a></li></ul></li><li><spanclass="tocitem">Training Models</span><ul><li><aclass="tocitem"href="../../training/optimisers/">Optimisers</a></li><li><aclass="tocitem"href="../../training/training/">Training</a></li></ul></li><li><aclass="tocitem"href="../../gpu/">GPU Support</a></li><li><aclass="tocitem"href="../../saving/">Saving & Loading</a></li><li><aclass="tocitem"href="../../ecosystem/">The Julia Ecosystem</a></li><li><aclass="tocitem"href="../../utilities/">Utility Functions</a></li><li><aclass="tocitem"href="../../performance/">Performance Tips</a></li><li><aclass="tocitem"href="../../datasets/">Datasets</a></li><li><aclass="tocitem"href="../../community/">Community</a></li></ul><divclass="docs-version-selector field has-addons"><divclass="control"><spanclass="docs-label button is-static is-size-7">Version</span></div><divclass="docs-selector control is-expanded"><divclass="select is-fullwidth is-size-7"><selectid="documenter-version-selector"></select></div></div></div></nav><divclass="docs-main"><headerclass="docs-navbar"><navclass="breadcrumb"><ulclass="is-hidden-mobile"><li><aclass="is-disabled">Bu
julia> m(5) == 26
true
julia> m = Chain(Dense(10, 5), Dense(5, 2));
julia> x = rand(10);
julia> m(x) == m[2](m[1](x))
true</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/basic.jl#L1-L24">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.Dense"href="#Flux.Dense"><code>Flux.Dense</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">Dense(in::Integer, out::Integer, σ = identity)</code></pre><p>Create a traditional <code>Dense</code> layer with parameters <code>W</code> and <code>b</code>.</p><pre><codeclass="language-none">y = σ.(W * x .+ b)</code></pre><p>The input <code>x</code> must be a vector of length <code>in</code>, or a batch of vectors represented as an <code>in × N</code> matrix. The out <code>y</code> will be a vector or batch of length <code>out</code>.</p><p><strong>Examples</strong></p><p>```jldoctest; setup = :(using Random; Random.seed!(0)) julia> d = Dense(5, 2) Dense(5, 2)</p><p>julia> d(rand(5)) 2-element Array{Float32,1}: -0.16210233 0.12311903```</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/basic.jl#L85-L104">source</a></section></article><h2id="Convolution-and-Pooling-Layers-1"><aclass="docs-heading-anchor"href="#Convolution-and-Pooling-Layers-1">Convolution and Pooling Layers</a><aclass="docs-heading-anchor-permalink"href="#Convolution-and-Pooling-Layers-1"title="Permalink"></a></h2><p>These layers are used to build convolutional neural networks (CNNs).</p><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.Conv"href="#Flux.Conv"><code>Flux.Conv</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">Conv(size, in => out, σ = identity; init = glorot_uniform,
stride = 1, pad = 0, dilation = 1)</code></pre><p>Standard convolutional layer. <code>size</code> should be a tuple like <code>(2, 2)</code>. <code>in</code> and <code>out</code> specify the number of input and output channels respectively.</p><p>Data should be stored in WHCN order (width, height, # channels, batch size). In other words, a 100×100 RGB image would be a <code>100×100×3×1</code> array, and a batch of 50 would be a <code>100×100×3×50</code> array.</p><p><strong>Examples</strong></p><p>Apply a <code>Conv</code> layer to a 1-channel input using a 2×2 window size, giving us a 16-channel output. Output is activated with ReLU.</p><pre><codeclass="language-julia">size = (2,2)
Conv(size, in => out, relu)</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L10-L31">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.MaxPool"href="#Flux.MaxPool"><code>Flux.MaxPool</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">MaxPool(k; pad = 0, stride = k)</code></pre><p>Max pooling layer. <code>k</code> is the size of the window for each dimension of the input.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L357-L361">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.GlobalMaxPool"href="#Flux.GlobalMaxPool"><code>Flux.GlobalMaxPool</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">GlobalMaxPool()</code></pre><p>Global max pooling layer.</p><p>Transforms (w,h,c,b)-shaped input into (1,1,c,b)-shaped output, by performing max pooling on the complete (w,h)-shaped feature maps.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L307-L314">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.MeanPool"href="#Flux.MeanPool"><code>Flux.MeanPool</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">MeanPool(k; pad = 0, stride = k)</code></pre><p>Mean pooling layer. <code>k</code> is the size of the window for each dimension of the input.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L386-L390">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.GlobalMeanPool"href="#Flux.GlobalMeanPool"><code>Flux.GlobalMeanPool</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">GlobalMeanPool()</code></pre><p>Global mean pooling layer.</p><p>Transforms (w,h,c,b)-shaped input into (1,1,c,b)-shaped output, by performing mean pooling on the complete (w,h)-shaped feature maps.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L332-L339">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.DepthwiseConv"href="#Flux.DepthwiseConv"><code>Flux.DepthwiseConv</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">DepthwiseConv(size, in => out, σ = identity; init = glorot_uniform,
stride = 1, pad = 0, dilation = 1)</code></pre><p>Depthwise convolutional layer. <code>size</code> should be a tuple like <code>(2, 2)</code>. <code>in</code> and <code>out</code> specify the number of input and output channels respectively. Note that <code>out</code> must be an integer multiple of <code>in</code>.</p><p>Data should be stored in WHCN order (width, height, # channels, batch size). In other words, a 100×100 RGB image would be a <code>100×100×3×1</code> array, and a batch of 50 would be a <code>100×100×3×50</code> array.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L166-L177">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.ConvTranspose"href="#Flux.ConvTranspose"><code>Flux.ConvTranspose</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">ConvTranspose(size, in => out, σ = identity; init = glorot_uniform,
stride = 1, pad = 0, dilation = 1)</code></pre><p>Standard convolutional transpose layer. <code>size</code> should be a tuple like <code>(2, 2)</code>. <code>in</code> and <code>out</code> specify the number of input and output channels respectively.</p><p>Data should be stored in WHCN order (width, height, # channels, batch size). In other words, a 100×100 RGB image would be a <code>100×100×3×1</code> array, and a batch of 50 would be a <code>100×100×3×50</code> array.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L92-L102">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.CrossCor"href="#Flux.CrossCor"><code>Flux.CrossCor</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">CrossCor(size, in => out, σ = identity; init = glorot_uniform,
stride = 1, pad = 0, dilation = 1)</code></pre><p>Standard cross convolutional layer. <code>size</code> should be a tuple like <code>(2, 2)</code>. <code>in</code> and <code>out</code> specify the number of input and output channels respectively.</p><p>Data should be stored in WHCN order (width, height, # channels, batch size). In other words, a 100×100 RGB image would be a <code>100×100×3×1</code> array, and a batch of 50 would be a <code>100×100×3×50</code> array.</p><p><strong>Examples</strong></p><p>Apply a <code>CrossCor</code> layer to a 1-channel input using a 2×2 window size, giving us a 16-channel output. Output is activated with ReLU.</p><pre><codeclass="language-julia">size = (2,2)
CrossCor((2, 2), 1=>16, relu)</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/conv.jl#L232-L253">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.flatten"href="#Flux.flatten"><code>Flux.flatten</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">flatten(x::AbstractArray)</code></pre><p>Transform (w, h, c, b)-shaped input into (w × h × c, b)-shaped output by linearizing all values for each element in the batch.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L256-L261">source</a></section></article><h2id="Recurrent-Layers-1"><aclass="docs-heading-anchor"href="#Recurrent-Layers-1">Recurrent Layers</a><aclass="docs-heading-anchor-permalink"href="#Recurrent-Layers-1"title="Permalink"></a></h2><p>Much like the core layers above, but can be used to process sequence data (as well as other kinds of structured data).</p><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.RNN"href="#Flux.RNN"><code>Flux.RNN</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">RNN(in::Integer, out::Integer, σ = tanh)</code></pre><p>The most basic recurrent layer; essentially acts as a <code>Dense</code> layer, but with the output fed back into the input each time step.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/recurrent.jl#L91-L96">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.LSTM"href="#Flux.LSTM"><code>Flux.LSTM</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">LSTM(in::Integer, out::Integer)</code></pre><p><ahref="https://www.researchgate.net/publication/13853244_Long_Short-term_Memory">Long Short Term Memory</a> recurrent layer. Behaves like an RNN but generally exhibits a longer memory span over sequences.</p><p>See <ahref="https://colah.github.io/posts/2015-08-Understanding-LSTMs/">this article</a> for a good overview of the internals.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/recurrent.jl#L136-L144">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.GRU"href="#Flux.GRU"><code>Flux.GRU</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">GRU(in::Integer, out::Integer)</code></pre><p><ahref="https://arxiv.org/abs/1406.1078">Gated Recurrent Unit</a> layer. Behaves like an RNN but generally exhibits a longer memory span over sequences.</p><p>See <ahref="https://colah.github.io/posts/2015-08-Understanding-LSTMs/">this article</a> for a good overview of the internals.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/recurrent.jl#L177-L185">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.Recur"href="#Flux.Recur"><code>Flux.Recur</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">Recur(cell)</code></pre><p><code>Recur</code> takes a recurrent cell and makes it stateful, managing the hidden state in the background. <code>cell</code> should be a model of the form:</p><pre><codeclass="language-none">h, y = cell(h, x...)</code></pre><p>For example, here's a recurrent network that keeps a running total of its inputs:</p><pre><codeclass="language-julia">accum(h, x) = (h + x, x)
rnn.state # 60</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/recurrent.jl#L7-L26">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.reset!"href="#Flux.reset!"><code>Flux.reset!</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">reset!(rnn)</code></pre><p>Reset the hidden state of a recurrent layer back to its original value.</p><p>Assuming you have a <code>Recur</code> layer <code>rnn</code>, this is roughly equivalent to:</p><pre><codeclass="language-julia">rnn.state = hidden(rnn.cell)</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/recurrent.jl#L45-L54">source</a></section></article><h2id="Other-General-Purpose-Layers-1"><aclass="docs-heading-anchor"href="#Other-General-Purpose-Layers-1">Other General Purpose Layers</a><aclass="docs-heading-anchor-permalink"href="#Other-General-Purpose-Layers-1"title="Permalink"></a></h2><p>These are marginally more obscure than the Basic Layers. But in contrast to the layers described in the other sections are not readily grouped around a particular purpose (e.g. CNNs or RNNs).</p><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.Maxout"href="#Flux.Maxout"><code>Flux.Maxout</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">Maxout(over)</code></pre><p>The <ahref="https://arxiv.org/pdf/1302.4389.pdf">Maxout</a> layer has a number of internal layers which all receive the same input. It returns the elementwise maximum of the internal layers' outputs.</p><p>Maxout over linear dense layers satisfies the univeral approximation theorem.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/basic.jl#L183-L191">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.SkipConnection"href="#Flux.SkipConnection"><code>Flux.SkipConnection</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">SkipConnection(layer, connection)</code></pre><p>Create a skip connection which consists of a layer or <code>Chain</code> of consecutive layers and a shortcut connection linking the block's input to the output through a user-supplied 2-argument callable. The first argument to the callable will be propagated through the given <code>layer</code> while the second is the unchanged, "skipped" input.</p><p>The simplest "ResNet"-type connection is just <code>SkipConnection(layer, +)</code>, and requires the output of the layers to be the same shape as the input. Here is a more complicated example:</p><pre><codeclass="language-julia">m = Conv((3,3), 4=>7, pad=(1,1))
size(sm(x)) == (5, 5, 11, 10)</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/basic.jl#L226-L246">source</a></section></article><h2id="Normalisation-and-Regularisation-1"><aclass="docs-heading-anchor"href="#Normalisation-and-Regularisation-1">Normalisation & Regularisation</a><aclass="docs-heading-anchor-permalink"href="#Normalisation-and-Regularisation-1"title="Permalink"></a></h2><p>These layers don't affect the structure of the network but may improve training times or reduce overfitting.</p><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.normalise"href="#Flux.normalise"><code>Flux.normalise</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">normalise(x; dims=1)</code></pre><p>Normalise <code>x</code> to mean 0 and standard deviation 1 across the dimensions given by <code>dims</code>. Defaults to normalising over columns.</p><pre><codeclass="language-julia-repl">julia> a = reshape(collect(1:9), 3, 3)
ϵ = 1e-8, momentum = .1)</code></pre><p><ahref="https://arxiv.org/pdf/1502.03167.pdf">Batch Normalization</a> layer. <code>channels</code> should be the size of the channel dimension in your data (see below).</p><p>Given an array with <code>N</code> dimensions, call the <code>N-1</code>th the channel dimension. (For a batch of feature vectors this is just the data dimension, for <code>WHCN</code> images it's the usual channel dimension.)</p><p><code>BatchNorm</code> computes the mean and variance for each each <code>W×H×1×N</code> slice and shifts them to have a new mean and variance (corresponding to the learnable, per-channel <code>bias</code> and <code>scale</code> parameters).</p><p>Use <ahref="#Flux.testmode!"><code>testmode!</code></a> during inference.</p><p><strong>Examples</strong></p><pre><codeclass="language-julia">m = Chain(
softmax)</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/normalise.jl#L122-L149">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.dropout"href="#Flux.dropout"><code>Flux.dropout</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">dropout(x, p; dims = :)</code></pre><p>The dropout function. For each input, either sets that input to <code>0</code> (with probability <code>p</code>) or scales it by <code>1 / (1 - p)</code>. <code>dims</code> specifies the unbroadcasted dimensions, e.g. <code>dims=1</code> applies dropout along columns and <code>dims=2</code> along rows. This is used as a regularisation, i.e. it reduces overfitting during training.</p><p>See also the <ahref="#Flux.Dropout"><code>Dropout</code></a> layer.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/normalise.jl#L12-L21">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.Dropout"href="#Flux.Dropout"><code>Flux.Dropout</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">Dropout(p, dims = :)</code></pre><p>Dropout layer. In the forward pass, apply the <ahref="#Flux.dropout"><code>Flux.dropout</code></a> function on the input.</p><p>Does nothing to the input once <ahref="#Flux.testmode!"><code>Flux.testmode!</code></a> is <code>true</code>.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/normalise.jl#L30-L36">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.AlphaDropout"href="#Flux.AlphaDropout"><code>Flux.AlphaDropout</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">AlphaDropout(p)</code></pre><p>A dropout layer. Used in <ahref="https://papers.nips.cc/paper/6698-self-normalizing-neural-networks.pdf">Self-Normalizing Neural Networks</a>. The AlphaDropout layer ensures that mean and variance of activations remain the same as before.</p><p>Does nothing to the input once <ahref="#Flux.testmode!"><code>testmode!</code></a> is true.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/normalise.jl#L65-L74">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.LayerNorm"href="#Flux.LayerNorm"><code>Flux.LayerNorm</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">LayerNorm(h::Integer)</code></pre><p>A <ahref="https://arxiv.org/pdf/1607.06450.pdf">normalisation layer</a> designed to be used with recurrent hidden states of size <code>h</code>. Normalises the mean and standard deviation of each input before applying a per-neuron gain/bias.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/normalise.jl#L100-L106">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.InstanceNorm"href="#Flux.InstanceNorm"><code>Flux.InstanceNorm</code></a> — <spanclass="docstring-category">Type</span></header><section><div><pre><codeclass="language-julia">InstanceNorm(channels::Integer, σ = identity;
initβ = zeros, initγ = ones,
ϵ = 1e-8, momentum = .1)</code></pre><p><ahref="https://arxiv.org/abs/1607.08022">Instance Normalization</a> layer. <code>channels</code> should be the size of the channel dimension in your data (see below).</p><p>Given an array with <code>N</code> dimensions, call the <code>N-1</code>th the channel dimension. (For a batch of feature vectors this is just the data dimension, for <code>WHCN</code> images it's the usual channel dimension.)</p><p><code>InstanceNorm</code> computes the mean and variance for each each <code>W×H×1×1</code> slice and shifts them to have a new mean and variance (corresponding to the learnable, per-channel <code>bias</code> and <code>scale</code> parameters).</p><p>Use <ahref="#Flux.testmode!"><code>testmode!</code></a> during inference.</p><p><strong>Examples</strong></p><pre><codeclass="language-julia">m = Chain(
ϵ = 1f-5, momentum = 0.1f0)</code></pre><p><ahref="https://arxiv.org/pdf/1803.08494.pdf">Group Normalization</a> layer. This layer can outperform Batch Normalization and Instance Normalization.</p><p><code>chs</code> is the number of channels, the channel dimension of your input. For an array of N dimensions, the <code>N-1</code>th index is the channel dimension.</p><p><code>G</code> is the number of groups along which the statistics are computed. The number of channels must be an integer multiple of the number of groups.</p><p>Use <ahref="#Flux.testmode!"><code>testmode!</code></a> during inference.</p><p><strong>Examples</strong></p><pre><codeclass="language-julia">m = Chain(Conv((3,3), 1=>32, leakyrelu;pad = 1),
GroupNorm(32,16))
# 32 channels, 16 groups (G = 16), thus 2 channels per group used</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/normalise.jl#L313-L335">source</a></section></article><h3id="Testmode-1"><aclass="docs-heading-anchor"href="#Testmode-1">Testmode</a><aclass="docs-heading-anchor-permalink"href="#Testmode-1"title="Permalink"></a></h3><p>Many normalisation layers behave differently under training and inference (testing). By default, Flux will automatically determine when a layer evaluation is part of training or inference. Still, depending on your use case, it may be helpful to manually specify when these layers should be treated as being trained or not. For this, Flux provides <code>Flux.testmode!</code>. When called on a model (e.g. a layer or chain of layers), this function will place the model into the mode specified.</p><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.testmode!"href="#Flux.testmode!"><code>Flux.testmode!</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">testmode!(m, mode = true)</code></pre><p>Set a layer or model's test mode (see below). Using <code>:auto</code> mode will treat any gradient computation as training.</p><p><em>Note</em>: if you manually set a model into test mode, you need to manually place it back into train mode during training phase.</p><p>Possible values include:</p><ul><li><code>false</code> for training</li><li><code>true</code> for testing</li><li><code>:auto</code> or <code>nothing</code> for Flux to detect the mode automatically</li></ul></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/functor.jl#L42-L55">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.trainmode!"href="#Flux.trainmode!"><code>Flux.trainmode!</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">trainmode!(m, mode = true)</code></pre><p>Set a layer of model's train mode (see below). Symmetric to <ahref="#Flux.testmode!"><code>testmode!</code></a> (i.e. `trainmode!(m, mode) == testmode!(m, !mode)).</p><p><em>Note</em>: if you manually set a model into train mode, you need to manually place it into test mode during testing phase.</p><p>Possible values include:</p><ul><li><code>true</code> for training</li><li><code>false</code> for testing</li><li><code>:auto</code> or <code>nothing</code> for Flux to detect the mode automatically</li></ul></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/functor.jl#L58-L71">source</a></section></article><h2id="Cost-Functions-1"><aclass="docs-heading-anchor"href="#Cost-Functions-1">Cost Functions</a><aclass="docs-heading-anchor-permalink"href="#Cost-Functions-1"title="Permalink"></a></h2><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.mae"href="#Flux.mae"><code>Flux.mae</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">mae(ŷ, y)</code></pre><p>Return the mean of absolute error; calculated as <code>sum(abs.(ŷ .- y)) / length(y)</code>.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L2-L7">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.mse"href="#Flux.mse"><code>Flux.mse</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">mse(ŷ, y)</code></pre><p>Return the mean squared error between ŷ and y; calculated as <code>sum((ŷ .- y).^2) / length(y)</code>.</p><p><strong>Examples</strong></p><pre><codeclass="language-julia-rep
1//1</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L11-L22">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.msle"href="#Flux.msle"><code>Flux.msle</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">msle(ŷ, y; ϵ=eps(eltype(ŷ)))</code></pre><p>Return the mean of the squared logarithmic errors; calculated as <code>sum((log.(ŷ .+ ϵ) .- log.(y .+ ϵ)).^2) / length(y)</code>. The <code>ϵ</code> term provides numerical stability.</p><p>Penalizes an under-predicted estimate greater than an over-predicted estimate.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L26-L34">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.huber_loss"href="#Flux.huber_loss"><code>Flux.huber_loss</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">huber_loss(ŷ, y; δ=1.0)</code></pre><p>Return the mean of the <ahref="https://en.wikipedia.org/wiki/Huber_loss">Huber loss</a> given the prediction <code>ŷ</code> and true values <code>y</code>.</p><pre><codeclass="language-none"> | 0.5 * |ŷ - y|, for |ŷ - y| <= δ
Huber loss = |
| δ * (|ŷ - y| - 0.5 * δ), otherwise</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L39-L48">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.crossentropy"href="#Flux.crossentropy"><code>Flux.crossentropy</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">crossentropy(ŷ, y; weight = nothing)</code></pre><p>Return the cross entropy between the given probability distributions; calculated as <code>-sum(y .* log.(ŷ) .* weight) / size(y, 2)</code>.</p><p><code>weight</code> can be <code>Nothing</code>, a <code>Number</code> or an <code>AbstractVector</code>. <code>weight=nothing</code> acts like <code>weight=1</code> but is faster.</p><p>See also: <ahref="#Flux.logitcrossentropy"><code>Flux.logitcrossentropy</code></a>, <ahref="#Flux.binarycrossentropy"><code>Flux.binarycrossentropy</code></a>, <ahref="#Flux.logitbinarycrossentropy"><code>Flux.logitbinarycrossentropy</code></a></p><p><strong>Examples</strong></p><pre><codeclass="language-julia-repl">julia> Flux.crossentropy(softmax([-1.1491, 0.8619, 0.3127]), [1, 1, 0])
3.085467254747739</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L68-L84">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.logitcrossentropy"href="#Flux.logitcrossentropy"><code>Flux.logitcrossentropy</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">logitcrossentropy(ŷ, y; weight = 1)</code></pre><p>Return the crossentropy computed after a <ahref="../nnlib/#NNlib.logsoftmax"><code>Flux.logsoftmax</code></a> operation; calculated as <code>-sum(y .* logsoftmax(ŷ) .* weight) / size(y, 2)</code>.</p><p><code>logitcrossentropy(ŷ, y)</code> is mathematically equivalent to <ahref="#Flux.crossentropy"><code>Flux.crossentropy(softmax(log(ŷ)), y)</code></a> but it is more numerically stable.</p><p>See also: <ahref="#Flux.crossentropy"><code>Flux.crossentropy</code></a>, <ahref="#Flux.binarycrossentropy"><code>Flux.binarycrossentropy</code></a>, <ahref="#Flux.logitbinarycrossentropy"><code>Flux.logitbinarycrossentropy</code></a></p><p><strong>Examples</strong></p><pre><codeclass="language-julia-repl">julia> Flux.logitcrossentropy([-1.1491, 0.8619, 0.3127], [1, 1, 0])
3.085467254747738</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L87-L103">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.binarycrossentropy"href="#Flux.binarycrossentropy"><code>Flux.binarycrossentropy</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">binarycrossentropy(ŷ, y; ϵ=eps(ŷ))</code></pre><p>Return <span>$-y*\log(ŷ + ϵ) - (1-y)*\log(1-ŷ + ϵ)$</span>. The <code>ϵ</code> term provides numerical stability.</p><p>Typically, the prediction <code>ŷ</code> is given by the output of a <ahref="../nnlib/#NNlib.sigmoid"><code>sigmoid</code></a> activation.</p><p>See also: <ahref="#Flux.crossentropy"><code>Flux.crossentropy</code></a>, <ahref="#Flux.logitcrossentropy"><code>Flux.logitcrossentropy</code></a>, <ahref="#Flux.logitbinarycrossentropy"><code>Flux.logitbinarycrossentropy</code></a></p><p><strong>Examples</strong></p><pre><codeclass="language-julia-repl">julia> Flux.binarycrossentropy.(σ.([-1.1491, 0.8619, 0.3127]), [1, 1, 0])
3-element Array{Float64,1}:
1.424397097347566
0.35231664672364077
0.8616703662235441</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L108-L125">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.logitbinarycrossentropy"href="#Flux.logitbinarycrossentropy"><code>Flux.logitbinarycrossentropy</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">logitbinarycrossentropy(ŷ, y)</code></pre><p><code>logitbinarycrossentropy(ŷ, y)</code> is mathematically equivalent to <ahref="#Flux.binarycrossentropy"><code>Flux.binarycrossentropy(σ(log(ŷ)), y)</code></a> but it is more numerically stable.</p><p>See also: <ahref="#Flux.crossentropy"><code>Flux.crossentropy</code></a>, <ahref="#Flux.logitcrossentropy"><code>Flux.logitcrossentropy</code></a>, <ahref="#Flux.binarycrossentropy"><code>Flux.binarycrossentropy</code></a></p><p><strong>Examples</strong></p><pre><codeclass="language-julia-repl">julia> Flux.logitbinarycrossentropy.([-1.1491, 0.8619, 0.3127], [1, 1, 0])
3-element Array{Float64,1}:
1.4243970973475661
0.35231664672364094
0.8616703662235443</code></pre></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L131-L147">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.kldivergence"href="#Flux.kldivergence"><code>Flux.kldivergence</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">kldivergence(ŷ, y)</code></pre><p>Return the <ahref="https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence">Kullback-Leibler divergence</a> between the given probability distributions.</p><p>KL divergence is a measure of how much one probability distribution is different from the other. It is always non-negative and zero only when both the distributions are equal everywhere.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L185-L196">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.poisson"href="#Flux.poisson"><code>Flux.poisson</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">poisson(ŷ, y)</code></pre><p>Return how much the predicted distribution <code>ŷ</code> diverges from the expected Poisson distribution <code>y</code>; calculated as <code>sum(ŷ .- y .* log.(ŷ)) / size(y, 2)</code>.</p><p><ahref="https://peltarion.com/knowledge-center/documentation/modeling-view/build-an-ai-model/loss-functions/poisson">More information.</a>.</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L203-L210">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.hinge"href="#Flux.hinge"><code>Flux.hinge</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">hinge(ŷ, y)</code></pre><p>Return the <ahref="https://en.wikipedia.org/wiki/Hinge_loss">hinge loss</a> given the prediction <code>ŷ</code> and true labels <code>y</code> (containing 1 or -1); calculated as <code>sum(max.(0, 1 .- ŷ .* y)) / size(y, 2)</code>.</p><p>See also: <ahref="#Flux.squared_hinge"><code>squared_hinge</code></a></p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L213-L221">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.squared_hinge"href="#Flux.squared_hinge"><code>Flux.squared_hinge</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">squared_hinge(ŷ, y)</code></pre><p>Return the squared hinge loss given the prediction <code>ŷ</code> and true labels <code>y</code> (containing 1 or -1); calculated as <code>sum((max.(0, 1 .- ŷ .* y)).^2) / size(y, 2)</code>.</p><p>See also: <ahref="#Flux.hinge"><code>hinge</code></a></p></div><aclass="docs-sourcelink"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/layers/stateless.jl#L224-L231">source</a></section></article><articleclass="docstring"><header><aclass="docstring-binding"id="Flux.dice_coeff_loss"href="#Flux.dice_coeff_loss"><code>Flux.dice_coeff_loss</code></a> — <spanclass="docstring-category">Function</span></header><section><div><pre><codeclass="language-julia">dice_coeff_loss(ŷ, y; smooth=1)</code></pre><p>Return a loss based on the dice coefficient. Used in the <ahref="https://arxiv.org/pdf/1606.04797v1.pdf">V-Net</a> image segmentation architecture. Similar to the F1_score. Calculated as: 1 - 2<em>sum(|ŷ .</em> y| + smooth) / (sum(ŷ.^2) + sum(y.^2) + smooth)`</p></div><aclass="docs-sourcelink"target="_blank"href="https://github.