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

dropout may return incorrect results due to improper use of inbounds #655

Open
yurivish opened this issue Jan 20, 2021 · 0 comments
Open

Comments

@yurivish
Copy link

yurivish commented Jan 20, 2021

The dropout! function applies @inbounds to a for loop iterating over 1:length(x) where x is not guaranteed to have standard indexing. As a consequence the function can return incorrect results and silently access out-of-bounds memory.

@inbounds for i=1:length(y)
if y[i] > p
y[i] = x[i] / q
else
y[i] = 0
end
end

A concrete example is with OffsetArrays:

julia> using Knet, OffsetArrays

julia> a = Float64[n for n in 1:21];

julia> o = OffsetArray(a, -10:10);

julia> dropout(o, .5, drop=true)
21-element OffsetArray(::Array{Float64,1}, -10:10) with eltype Float64 with indices -10:10:
  0.5071590064611642
  0.8322953335779582
  0.3388610612978913
  0.9177628344956359
  0.5919343644328279
  0.6557221967728748
  0.14356432793391516
  0.596784239077887
  0.5427223931847711
  0.5626176563028811
  0.8176537879294099
  0.0
  0.0
  0.0
  0.0
  0.0
  0.0
 36.0
  0.0
  0.0
  0.0

When an array with nonstandard indexing is passed to dropout, similar(array) is passed to dropout!. similar for OffsetArrays returns another OffsetArray and dropout access indices 1:10 (valid) and 11:21 (invalid), while leaving uninitialized memory with arbitrary values at indices -10:0 as seen above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant