fix argmax and add test

This commit is contained in:
boathit 2018-08-21 11:29:57 +08:00
parent 930776eb1a
commit 616ed194df
3 changed files with 20 additions and 4 deletions

View File

@ -54,11 +54,13 @@ end
onehotbatch(ls, labels, unk...) =
OneHotMatrix(length(labels), [onehot(l, labels, unk...) for l in ls])
argmax(y::AbstractVector, labels = 1:length(y)) =
labels[findfirst(y, maximum(y))]
import Base:argmax
argmax(y::AbstractMatrix, l...) =
squeeze(mapslices(y -> argmax(y, l...), y, 1), 1)
argmax(y::AbstractVector, labels) =
labels[something(findfirst(isequal(maximum(y)), y), 0)]
argmax(y::AbstractMatrix, labels) =
dropdims(mapslices(y -> argmax(y, labels), y, dims=1), dims=1)
# Ambiguity hack

13
test/onehot.jl Normal file
View File

@ -0,0 +1,13 @@
using Flux:argmax
using Test
@testset "argmax" begin
a = [1, 2, 5, 3.]
A = [1 20 5; 2 7 6; 3 9 10; 2 1 14]
labels = ['A', 'B', 'C', 'D']
@test argmax(a) == 3
@test argmax(A) == CartesianIndex(1, 2)
@test argmax(a, labels) == 'C'
@test argmax(A, labels) == ['C', 'A', 'D']
end

View File

@ -24,6 +24,7 @@ insert!(LOAD_PATH, 2, "@v#.#")
@testset "Flux" begin
include("utils.jl")
include("onehot.jl")
include("tracker.jl")
include("layers/normalisation.jl")
include("layers/stateless.jl")