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

Rename AvgMode to AggMode #104

Merged
merged 1 commit into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/LossFunctions.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module LossFunctions

using RecipesBase

import Base.*
using Base.Cartesian
using Markdown, SparseArrays, InteractiveUtils

using RecipesBase
using LearnBase
import LearnBase: value, value!, deriv, deriv2, deriv!, scaled, value_deriv,
import LearnBase:
value, value!,
deriv, deriv2, deriv!,
value_deriv,
scaled,
isminimizable,
isdifferentiable,
istwicedifferentiable,
Expand All @@ -25,6 +28,7 @@ import LearnBase: value, value!, deriv, deriv2, deriv!, scaled, value_deriv,
isclasscalibrated,
isdistancebased,
issymmetric

# Reexport LearnBase
eval(Expr(:toplevel, Expr(:export, setdiff(names(LearnBase), [:LearnBase])...)))

Expand Down Expand Up @@ -70,10 +74,10 @@ export

weightedloss,

AvgMode
AggMode

include("common.jl")
include("averagemode.jl")
include("aggregatemode.jl")

include("supervised/supervised.jl")
include("supervised/sparse.jl")
Expand All @@ -85,6 +89,8 @@ include("supervised/other.jl")
include("supervised/ordinal.jl")
include("supervised/io.jl")

include("deprecated.jl")

# allow using some special losses as function
(loss::ScaledSupervisedLoss)(args...) = value(loss, args...)
(loss::WeightedBinaryLoss)(args...) = value(loss, args...)
Expand All @@ -99,13 +105,4 @@ for T in union(subtypes(DistanceLoss), subtypes(MarginLoss))
@eval (loss::$T)(args...) = value(loss, args...)
end

# deprecations
@deprecate ScaledMarginLoss(loss, ::Type{Val{K}}) where {K} ScaledMarginLoss(loss, Val(K))
@deprecate ScaledDistanceLoss(loss, ::Type{Val{K}}) where {K} ScaledDistanceLoss(loss, Val(K))
@deprecate ScaledSupervisedLoss(loss, ::Type{Val{K}}) where {K} ScaledSupervisedLoss(loss, Val(K))
@deprecate ((*)(::Type{Val{K}}, loss::Loss) where {K}) (*)(Val(K), loss)
@deprecate scaled(loss, ::Type{Val{K}}) where {K} scaled(loss, Val(K))
@deprecate weightedloss(loss::Loss, ::Type{Val{W}}) where {W} weightedloss(loss, Val(W))
@deprecate WeightedBinaryLoss(loss, ::Type{Val{W}}) where {W} WeightedBinaryLoss(loss, Val(W))

end # module
41 changes: 17 additions & 24 deletions src/averagemode.jl → src/aggregatemode.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
abstract type AverageMode end
abstract type AggregateMode end

module AvgMode
module AggMode

using StatsBase
import ..LossFunctions.AverageMode
import ..LossFunctions.AggregateMode

export
None,
Sum,
Mean,
Macro,
Micro,
WeightedMean,
WeightedSum

"""
AvgMode.None()
AggMode.None()

Opt-out of aggregation. This is usually the default value.
Using `None` will cause the element-wise results to be returned.
"""
struct None <: AverageMode end
struct None <: AggregateMode end

"""
AvgMode.Sum()
AggMode.Sum()

Causes the method to return the unweighted sum of the
elements instead of the individual elements. Can be used in
combination with `ObsDim`, in which case a vector will be
returned containing the sum for each observation (useful
mainly for multivariable regression).
"""
struct Sum <: AverageMode end
struct Sum <: AggregateMode end

"""
AvgMode.Mean()
AggMode.Mean()

Causes the method to return the unweighted mean of the
elements instead of the individual elements. Can be used in
combination with `ObsDim`, in which case a vector will be
returned containing the mean for each observation (useful
mainly for multivariable regression).
"""
struct Mean <: AverageMode end

