</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="../training/optimisers.html">Optimisers</a></li><li><aclass="toctext"href="../training/training.html">Training</a></li></ul></li><liclass="current"><aclass="toctext"href="onehot.html">One-Hot Encoding</a><ulclass="internal"><li><aclass="toctext"href="#Batches-1">Batches</a></li></ul></li><li><aclass="toctext"href="../gpu.html">GPU Support</a></li><li><aclass="toctext"href="../contributing.html">Contributing & Help</a></li></ul></nav><articleid="docs"><header><nav><ul><li><ahref="onehot.html">One-Hot Encoding</a></li></ul><aclass="edit-page"href="https://github.com/FluxML/Flux.jl/tree/d3419c943bd8c8c3d06ffa8ea5618625b3b14613/docs/src/data/onehot.md"><spanclass="fa"></span> Edit on GitHub</a></nav><hr/><divid="topbar"><span>One-Hot Encoding</span><aclass="fa fa-bars"href="#"></a></div></header><h1><aclass="nav-anchor"id="One-Hot-Encoding-1"href="#One-Hot-Encoding-1">One-Hot Encoding</a></h1><p>It's common to encode categorical variables (like <code>true</code>, <code>false</code> or <code>cat</code>, <code>dog</code>) in "one-of-k" or <ahref="https://en.wikipedia.org/wiki/One-hot">"one-hot"</a> form. Flux provides the <code>onehot</code> function to make this easy.</p><pre><codeclass="language-none">julia> using Flux: onehot
true</code></pre><p>The inverse is <code>argmax</code> (which can take a general probability distribution, as well as just booleans).</p><pre><codeclass="language-julia">julia> argmax(ans, [:a, :b, :c])
:c</code></pre><h2><aclass="nav-anchor"id="Batches-1"href="#Batches-1">Batches</a></h2><p><code>onehotbatch</code> creates a batch (matrix) of one-hot vectors, and <code>argmax</code> treats matrices as batches.</p><pre><codeclass="language-julia">julia> using Flux: onehotbatch
:b</code></pre><p>Note that these operations returned <code>OneHotVector</code> and <code>OneHotMatrix</code> rather than <code>Array</code>s. <code>OneHotVector</code>s behave like normal vectors but avoid any unnecessary cost compared to using an integer index directly. For example, multiplying a matrix with a one-hot vector simply slices out the relevant row of the matrix under the hood.</p><footer><hr/><aclass="previous"href="../training/training.html"><spanclass="direction">Previous</span><spanclass="title">Training</span></a><aclass="next"href="../gpu.html"><spanclass="direction">Next</span><spanclass="title">GPU Support</span></a></footer></article></body></html>