</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="../../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"action="../search.html"><inputid="search-query"name="q"type="text"placeholder="Search docs"/></form><ul><li><aclass="toctext"href="../index.html">Home</a></li><li><spanclass="toctext">Building Models</span><ul><li><aclass="toctext"href="../models/basics.html">Basics</a></li><li><aclass="toctext"href="../models/recurrence.html">Recurrence</a></li><li><aclass="toctext"href="../models/layers.html">Layer Reference</a></li></ul></li><li><spanclass="toctext">Training Models</span><ul><li><aclass="toctext"href="optimisers.html">Optimisers</a></li><liclass="current"><aclass="toctext"href="training.html">Training</a><ulclass="internal"><li><aclass="toctext"href="#Loss-Functions-1">Loss Functions</a></li><li><aclass="toctext"href="#Callbacks-1">Callbacks</a></li></ul></li></ul></li><li><aclass="toctext"href="../contributing.html">Contributing & Help</a></li></ul></nav><articleid="docs"><header><nav><ul><li>Training Models</li><li><ahref="training.html">Training</a></li></ul><aclass="edit-page"href="https://github.com/FluxML/Flux.jl/tree/3f83be7bb7cdcdf56ec19514d0639ae85d6c9c56/docs/src/training/training.md"><spanclass="fa"></span> Edit on GitHub</a></nav><hr/><divid="topbar"><span>Training</span><aclass="fa fa-bars"href="#"></a></div></header><p>To actually train a model we need three things:</p><ul><li><p>A <em>loss function</em>, that evaluates how well a model is doing given some input data.</p></li><li><p>A collection of data points that will be provided to the loss function.</p></li><li><p>An <ahref="optimisers.html">optimiser</a> that will update the model parameters appropriately.</p></li></ul><p>With these we can call <code>Flux.train!</code>:</p><pre><codeclass="language-julia">Flux.train!(loss, data, opt)</code></pre><p>There are plenty of examples in the <ahref="https://github.com/FluxML/model-zoo">model zoo</a>.</p><h2><aclass="nav-anchor"id="Loss-Functions-1"href="#Loss-Functions-1">Loss Functions</a></h2><p>The <code>loss</code> that we defined in <ahref="../models/basics.html">basics</a> is completely valid for training. We can also define a loss in terms of some model:</p><pre><codeclass="language-julia">m = Chain(
loss(x, y) = Flux.mse(m(x), y)</code></pre><p>The loss will almost always be defined in terms of some <em>cost function</em> that measures the distance of the prediction <code>m(x)</code> from the target <code>y</code>. Flux has several of these built in, like <code>mse</code> for mean squared error or <code>logloss</code> for cross entropy loss, but you can calculate it however you want.</p><h2><aclass="nav-anchor"id="Callbacks-1"href="#Callbacks-1">Callbacks</a></h2><p><code>train!</code> takes an additional argument, <code>cb</code>, that's used for callbacks so that you can observe the training process. For example:</p><pre><codeclass="language-julia">train!(loss, data, opt, cb = () -> println("training"))</code></pre><p>Callbacks are called for every batch of training data. You can slow this down using <code>Flux.throttle(f, timeout)</code> which prevents <code>f</code> from being called more than once every <code>timeout</code> seconds.</p><p>A more typical callback might look like this:</p><pre><codeclass="language-julia">test_x, test_y = # ... create single batch of test data ...
evalcb() = @show(loss(test_x, test_y))
Flux.train!(loss, data, opt,
cb = throttle(evalcb, 5))</code></pre><footer><hr/><aclass="previous"href="optimisers.html"><spanclass="direction">Previous</span><spanclass="title">Optimisers</span></a><aclass="next"href="../contributing.html"><spanclass="direction">Next</span><spanclass="title">Contributing & Help</span></a></footer></article></body></html>