`src/layers/stateless.jl`: Add missing docstrings

This commit is contained in:
janEbert 2019-08-31 11:08:25 +02:00
parent 9b68423e64
commit 2f955a33cd
1 changed files with 31 additions and 6 deletions

View File

@ -10,7 +10,14 @@ mae(ŷ, y) = sum(abs.(ŷ .- y)) * 1 // length(y)
"""
mse(, y)
Return the mean squared error `sum((ŷ .- y).^2) / length(y)`.
Return the mean squared error between and y;
defined as ``\\frac{1}{n} \\sum_{i=1}^n (ŷ_i - y_i)^2``.
# Examples
```jldoctest
julia> Flux.mse([0, 2], [1, 1])
1//1
```
"""
mse(, y) = sum(( .- y).^2) * 1 // length(y)
@ -58,22 +65,40 @@ function _crossentropy(ŷ::AbstractVecOrMat, y::AbstractVecOrMat, weight::Abstr
end
"""
crossentropy(, y; weight=1)
crossentropy(, y; weight = nothing)
Return the crossentropy computed as `-sum(y .* log.(ŷ) .* weight) / size(y, 2)`.
Return the cross entropy between the given probability distributions;
computed as `-sum(y .* log.(ŷ) .* weight) / size(y, 2)`.
`weight` can be `Nothing`, a `Number` or an `AbstractVector`.
`weight=nothing` acts like `weight=1` but is faster.
See also [`logitcrossentropy`](@ref), [`binarycrossentropy`](@ref).
# Examples
```jldoctest
julia> Flux.crossentropy(softmax([-1.1491, 0.8619, 0.3127]), [1, 1, 0])
3.085467254747739
```
"""
crossentropy(::AbstractVecOrMat, y::AbstractVecOrMat; weight=nothing) = _crossentropy(, y, weight)
"""
logitcrossentropy(, y; weight = 1)
Return the crossentropy computed after a [softmax](@ref) operation:
Return the crossentropy computed after a [`logsoftmax`](@ref) operation;
computed as `-sum(y .* logsoftmax(ŷ) .* weight) / size(y, 2)`.
-sum(y .* logsoftmax() .* weight) / size(y, 2)
`logitcrossentropy(ŷ, y)` is mathematically equivalent to
[`crossentropy(softmax(log(ŷ)), y)`](@ref) but it is more numerically stable.
See also [`crossentropy`](@ref), [`binarycrossentropy`](@ref).
# Examples
```jldoctest
julia> Flux.logitcrossentropy([-1.1491, 0.8619, 0.3127], [1, 1, 0])
3.085467254747738
```
"""
function logitcrossentropy(::AbstractVecOrMat, y::AbstractVecOrMat; weight = 1)
return -sum(y .* logsoftmax() .* weight) * 1 // size(y, 2)