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

Log cosh #421

Merged
merged 12 commits into from
Sep 3, 2020
2 changes: 1 addition & 1 deletion src/MLJBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export orientation, reports_each_observation,
spports_weights, prediction_type

# measures/continuous.jl:
export mav, mae, mape, rms, rmsl, rmslp1, rmsp, l1, l2
export mav, mae, mape, rms, rmsl, rmslp1, rmsp, l1, l2, log_cosh

# measures/confusion_matrix.jl:
export confusion_matrix, confmat
Expand Down
30 changes: 30 additions & 0 deletions src/measures/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,33 @@ function (m::MAPE)(ŷ::Vec{<:Real}, y::Vec{<:Real})
end
return ret / count
end

struct LogCosh <: Measure end

"""
log_cosh(ŷ, y)

Log-Cosh per-observation loss:

``\\text{Log-Cosh Loss} = log(cosh(ŷᵢ-yᵢ))``

where `yᵢ` is the target and `ŷᵢ` is the output.

For more information, run `info(log_cosh)`.
"""
const log_cosh = LogCosh()

metadata_measure(LogCosh;
name = "log_cosh",
target_scitype = Union{Vec{Continuous},Vec{Count}},
prediction_type = :deterministic,
orientation = :loss,
reports_each_observation = true,
is_feature_dependent = false,
ablaom marked this conversation as resolved.
Show resolved Hide resolved
supports_weights = false,
docstring = "log cosh loss; aliases: `log_cosh`.")

function (log_cosh::LogCosh)(ŷ::Vec{<:Real}, y::Vec{<:Real})
check_dimensions(ŷ, y)
return log.(cosh.(ŷ-y))
end
Copy link
Member

@ablaom ablaom Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (see C below): return a vector of per-observation losses: return log.(cosh.(ŷ-y))) / length(y)

oops I mean't to drop the /length(y) as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sum is missing in this definition

1 change: 1 addition & 0 deletions test/measures/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rng = StableRNG(666899)
@test isapprox(mean(l1(yhat, y, w)), mae(yhat, y, w))
@test isapprox(mean(l2(yhat, y)), 5)
@test isapprox(mean(l2(yhat, y, w)), rms(yhat, y, w)^2)
@test isapprox(mean(log_cosh(yhat, y)), 1.3715546675)

ablaom marked this conversation as resolved.
Show resolved Hide resolved
yhat = y .+ 1
@test isapprox(rmsl(yhat, y),
Expand Down