denizyuret/Knet.jl

dropout may return incorrect results due to improper use of inbounds

yurivish opened this issue · 0 comments

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.