Flux.jl/docs/src/data/onehot.md

55 lines
1.4 KiB
Markdown
Raw Normal View History

2017-09-11 12:40:11 +00:00
# One-Hot Encoding
It's common to encode categorical variables (like `true`, `false` or `cat`, `dog`) in "one-of-k" or ["one-hot"](https://en.wikipedia.org/wiki/One-hot) form. Flux provides the `onehot` function to make this easy.
```
2018-08-23 14:44:28 +00:00
julia> using Flux: onehot, onecold
2017-09-11 12:40:11 +00:00
julia> onehot(:b, [:a, :b, :c])
3-element Flux.OneHotVector:
false
true
false
julia> onehot(:c, [:a, :b, :c])
3-element Flux.OneHotVector:
false
false
true
```
2018-08-23 14:44:28 +00:00
The inverse is `onecold` (which can take a general probability distribution, as well as just booleans).
2017-09-11 12:40:11 +00:00
```julia
2018-08-23 14:44:28 +00:00
julia> onecold(ans, [:a, :b, :c])
2017-09-11 12:40:11 +00:00
:c
2018-08-23 14:44:28 +00:00
julia> onecold([true, false, false], [:a, :b, :c])
2017-09-11 12:40:11 +00:00
:a
2018-08-23 14:44:28 +00:00
julia> onecold([0.3, 0.2, 0.5], [:a, :b, :c])
2017-09-11 12:40:11 +00:00
:c
```
## Batches
2018-08-23 14:44:28 +00:00
`onehotbatch` creates a batch (matrix) of one-hot vectors, and `onecold` treats matrices as batches.
2017-09-11 12:40:11 +00:00
```julia
julia> using Flux: onehotbatch
julia> onehotbatch([:b, :a, :b], [:a, :b, :c])
3×3 Flux.OneHotMatrix:
false true false
true false true
false false false
julia> onecold(ans, [:a, :b, :c])
3-element Array{Symbol,1}:
:b
:a
:b
```
2017-09-12 12:04:47 +00:00
Note that these operations returned `OneHotVector` and `OneHotMatrix` rather than `Array`s. `OneHotVector`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.