struct Macro <: AverageMode end
const Micro = Mean
struct Mean <: AggregateMode end

"""
AvgMode.WeightedSum(weights; [normalize = false])
AggMode.WeightedSum(weights; [normalize = false])

Causes the method to return the weighted sum of all
observations. The variable `weights` has to be a vector of
Expand All @@ -62,7 +57,6 @@ module AvgMode
can be used to give certain observations a stronger
influence on the sum.


- `normalize::Bool`: Boolean that specifies if the weight
vector should be transformed in such a way that it sums to
one (i.e. normalized). This will not mutate the weight
Expand All @@ -76,20 +70,20 @@ module AvgMode
# Examples

```julia-repl
julia> AvgMode.WeightedSum([1,1,2]); # 3 observations
julia> AggMode.WeightedSum([1,1,2]); # 3 observations

julia> AvgMode.WeightedSum([1,1,2], normalize = true);
julia> AggMode.WeightedSum([1,1,2], normalize = true);
```
"""
struct WeightedSum{T<:AbstractWeights} <: AverageMode
struct WeightedSum{T<:AbstractWeights} <: AggregateMode
weights::T
normalize::Bool
end
WeightedSum(A::AbstractVector, normalize::Bool) = WeightedSum(weights(A), normalize)
WeightedSum(A::AbstractVector; normalize::Bool = false) = WeightedSum(weights(A), normalize)

"""
AvgMode.WeightedMean(weights; [normalize = true])
AggMode.WeightedMean(weights; [normalize = true])

Causes the method to return the weighted mean of all
observations. The variable `weights` has to be a vector of
Expand All @@ -103,7 +97,6 @@ module AvgMode
be used to give certain observations a stronger influence
on the mean.


- `normalize::Bool`: Boolean that specifies if the weight
vector should be transformed in such a way that it sums to
one (i.e. normalized). This will not mutate the weight
Expand All @@ -117,12 +110,12 @@ module AvgMode
# Examples

```julia-repl
julia> AvgMode.WeightedMean([1,1,2]); # 3 observations
julia> AggMode.WeightedMean([1,1,2]); # 3 observations

julia> AvgMode.WeightedMean([1,1,2], normalize = false);
julia> AggMode.WeightedMean([1,1,2], normalize = false);
```
"""
struct WeightedMean{T<:AbstractWeights} <: AverageMode
struct WeightedMean{T<:AbstractWeights} <: AggregateMode
weights::T
normalize::Bool
end
Expand Down
19 changes: 19 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@deprecate ScaledMarginLoss(loss, ::Type{Val{K}}) where {K} ScaledMarginLoss(loss, Val(K))
@deprecate ScaledDistanceLoss(loss, ::Type{Val{K}}) where {K} ScaledDistanceLoss(loss, Val(K))
@deprecate ScaledSupervisedLoss(loss, ::Type{Val{K}}) where {K} ScaledSupervisedLoss(loss, Val(K))
@deprecate ((*)(::Type{Val{K}}, loss::Loss) where {K}) (*)(Val(K), loss)
@deprecate scaled(loss, ::Type{Val{K}}) where {K} scaled(loss, Val(K))
@deprecate weightedloss(loss::Loss, ::Type{Val{W}}) where {W} weightedloss(loss, Val(W))
@deprecate WeightedBinaryLoss(loss, ::Type{Val{W}}) where {W} WeightedBinaryLoss(loss, Val(W))

Base.@deprecate_binding AverageMode AggregateMode
module AvgMode
import ..LossFunctions.AggMode
Base.@deprecate_binding None AggMode.None
Base.@deprecate_binding Sum AggMode.Sum
Base.@deprecate_binding Mean AggMode.Mean
Base.@deprecate_binding Macro AggMode.Mean
Base.@deprecate_binding Micro AggMode.Mean
Base.@deprecate_binding WeightedSum AggMode.WeightedSum
Base.@deprecate_binding WeightedMean AggMode.WeightedMean
end