Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Epsilon change in normalise for stability #2421

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/layers/stateless.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

"""
normalise(x; dims=ndims(x), eps=1e-5)
normalise(x; dims=ndims(x), eps=1f-5)

Normalise `x` to mean 0 and standard deviation 1 across the dimension(s) given by `dims`.
Per default, `dims` is the last dimension.
`eps` is a small term added to the denominator for numerical stability.
`eps` is a small term added to the variance for numerical stability.

# Examples
```jldoctest
Expand Down Expand Up @@ -34,10 +34,11 @@ julia> isapprox(std(y; dims=1, corrected=false), ones(1, 10), atol=1e-5)
true
```
"""
@inline function normalise(x::AbstractArray; dims=ndims(x), eps=ofeltype(x, 1e-5))
@inline function normalise(x::AbstractArray; dims=ndims(x), eps=1f-5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this now assume Float32? Elsewhere we try to allow for Float16 too.

μ = mean(x, dims=dims)
σ = std(x, dims=dims, mean=μ, corrected=false)
return @. (x - μ) / (σ + eps)
σ² = var(x, dims=dims, mean=μ, corrected=false)
ε = ofeltype(x, eps)
return @. (x - μ) / sqrt(σ² + ε^2)
end

"""
Expand Down
Loading