</script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css"rel="stylesheet"type="text/css"/><linkhref="https://fonts.googleapis.com/css?family=Lato|Roboto+Mono"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"rel="stylesheet"type="text/css"/><script>documenterBaseURL="../.."</script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"data-main="../../assets/documenter.js"></script><scriptsrc="../../siteinfo.js"></script><scriptsrc="../../../versions.js"></script><linkhref="../../assets/documenter.css"rel="stylesheet"type="text/css"/><linkhref="../../assets/flux.css"rel="stylesheet"type="text/css"/></head><body><navclass="toc"><h1>Flux</h1><selectid="version-selector"onChange="window.location.href=this.value"style="visibility: hidden"></select><formclass="search"id="search-form"action="../../search/"><inputid="search-query"name="q"type="text"placeholder="Search docs"/></form><ul><li><aclass="toctext"href="../../">Home</a></li><li><spanclass="toctext">Building Models</span><ul><li><aclass="toctext"href="../basics/">Basics</a></li><li><aclass="toctext"href="../recurrence/">Recurrence</a></li><li><aclass="toctext"href="../regularisation/">Regularisation</a></li><liclass="current"><aclass="toctext"href>Model Reference</a><ulclass="internal"><li><aclass="toctext"href="#Basic-Layers-1">Basic Layers</a></li><li><aclass="toctext"href="#Convolution-and-Pooling-Layers-1">Convolution and Pooling Layers</a></li><li><aclass="toctext"href="#Recurrent-Layers-1">Recurrent Layers</a></li><li><aclass="toctext"href="#Other-General-Purpose-Layers-1">Other General Purpose Layers</a></li><li><aclass="toctext"href="#Normalisation-and-Regularisation-1">Normalisation & Regularisation</a></li><li><aclass="toctext"href="#Cost-Functions-1">Cost Functions</a></li></ul></li><li><aclass="toctext"href="../nnlib/">NNlib</a></li></ul></li><li><spanclass="toctext">Handling Data</span><ul><li><aclass="toctext"href="../../data/onehot/">One-Hot Encoding</a></li><li><aclass="toctext"href="../../data/dataloader/">DataLoader</a></li></ul></li><li><spanclass="toctext">Training Models</span><ul><li><aclass="toctext"href="../../training/optimisers/">Optimisers</a></li><li><aclass="toctext"href="../../training/training/">Training</a></li></ul></li><li><aclass="toctext"href="../../gpu/">GPU Support</a></li><li><aclass="toctext"href="../../saving/">Saving & Loading</a></li><li><aclass="toctext"href="../../ecosystem/">The Julia Ecosystem</a></li><li><aclass="toctext"href="../../performance/">Performance Tips</a></li><li><aclass="toctext"href="../../community/">Community</a></li></ul></nav><articleid="docs"><header><nav><ul><li>Building Models</li><li><ahref>Model Reference</a></li></ul><aclass="edit-page"href="https://github.com/FluxML/Flux.jl/blob/master/docs/src/models/layers.md"><spanclass="fa"></span> Edit on GitHub</a></nav><hr/><divid="topbar"><span>Model Reference</span><aclass="fa fa-bars"href="#"></a></div></header><h2><aclass="nav-anchor"id="Basic-Layers-1"href="#Basic-Layers-1">Basic Layers</a></h2><p>These core layers form the foundation of almost all neural networks.</p><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.Chain"href="#Flux.Chain"><code>Flux.Chain</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">Chain(layers...)</code></pre><p>Chain multiple layers / functions together, so that they are called in sequence on a given input.</p><pre><codeclass="language-julia">m = Chain(x -> x^2, x -> x+1)
m(x) == m[2](m[1](x))</code></pre><p><code>Chain</code> also supports indexing and slicing, e.g. <code>m[2]</code> or <code>m[1:end-1]</code>. <code>m[1:3](x)</code> will calculate the output of the first three layers.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/basic.jl#L1-L18">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.Dense"href="#Flux.Dense"><code>Flux.Dense</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">Dense(in::Integer, out::Integer, σ = identity)</code></pre><p>Creates 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><pre><codeclass="language-julia">julia> d = Dense(5, 2)
-0.00449443</code></pre></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/basic.jl#L78-L97">source</a></section><h2><aclass="nav-anchor"id="Convolution-and-Pooling-Layers-1"href="#Convolution-and-Pooling-Layers-1">Convolution and Pooling Layers</a></h2><p>These layers are used to build convolutional neural networks (CNNs).</p><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.Conv"href="#Flux.Conv"><code>Flux.Conv</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">Conv(size, in=>out)
Conv(size, in=>out, relu)</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>Example: Applying Conv layer to a 1-channel input using a 2x2 window size, giving us a 16-channel output. Output is activated with ReLU.</p><pre><codeclass="language-none">size = (2,2)
Conv((2, 2), 1=>16, relu)</code></pre><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>Takes the keyword arguments <code>pad</code>, <code>stride</code> and <code>dilation</code>.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/conv.jl#L10-L30">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.MaxPool"href="#Flux.MaxPool"><code>Flux.MaxPool</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">MaxPool(k)</code></pre><p>Max pooling layer. <code>k</code> stands for the size of the window for each dimension of the input.</p><p>Takes the keyword arguments <code>pad</code> and <code>stride</code>.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/conv.jl#L307-L313">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.MeanPool"href="#Flux.MeanPool"><code>Flux.MeanPool</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">MeanPool(k)</code></pre><p>Mean pooling layer. <code>k</code> stands for the size of the window for each dimension of the input.</p><p>Takes the keyword arguments <code>pad</code> and <code>stride</code>.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/conv.jl#L338-L344">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.DepthwiseConv"href="#Flux.DepthwiseConv"><code>Flux.DepthwiseConv</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">DepthwiseConv(size, in=>out)
DepthwiseConv(size, in=>out, relu)</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. In other words, a 100×100 RGB image would be a <code>100×100×3</code> array, and a batch of 50 would be a <code>100×100×3×50</code> array.</p><p>Takes the keyword arguments <code>pad</code>, <code>stride</code> and <code>dilation</code>.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/conv.jl#L166-L178">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.ConvTranspose"href="#Flux.ConvTranspose"><code>Flux.ConvTranspose</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">ConvTranspose(size, in=>out)
ConvTranspose(size, in=>out, relu)</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. In other words, a 100×100 RGB image would be a <code>100×100×3</code> array, and a batch of 50 would be a <code>100×100×3×50</code> array.</p><p>Takes the keyword arguments <code>pad</code>, <code>stride</code> and <code>dilation</code>.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/conv.jl#L91-L102">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.CrossCor"href="#Flux.CrossCor"><code>Flux.CrossCor</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">CrossCor(size, in=>out)
CrossCor(size, in=>out, relu)</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>Example: Applying CrossCor layer to a 1-channel input using a 2x2 window size, giving us a 16-channel output. Output is activated with ReLU.</p><pre><codeclass="language-none">size = (2,2)
CrossCor((2, 2), 1=>16, relu)</code></pre><p>Data should be stored in WHCN order (width, height, # channels, # batches). 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>Takes the keyword arguments <code>pad</code>, <code>stride</code> and <code>dilation</code>.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/conv.jl#L233-L253">source</a></section><h2><aclass="nav-anchor"id="Recurrent-Layers-1"href="#Recurrent-Layers-1">Recurrent Layers</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><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.RNN"href="#Flux.RNN"><code>Flux.RNN</code></a> — <spanclass="docstring-category">Function</span>.</div><div><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></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/recurrent.jl#L90-L95">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.LSTM"href="#Flux.LSTM"><code>Flux.LSTM</code></a> — <spanclass="docstring-category">Function</span>.</div><div><div><pre><codeclass="language-julia">LSTM(in::Integer, out::Integer)</code></pre><p>Long Short Term Memory 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></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/recurrent.jl#L135-L143">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.GRU"href="#Flux.GRU"><code>Flux.GRU</code></a> — <spanclass="docstring-category">Function</span>.</div><div><div><pre><codeclass="language-julia">GRU(in::Integer, out::Integer)</code></pre><p>Gated Recurrent Unit 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></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/recurrent.jl#L176-L184">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.Recur"href="#Flux.Recur"><code>Flux.Recur</code></a> — <spanclass="docstring-category">Type</span>.</div><div><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></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/recurrent.jl#L7-L26">source</a></section><h2><aclass="nav-anchor"id="Other-General-Purpose-Layers-1"href="#Other-General-Purpose-Layers-1">Other General Purpose Layers</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><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.Maxout"href="#Flux.Maxout"><code>Flux.Maxout</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">Maxout(over)</code></pre><p><code>Maxout</code> is a neural network layer, which has a number of internal layers, which all have the same input, and the maxout returns the elementwise maximium of the internal layers' outputs.</p><p>Maxout over linear dense layers satisfies the univeral approximation theorem.</p><p>Reference: Ian J. Goodfellow, David Warde-Farley, Mehdi Mirza, Aaron Courville, and Yoshua Bengio.</p><ol><li>Maxout networks.</li></ol><p>In Proceedings of the 30th International Conference on International Conference on Machine Learning - Volume 28 (ICML'13), Sanjoy Dasgupta and David McAllester (Eds.), Vol. 28. JMLR.org III-1319-III-1327. https://arxiv.org/pdf/1302.4389.pdf</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/basic.jl#L176-L191">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.SkipConnection"href="#Flux.SkipConnection"><code>Flux.SkipConnection</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">SkipConnection(layers, connection)</code></pre><p>Creates a Skip Connection, of a layer or <code>Chain</code> of consecutive layers plus a shortcut connection. The connection function will combine the result of the layers with the original input, to give the final output.</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-none">m = Conv((3,3), 4=>7, pad=(1,1))
size(sm(x)) == (5, 5, 11, 10)</code></pre></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/basic.jl#L225-L243">source</a></section><h2><aclass="nav-anchor"id="Normalisation-and-Regularisation-1"href="#Normalisation-and-Regularisation-1">Normalisation & Regularisation</a></h2><p>These layers don't affect the structure of the network but may improve training times or reduce overfitting.</p><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.BatchNorm"href="#Flux.BatchNorm"><code>Flux.BatchNorm</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">BatchNorm(channels::Integer, σ = identity;
ϵ = 1e-8, momentum = .1)</code></pre><p>Batch Normalization layer. The <code>channels</code> input 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>See <ahref="https://arxiv.org/pdf/1502.03167.pdf">Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift</a>.</p><p>Example:</p><pre><codeclass="language-julia">m = Chain(
softmax)</code></pre></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/normalise.jl#L118-L148">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.Dropout"href="#Flux.Dropout"><code>Flux.Dropout</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">Dropout(p, dims = :)</code></pre><p>A Dropout layer. In the forward pass, applies the <ahref="#Flux.dropout"><code>dropout</code></a> function on the input.</p><p>Does nothing to the input once <ahref="#Flux.testmode!"><code>testmode!</code></a> is false.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/normalise.jl#L30-L36">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.dropout"href="#Flux.dropout"><code>Flux.dropout</code></a> — <spanclass="docstring-category">Function</span>.</div><div><div><pre><codeclass="language-julia">dropout(p, dims = :)</code></pre><p>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>. The <code>dims</code> argument is to specify the unbroadcasted dimensions, i.e. <code>dims=1</code> does 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 <ahref="#Flux.Dropout"><code>Dropout</code></a>.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/normalise.jl#L12-L21">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.AlphaDropout"href="#Flux.AlphaDropout"><code>Flux.AlphaDropout</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><pre><codeclass="language-julia">AlphaDropout(p)</code></pre><p>A dropout layer. It is used in Self-Normalizing Neural Networks. (https://papers.nips.cc/paper/6698-self-normalizing-neural-networks.pdf) The AlphaDropout layer ensures that mean and variance of activations remains the same as before.</p><p>Does nothing to the input once <ahref="#Flux.testmode!"><code>testmode!</code></a> is false.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/normalise.jl#L62-L70">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.LayerNorm"href="#Flux.LayerNorm"><code>Flux.LayerNorm</code></a> — <spanclass="docstring-category">Type</span>.</div><div><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/stddev of each input before applying a per-neuron gain/bias.</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/normalise.jl#L96-L102">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.GroupNorm"href="#Flux.GroupNorm"><code>Flux.GroupNorm</code></a> — <spanclass="docstring-category">Type</span>.</div><div><div><p>Group Normalization. This layer can outperform Batch-Normalization and Instance-Normalization.</p><pre><codeclass="language-none">GroupNorm(chs::Integer, G::Integer, λ = identity;
ϵ = 1f-5, momentum = 0.1f0)</code></pre><p><span>$chs$</span> is the number of channels, the channel dimension of your input. For an array of N dimensions, the (N-1)th index is the channel dimension.</p><p><span>$G$</span> is the number of groups along which the statistics would be 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>Example:</p><pre><codeclass="language-none">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><p>Link : https://arxiv.org/pdf/1803.08494.pdf</p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/normalise.jl#L309-L332">source</a></section><h3><aclass="nav-anchor"id="Testmode-1"href="#Testmode-1">Testmode</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>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><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.testmode!"href="#Flux.testmode!"><code>Flux.testmode!</code></a> — <spanclass="docstring-category">Function</span>.</div><div><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></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/functor.jl#L42-L55">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.trainmode!"href="#Flux.trainmode!"><code>Flux.trainmode!</code></a> — <spanclass="docstring-category">Function</span>.</div><div><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></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/functor.jl#L58-L71">source</a></section><h2><aclass="nav-anchor"id="Cost-Functions-1"href="#Cost-Functions-1">Cost Functions</a></h2><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.mse"href="#Flux.mse"><code>Flux.mse</code></a> — <spanclass="docstring-category">Function</span>.</div><div><div><pre><codeclass="language-julia">mse(ŷ, y)</code></pre><p>Return the mean squared error <code>sum((ŷ .- y).^2) / length(y)</code>. </p></div></div><aclass="source-link"target="_blank"href="https://github.com/FluxML/Flux.jl/blob/df73b8b8fb3a99308057866f99565fb89366651b/src/layers/stateless.jl#L2-L6">source</a></section><sectionclass="docstring"><divclass="docstring-header"><aclass="docstring-binding"id="Flux.crossentropy"href="#Flux.crossentropy"><code>Flux.crossentropy</code></a> — <spanclass="docstring-category">Function</span>.</div><div><div><pre><codeclass="language-julia">crossentropy(ŷ, y; weight=1)</code></pre><p>Return the crossentropy computed as <code>-sum(y .* log.(ŷ) .* weight) / size(y, 2)</code>. </p><p>See also <ahref="#Flux.logitcrossentropy"><code>logitcrossentropy</code></a>, <ahref="#Flux.binarycrossentropy"><code>binarycrossentropy</code