2017-05-01 15:57:51 +00:00
|
|
|
# Arrays
|
|
|
|
|
2017-02-02 04:39:41 +00:00
|
|
|
initn(dims...) = randn(dims...)/100
|
2016-08-25 16:25:33 +00:00
|
|
|
|
2017-09-06 22:58:55 +00:00
|
|
|
flatten(xs) = reshape(xs, size(xs, 1), :)
|
2017-08-19 19:52:29 +00:00
|
|
|
|
2017-09-06 22:58:55 +00:00
|
|
|
unsqueeze(xs, dim) = reshape(xs, (size(xs)[1:dim-1]..., 1, size(xs)[dim:end]...))
|
2017-08-19 19:52:29 +00:00
|
|
|
|
2017-09-06 22:58:55 +00:00
|
|
|
stack(xs, dim) = cat(dim, unsqueeze.(xs, dim)...)
|
|
|
|
unstack(xs, dim) = [slicedim(xs, dim, i) for i = 1:size(xs, dim)]
|
2017-05-01 15:57:51 +00:00
|
|
|
|
|
|
|
# Other
|
2017-05-01 12:46:23 +00:00
|
|
|
|
|
|
|
function accuracy(m, data)
|
|
|
|
n = 0
|
|
|
|
correct = 0
|
|
|
|
for (x, y) in data
|
|
|
|
x, y = tobatch.((x, y))
|
|
|
|
n += size(x, 1)
|
2017-09-11 12:40:11 +00:00
|
|
|
correct += sum(argmax(m(x)) .== argmax(y))
|
2017-05-01 12:46:23 +00:00
|
|
|
end
|
|
|
|
return correct/n
|
|
|
|
end
|
2017-08-18 00:04:50 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Returns a function that when invoked, will only be triggered at most once
|
|
|
|
during `timeout` seconds. Normally, the throttled function will run
|
|
|
|
as much as it can, without ever going more than once per `wait` duration;
|
|
|
|
but if you'd like to disable the execution on the leading edge, pass
|
|
|
|
`leading=false`. To enable execution on the trailing edge, ditto.
|
|
|
|
"""
|
|
|
|
function throttle(f, timeout; leading=true, trailing=false)
|
|
|
|
cooldown = true
|
|
|
|
later = nothing
|
|
|
|
|
|
|
|
function throttled(args...; kwargs...)
|
|
|
|
yield()
|
|
|
|
|
|
|
|
if cooldown
|
|
|
|
if leading
|
|
|
|
f(args...; kwargs...)
|
|
|
|
else
|
|
|
|
later = () -> f(args...; kwargs...)
|
|
|
|
end
|
|
|
|
|
|
|
|
cooldown = false
|
|
|
|
@schedule try
|
|
|
|
while (sleep(timeout); later != nothing)
|
|
|
|
later()
|
|
|
|
later = nothing
|
|
|
|
end
|
|
|
|
finally
|
|
|
|
cooldown = true
|
|
|
|
end
|
|
|
|
elseif trailing
|
|
|
|
later = () -> f(args...; kwargs...)
|
|
|
|
end
|
|
|
|
|
|
|
|
nothing
|
|
|
|
end
|
|
|
|
end
|