Flux.jl/src/utils.jl

123 lines
2.6 KiB
Julia
Raw Normal View History

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
2017-10-18 16:07:58 +00:00
"""
chunk(xs, n)
Split `xs` into `n` parts.
```julia
julia> chunk(1:10, 3)
3-element Array{Array{Int64,1},1}:
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10]
```
"""
chunk(xs, n) = collect(Iterators.partition(xs, ceil(Int, length(xs)/n)))
2017-10-15 22:44:40 +00:00
batchindex(xs, i) = (reverse(Base.tail(reverse(indices(xs))))..., i)
2017-10-18 15:21:15 +00:00
"""
batch(xs)
Batch the arrays in `xs` into a single array.
```julia
julia> batch([[1,2,3],[4,5,6]])
3×2 Array{Int64,2}:
1 4
2 5
3 6
```
"""
2017-10-15 22:44:40 +00:00
function batch(xs)
2017-10-18 15:21:15 +00:00
data = first(xs) isa AbstractArray ?
similar(first(xs), size(first(xs))..., length(xs)) :
Vector{eltype(xs)}(length(xs))
2017-10-15 22:44:40 +00:00
for (i, x) in enumerate(xs)
data[batchindex(data, i)...] = x
end
return data
end
Base.rpad(v::AbstractVector, n::Integer, p) = [v; fill(p, max(n - length(v), 0))]
2017-10-18 15:21:15 +00:00
"""
batchseq(seqs, pad)
Take a list of `N` sequences, and turn them into a single sequence where each
item is a batch of `N`. Short sequences will be padded by `pad`.
```julia
julia> batchseq([[1, 2, 3], [4, 5]], 0)
3-element Array{Array{Int64,1},1}:
[1, 4]
[2, 5]
[3, 0]
```
"""
function batchseq(xs, pad = nothing, n = maximum(length(x) for x in xs))
2017-10-15 22:44:40 +00:00
xs_ = [rpad(x, n, pad) for x in xs]
[batch([xs_[j][i] for j = 1:length(xs_)]) for i = 1:n]
end
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