Skip to content

Commit

Permalink
Clean up repo to run on 0.5 (#14)
Browse files Browse the repository at this point in the history
* Clean up repo to run on 0.5

* Forgot some Compat

* Forgot to modify the runtests path

* Fixed reshaping issue

* Introduce dependency on StatsBase for maxad

* Okay, not just for maxad

* 0.4 doesn't like array dims as tuples I guess

* Add status badges to the readme
  • Loading branch information
ararslan committed Aug 4, 2016
1 parent 9662afa commit bd01b7f
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 183 deletions.
30 changes: 15 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
language: cpp
compiler:
- clang
# Documentation: http://docs.travis-ci.com/user/languages/julia/
language: julia
os:
- linux
julia:
- 0.3
- 0.4
- 0.5
- nightly
notifications:
email: false
env:
matrix:
- JULIAVERSION="juliareleases"
- JULIAVERSION="julianightlies"
before_install:
- sudo add-apt-repository ppa:staticfloat/julia-deps -y
- sudo add-apt-repository ppa:staticfloat/${JULIAVERSION} -y
- sudo apt-get update -qq -y
- sudo apt-get install libpcre3-dev julia -y
script:
- julia -e 'Pkg.init(); run(`ln -s $(pwd()) $(Pkg.dir("NMF"))`); Pkg.pin("NMF"); Pkg.resolve()'
- julia ./runtests.jl
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia -e 'Pkg.clone(pwd()); Pkg.build("NMF"); Pkg.test("NMF"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("NMF")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())';
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
A Julia package for non-negative matrix factorization (NMF).

[![Build Status](https://travis-ci.org/JuliaStats/NMF.jl.svg?branch=master)](https://travis-ci.org/JuliaStats/NMF.jl)
[![NMF](http://pkg.julialang.org/badges/NMF_0.3.svg)](http://pkg.julialang.org/?pkg=NMF&ver=0.3)
[![NMF](http://pkg.julialang.org/badges/NMF_0.4.svg)](http://pkg.julialang.org/?pkg=NMF&ver=0.4)
[![Coverage Status](https://coveralls.io/repos/github/JuliaStats/NMF.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaStats/NMF.jl?branch=master)
[![NMF](http://pkg.julialang.org/badges/NMF_0.3.svg)](http://pkg.julialang.org/?pkg=NMF)
[![NMF](http://pkg.julialang.org/badges/NMF_0.4.svg)](http://pkg.julialang.org/?pkg=NMF)
[![NMF](http://pkg.julialang.org/badges/NMF_0.5.svg)](http://pkg.julialang.org/?pkg=NMF)

---------------------------

Expand Down
6 changes: 3 additions & 3 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.3-
MLBase 0.3-
ArrayViews 0.4.8-
julia 0.3
Compat 0.8.0
StatsBase
11 changes: 0 additions & 11 deletions runtests.jl

This file was deleted.

9 changes: 5 additions & 4 deletions src/NMF.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module NMF
using Compat
using StatsBase

using ArrayViews
using MLBase
import Compat: view
import Base: sum!

export nnmf

include("common.jl")
include("utils.jl")

include("initialization.jl")
include("multupd.jl")
include("projals.jl")
include("alspgrad.jl")

include("interf.jl")

end # module
78 changes: 39 additions & 39 deletions src/alspgrad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

function projgradnorm(g, x)
T = eltype(g)
v = convert(T, 0.0)
v = zero(T)
@inbounds for i = 1:length(g)
gi = g[i]
if gi < zero(T) || x[i] > zero(T)
Expand All @@ -22,18 +22,18 @@ end
# all elements of A becoming zero
# The new value of A would be A = A - α*G
function maxstep(G, A)
T = typeof(one(eltype(A))/one(eltype(G)))
T = typeof(one(eltype(A)) / one(eltype(G)))
αmax = zero(T)
for i = 1:length(G)
g = G[i]
if g >= 0
αmax = max(αmax, A[i]/g)
αmax = max(αmax, A[i] / g)
else
αmax = convert(T, Inf)
break
end
end
αmax
return αmax
end

## sub-routines for updating H
Expand All @@ -49,13 +49,13 @@ immutable ALSGradUpdH_State{T}

function ALSGradUpdH_State(X, W, H)
k, n = size(H)
new(Array(T, k, n),
Array(T, k, n),
Array(T, k, n),
Array(T, k, n),
Array(T, k, k),
Array(T, k, n),
Array(T, k, n))
@compat new(Array{T,2}(k, n),
Array{T,2}(k, n),
Array{T,2}(k, n),
Array{T,2}(k, n),
Array{T,2}(k, k),
Array{T,2}(k, n),
Array{T,2}(k, n))
end
end
ALSGradUpdH_State{T}(X, W::VecOrMat{T}, H::VecOrMat{T}) = ALSGradUpdH_State{T}(X, W, H)
Expand All @@ -77,8 +77,8 @@ function alspgrad_updateh!{T}(X,

s = ALSGradUpdH_State(X, W, H)
set_w!(s, X, W)
_alspgrad_updateh!(X, W, H, s,
maxiter, traceiter, tolg,
_alspgrad_updateh!(X, W, H, s,
maxiter, traceiter, tolg,
beta, sigma, verbose)
end

Expand All @@ -103,8 +103,8 @@ function _alspgrad_updateh!(X, # size (p, n)
T = eltype(H)

# banner
if verbose
@printf("%5s %12s %12s %12s %8s %12s\n",
if verbose
@printf("%5s %12s %12s %12s %8s %12s\n",
"Iter", "objv", "objv.change", "1st-ord", "alpha", "back-tracks")
WH = W * H
objv = sqL2dist(X, WH)
Expand All @@ -115,7 +115,7 @@ function _alspgrad_updateh!(X, # size (p, n)
t = 0
converged = false
to_decr = true
α = one(T)/one(eltype(G))
α = one(T) / one(eltype(G))
while !converged && t < maxiter
t += 1

Expand Down Expand Up @@ -152,7 +152,7 @@ function _alspgrad_updateh!(X, # size (p, n)
dv1 = BLAS.dot(G, D) # <G, D>
A_mul_B!(WtWD, WtW, D)
dv2 = BLAS.dot(WtWD, D) # <D, WtW * D>

# back-track
suff_decr = ((1 - σ) * dv1 + convert(T, 0.5) * dv2) < 0

Expand Down Expand Up @@ -189,7 +189,7 @@ function _alspgrad_updateh!(X, # size (p, n)
A_mul_B!(WH, W, H)
preobjv = objv
objv = sqL2dist(X, WH)
@printf("%5d %12.5e %12.5e %12.5e %8.4f %12d\n",
@printf("%5d %12.5e %12.5e %12.5e %8.4f %12d\n",
t, objv, objv - preobjv, pgnrm, α, it)
end
end
Expand All @@ -210,13 +210,13 @@ immutable ALSGradUpdW_State{T}

function ALSGradUpdW_State(X, W, H)
p, k = size(W)
new(Array(T, p, k),
Array(T, p, k),
Array(T, p, k),
Array(T, p, k),
Array(T, k, k),
Array(T, p, k),
Array(T, p, k))
@compat new(Array{T,2}(p, k),
Array{T,2}(p, k),
Array{T,2}(p, k),
Array{T,2}(p, k),
Array{T,2}(k, k),
Array{T,2}(p, k),
Array{T,2}(p, k))
end
end
ALSGradUpdW_State{T}(X, W::VecOrMat{T}, H::VecOrMat{T}) = ALSGradUpdW_State{T}(X, W, H)
Expand All @@ -239,8 +239,8 @@ function alspgrad_updatew!{T}(X,

s = ALSGradUpdW_State(X, W, H)
set_h!(s, X, H)
_alspgrad_updatew!(X, W, H, s,
maxiter, traceiter, tolg,
_alspgrad_updatew!(X, W, H, s,
maxiter, traceiter, tolg,
beta, sigma, verbose)
end

Expand All @@ -265,8 +265,8 @@ function _alspgrad_updatew!(X, # size (p, n)
T = eltype(W)

# banner
if verbose
@printf("%5s %12s %12s %12s %8s %12s\n",
if verbose
@printf("%5s %12s %12s %12s %8s %12s\n",
"Iter", "objv", "objv.change", "1st-ord", "alpha", "back-tracks")
WH = W * H
objv = sqL2dist(X, WH)
Expand All @@ -277,7 +277,7 @@ function _alspgrad_updatew!(X, # size (p, n)
t = 0
converged = false
to_decr = true
α = one(T)/one(eltype(G))
α = one(T) / one(eltype(G))
while !converged && t < maxiter
t += 1

Expand Down Expand Up @@ -314,7 +314,7 @@ function _alspgrad_updatew!(X, # size (p, n)
dv1 = BLAS.dot(G, D) # <G, D>
A_mul_B!(DHHt, D, HHt)
dv2 = BLAS.dot(DHHt, D) # <D * HHt, D>

# back-track
suff_decr = ((1 - σ) * dv1 + convert(T, 0.5) * dv2) < 0

Expand Down Expand Up @@ -351,7 +351,7 @@ function _alspgrad_updatew!(X, # size (p, n)
A_mul_B!(WH, W, H)
preobjv = objv
objv = sqL2dist(X, WH)
@printf("%5d %12.5e %12.5e %12.5e %8.4f %12d\n",
@printf("%5d %12.5e %12.5e %12.5e %8.4f %12d\n",
t, objv, objv - preobjv, pgnrm, α, it)
end
end
Expand All @@ -368,7 +368,7 @@ type ALSPGrad{T}
tolg::T # tolerance of grad norm in sub-routine
verbose::Bool # whether to show procedural information (main)

function ALSPGrad(;maxiter::Integer=100,
function ALSPGrad(;maxiter::Integer=100,
maxsubiter::Integer=200,
tol::Real=cbrt(eps(T)),
tolg::Real=eps(T)^(1/4),
Expand All @@ -387,7 +387,7 @@ immutable ALSPGradUpd{T} <: NMFUpdater{T}
end

solve!(alg::ALSPGrad, X, W, H) =
nmf_skeleton!(ALSPGradUpd(alg.maxsubiter, alg.tolg),
nmf_skeleton!(ALSPGradUpd(alg.maxsubiter, alg.tolg),
X, W, H, alg.maxiter, alg.verbose, alg.tol)


Expand All @@ -397,8 +397,8 @@ immutable ALSPGradUpd_State{T}
uwstate::ALSGradUpdW_State

ALSPGradUpd_State(X, W, H) =
new(W * H,
ALSGradUpdH_State(X, W, H),
new(W * H,
ALSGradUpdH_State(X, W, H),
ALSGradUpdW_State(X, W, H))
end

Expand All @@ -410,15 +410,15 @@ function update_wh!(upd::ALSPGradUpd, s::ALSPGradUpd_State, X, W, H)

# update H
set_w!(s.uhstate, X, W)
_alspgrad_updateh!(X, W, H, s.uhstate,
_alspgrad_updateh!(X, W, H, s.uhstate,
upd.maxsubiter, 20, upd.tolg, convert(T, 0.2), convert(T, 0.01), false)

# update W
set_h!(s.uwstate, X, H)
_alspgrad_updatew!(X, W, H, s.uwstate,
_alspgrad_updatew!(X, W, H, s.uwstate,
upd.maxsubiter, 20, upd.tolg, convert(T, 0.2), convert(T, 0.01), false)

# update WH
A_mul_B!(s.WH, W, H)
A_mul_B!(s.WH, W, H)
end

20 changes: 8 additions & 12 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

# tools to check size

function nmf_checksize(X,
W::AbstractMatrix,
H::AbstractMatrix)
function nmf_checksize(X, W::AbstractMatrix, H::AbstractMatrix)

p = size(X, 1)
n = size(X, 2)
Expand All @@ -13,6 +11,7 @@ function nmf_checksize(X,
if !(size(W,1) == p && size(H) == (k, n))
throw(DimensionMismatch("Dimensions of X, W, and H are inconsistent."))
end

return (p, n, k)
end

Expand All @@ -26,11 +25,10 @@ immutable Result{T}
converged::Bool
objvalue::T

function Result(W::Matrix{T}, H::Matrix{T},
niters::Int, converged::Bool, objv)

size(W, 2) == size(H, 1) ||
function Result(W::Matrix{T}, H::Matrix{T}, niters::Int, converged::Bool, objv)
if size(W, 2) != size(H, 1)
throw(DimensionMismatch("Inner dimensions of W and H mismatch."))
end
new(W, H, niters, converged, objv)
end
end
Expand All @@ -46,8 +44,8 @@ function nmf_skeleton!{T}(updater::NMFUpdater{T},

# init
state = prepare_state(updater, X, W, H)
preW = Array(T, size(W))
preH = Array(T, size(H))
preW = @compat Array{T,2}(size(W)...)
preH = @compat Array{T,2}(size(H)...)
if verbose
objv = evaluate_objv(updater, state, X, W, H)
@printf("%-5s %-13s %-13s %-13s\n", "Iter", "objv", "objv.change", "(W & H).change")
Expand Down Expand Up @@ -75,7 +73,7 @@ function nmf_skeleton!{T}(updater::NMFUpdater{T},
if verbose
preobjv = objv
objv = evaluate_objv(updater, state, X, W, H)
@printf("%5d %13.6e %13.6e %13.6e\n",
@printf("%5d %13.6e %13.6e %13.6e\n",
t, objv, objv - preobjv, dev)
end
end
Expand All @@ -85,5 +83,3 @@ function nmf_skeleton!{T}(updater::NMFUpdater{T},
end
return Result{T}(W, H, t, converged, objv)
end


0 comments on commit bd01b7f

Please sign in to comment.