-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: split KMM into KMM and uKMM (#37) * chore: remove @Nograd for removed function Co-authored-by: Kai Xu <[email protected]>
- Loading branch information
Showing
10 changed files
with
94 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,59 @@ | |
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
abstract type AbstractKMM <: DensityRatioEstimator end | ||
|
||
function _kmm_consts(x_nu, x_de, dre::AbstractKMM) | ||
@unpack σ, λ = dre | ||
|
||
# Gramian matrices for numerator and denominator | ||
Kdede = gaussian_gramian(x_de; σ=σ) | ||
if !iszero(λ) | ||
Kdede += safe_diagm(Kdede, λ) | ||
end | ||
Kdenu = gaussian_gramian(x_de, x_nu; σ=σ) | ||
|
||
# number of denominator and numerator samples | ||
n_de, n_nu = size(Kdenu) | ||
|
||
Kdede, typeof(σ)(n_de / n_nu) * sum(Kdenu, dims=2) | ||
end | ||
|
||
function _densratio(x_nu, x_de, dre::AbstractKMM, | ||
optlib::Type{<:OptimizationLibrary}) | ||
K, κ = _kmm_consts(x_nu, x_de, dre) | ||
_kmm_ratios(K, κ, dre, optlib) | ||
end | ||
|
||
""" | ||
uKMM(σ=1.0, B=Inf, ϵ=0.01, λ=0.001) | ||
Unconstrained Kernel Mean Matching (KMM). | ||
## Parameters | ||
* `σ` - Bandwidth of Gaussian kernel (default to `2.0`) | ||
* `λ` - Regularization parameter (default to `0.001`) | ||
## References | ||
* Huang et al. 2006. Correcting sample selection bias by | ||
unlabeled data. | ||
### Author | ||
* Júlio Hoffimann ([email protected]) | ||
* Kai Xu ([email protected]) | ||
""" | ||
@with_kw struct uKMM{T} <: AbstractKMM | ||
σ::T=2.0 | ||
λ::T=0.001 | ||
end | ||
|
||
default_optlib(dre::Type{<:uKMM}) = JuliaLib | ||
|
||
available_optlib(dre::Type{<:uKMM}) = [JuliaLib, JuMPLib] | ||
|
||
""" | ||
KMM(σ=1.0, B=Inf, ϵ=0.01, λ=0.001) | ||
|
@@ -24,7 +77,7 @@ Kernel Mean Matching (KMM). | |
* Júlio Hoffimann ([email protected]) | ||
* Kai Xu ([email protected]) | ||
""" | ||
@with_kw struct KMM{T} <: DensityRatioEstimator | ||
@with_kw struct KMM{T} <: AbstractKMM | ||
σ::T=2.0 | ||
B::T=Inf | ||
ϵ::T=0.01 | ||
|
@@ -33,26 +86,4 @@ end | |
|
||
default_optlib(dre::Type{<:KMM}) = JuMPLib | ||
|
||
available_optlib(dre::Type{<:KMM}) = [JuliaLib, JuMPLib] | ||
|
||
function _kmm_consts(x_nu, x_de, dre::KMM{T}) where {T<:AbstractFloat} | ||
@unpack σ, λ = dre | ||
|
||
# Gramian matrices for numerator and denominator | ||
Kdede = gaussian_gramian(x_de; σ=σ) | ||
if !iszero(λ) | ||
Kdede += safe_diagm(Kdede, λ) | ||
end | ||
Kdenu = gaussian_gramian(x_de, x_nu; σ=σ) | ||
|
||
# number of denominator and numerator samples | ||
n_de, n_nu = size(Kdenu) | ||
|
||
Kdede, T(n_de / n_nu) * sum(Kdenu, dims=2) | ||
end | ||
|
||
function _densratio(x_nu, x_de, dre::KMM, | ||
optlib::Type{<:OptimizationLibrary}) | ||
K, κ = _kmm_consts(x_nu, x_de, dre) | ||
_kmm_ratios(K, κ, dre, optlib) | ||
end | ||
available_optlib(dre::Type{<:KMM}) = [JuMPLib] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,4 @@ | |
|
||
import .Zygote | ||
|
||
Zygote.@nograd warn_kmm_julialib | ||
Zygote.@nograd safe_diagm |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters