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

Propose accuracy functions #2181

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/metrics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using OneHotArrays: OneHotMatrix, onecold, onehotbatch
using Statistics: mean

"""
multiclass_accuracy(model, test_x, test_y::Vector{T})

Used to calculate accuracy of multi-class problem.
Test set consists of `test_x` and `test_y`. `size(test_x)[end]` and `size(test_y)[end]`
equal to the number of samples. `test_x` contains batch of samples, and `test_y` is a Vector,
which contains the position of the largest element of the output (`findmax(output)`)
obtained by feeding `test_x` into the ideal model.

e.g.

```jldoctest
julia> typeof(test_x)
Matrix{Float32} (alias for Array{Float32, 2})

julia> typeof(test_y)
Vector{Int64} (alias for Array{Int64, 1})

julia> multiclass_accuracy(model, test_x, test_y);
```
"""
multiclass_accuracy(model, test_x, test_y::Vector{T}) where {T} = mean(onecold(model(test_x)) .== test_y)

Check warning on line 25 in src/metrics.jl

View check run for this annotation

Codecov / codecov/patch

src/metrics.jl#L25

Added line #L25 was not covered by tests

"""
multiclass_accuracy(model, test_x, test_y::Union{Matrix{T}, OneHotMatrix})

Used to calculate accuracy of multi-class problem.
Test set consists of `test_x` and `test_y`. `size(test_x)[end]` and `size(test_y)[end]`
equal to the number of samples. `test_x` contains batch of samples, and `test_y` is a Matrix
or OneHotMatrix, which contains the direct output obtained by feeding `test_x` into
the ideal model. Length of `test_y`'s elements equals to the number of classes.

e.g.

```jldoctest
julia> typeof(test_x)
Matrix{N0f8} (alias for Array{FixedPointNumbers.Normed{UInt8, 8}, 2})

julia> typeof(test_y)
OneHotMatrix{UInt32, Vector{UInt32}} (alias for OneHotArray{UInt32, 1, 2, Array{UInt32, 1}})

julia> multiclass_accuracy(model(test_x), test_y);
```
"""
multiclass_accuracy(model, test_x, test_y::Union{Matrix{T}, OneHotMatrix}) where {T} = mean(onecold(model(test_x)) .== onecold(test_y))

Check warning on line 48 in src/metrics.jl

View check run for this annotation

Codecov / codecov/patch

src/metrics.jl#L48

Added line #L48 was not covered by tests

"""
multilabel_accuracy(model, test_x, test_y; threshold=0.5)

Used to calculated accuracy of multi-label problem.
Test set consists of `test_x` and `test_y`. `size(test_x)[end]` and `size(test_y)[end]`
equal to the number of samples. `threshold` can be set to change the classes identified,
0.5 by default.
"""
multilabel_accuracy(model, test_x, test_y; threshold=0.5) = mean((model(test_x) .> threshold) .== test_y)

Check warning on line 58 in src/metrics.jl

View check run for this annotation

Codecov / codecov/patch

src/metrics.jl#L58

Added line #L58 was not covered by tests