-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the coordinate descent method from scikit-learn, supporting spa…
…rsity (#23) * Added coordiante descent translation from scikit-learn, still tests to write * Completed the implementation, gives same results as the Python version (however with different convergence criterion) * Fixed a small typing bug, added a working test * Added reference * Changed default alpha * Fixed a small mistake in test * Fixed a typing mistake in test * Updated coorddesc.jl header * Updates for Julia 1.1 * Removed Manifest, uses lazy transpose, removed unused lines * Fixes to Project.toml * Fixed interface for coordinate descent. Added testing of interf.jl
- Loading branch information
Showing
9 changed files
with
191 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Manifest.toml |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name = "NMF" | ||
uuid = "6ef6ca0d-6ad7-5ff6-b225-e928bfa0a386" | ||
license = "MIT" | ||
|
||
[deps] | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" | ||
|
||
[extras] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test"] |
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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# Coordinate descent method, translated from the Python/Cython implementation | ||
# in scikit-learn and modified to comply with the interfaces of the NMF package | ||
|
||
# Original files | ||
# https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/decomposition/nmf.py | ||
# https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/decomposition/cdnmf_fast.pyx | ||
|
||
# Original implementation authors: | ||
# Vlad Niculae | ||
# Lars Buitinck | ||
# Mathieu Blondel <mathieu@mblondel.org> | ||
# Tom Dupre la Tour | ||
|
||
# Original license: BSD 3 clause | ||
|
||
# Julia translation: Vilim Štih | ||
|
||
# Reference: Cichocki, Andrzej, and P. H. A. N. Anh-Huy. "Fast local algorithms for | ||
# large scale nonnegative matrix and tensor factorizations." | ||
# IEICE transactions on fundamentals of electronics, communications and | ||
# computer sciences 92.3: 708-721, 2009. | ||
|
||
|
||
mutable struct CoordinateDescent{T} | ||
maxiter::Int | ||
verbose::Bool | ||
tol::T | ||
α::T | ||
l₁ratio::T | ||
regularization::Symbol | ||
shuffle::Bool | ||
|
||
function CoordinateDescent{T}(;maxiter::Integer=100, | ||
verbose::Bool=false, | ||
tol::Real=cbrt(eps(T)), | ||
α::Real=T(0.0), | ||
regularization=:both, | ||
l₁ratio::Real=zero(T), | ||
shuffle::Bool=false) where T | ||
new{T}(maxiter, verbose, tol, α, l₁ratio, regularization, shuffle) | ||
end | ||
end | ||
|
||
|
||
solve!(alg::CoordinateDescent{T}, X, W, H) where {T} = | ||
nmf_skeleton!(CoordinateDescentUpd{T}(alg.α, alg.l₁ratio, alg.regularization, alg.shuffle), | ||
X, W, H, alg.maxiter, alg.verbose, alg.tol) | ||
|
||
|
||
struct CoordinateDescentUpd{T} <: NMFUpdater{T} | ||
l₁W::T | ||
l₂W::T | ||
l₁H::T | ||
l₂H::T | ||
shuffle::Bool | ||
function CoordinateDescentUpd{T}(α::T, l₁ratio::T, regularization::Symbol, shuffle::Bool) where {T} | ||
αW = zero(T) | ||
αH = zero(T) | ||
|
||
if (regularization == :both) || (regularization == :components) | ||
αH = α | ||
end | ||
|
||
if (regularization == :both) || (regularization == :transformation) | ||
αW = α | ||
end | ||
|
||
new{T}(αW*l₁ratio, | ||
αW*(1-l₁ratio), | ||
αH*l₁ratio, | ||
αH*(1-l₁ratio), | ||
shuffle) | ||
end | ||
end | ||
|
||
mutable struct CoordinateDescentState{T} | ||
violation::T | ||
violation_init::Union{Nothing, T} | ||
end | ||
|
||
prepare_state(::CoordinateDescentUpd{T}, X, W, H) where {T} = | ||
CoordinateDescentState(zero(T), nothing) | ||
evaluate_objv(::CoordinateDescentUpd{T}, s::CoordinateDescentState, X, W, H) where {T} = | ||
s.violation / (s.violation_init === nothing ? oneunit(T) : s.violation_init) | ||
|
||
"Updates W only" | ||
function _update_coord_descent!(X, W, H, l1_reg, l2_reg, shuffle) | ||
HHt = H * H' | ||
XHt = X * H' | ||
|
||
n_components = size(H, 1) | ||
n_samples = size(W, 1) | ||
|
||
if l2_reg > 0. | ||
HHt[diagind(HHt)] .+= l2_reg | ||
end | ||
if l1_reg > 0. | ||
XHt .-= l1_reg | ||
end | ||
if shuffle | ||
permutation = randperm(n_components) | ||
else | ||
permutation = 1:n_components | ||
end | ||
|
||
violation = zero(eltype(X)) | ||
|
||
for t in permutation | ||
for i in 1:n_samples | ||
# gradient = GW[t, i] where GW = np.dot(W, HHt) - XHt | ||
grad = -XHt[i, t] | ||
|
||
for r in 1:n_components | ||
grad += HHt[t, r] * W[i, r] | ||
end | ||
|
||
# projected gradient | ||
pg = W[i, t] == 0 ? min(zero(grad), grad) : grad | ||
violation += abs(pg) | ||
|
||
# Hessian | ||
hess = HHt[t, t] | ||
if hess != 0 | ||
W[i, t] = max(W[i, t] - grad / hess, zero(grad)) | ||
end | ||
end | ||
end | ||
return violation | ||
end | ||
|
||
|
||
function update_wh!(upd::CoordinateDescentUpd{T}, s::CoordinateDescentState{T}, | ||
X::AbstractArray{T}, W::AbstractArray{T}, H::AbstractArray{T}) where T | ||
Ht = transpose(H) | ||
|
||
violation = zero(T) | ||
violation += _update_coord_descent!(X, W, H, upd.l₁W, upd.l₂W, upd.shuffle) | ||
Wt = transpose(W) | ||
violation += _update_coord_descent!(PermutedDimsArray(X, (2,1)), Ht, Wt, | ||
upd.l₁H, upd.l₂H, upd.shuffle) | ||
|
||
s.violation = violation | ||
if s.violation_init !== nothing | ||
s.violation_init = violation | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using NMF | ||
using Test | ||
|
||
p = 5 | ||
n = 8 | ||
k = 3 | ||
|
||
for T in (Float64, Float32) | ||
Wg = max.(rand(T, p, k) .- T(0.5), zero(T)) | ||
Hg = max.(rand(T, k, n) .- T(0.5), zero(T)) | ||
X = Wg * Hg | ||
W = Wg .+ rand(T, p, k)*T(0.1) | ||
|
||
NMF.solve!(NMF.CoordinateDescent{T}(α=0.0, maxiter=1000, tol=1e-9), X, W, Hg) | ||
|
||
@test X ≈ W * Hg atol=1e-4 | ||
end |
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