Skip to content

Commit

Permalink
New stop condition: use component relative change
Browse files Browse the repository at this point in the history
Previously, convergence was achieved when
`|W[i, j]-preW[i, j]|<=tol for all i, j`.
Running NMF on `10*X`  (where `X` is the data matrix )
would increase the number of iterations compared to NMF on `X`.
Now, `||W[:, j]-preW[:, j]||/||W[:, j]+preW[:, j]||<=tol for all j`
is the new convergence criterion, which is relative difference
rather absolute difference and thus, it is scale invariant.
  • Loading branch information
youdongguo committed Aug 9, 2023
1 parent 07419bf commit 0a08fca
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ function nmf_skeleton!(updater::NMFUpdater{T},
update_wh!(updater, state, X, W, H)

# determine convergence
dev = max(maxad(preW, W), maxad(preH, H))
if dev < tol
converged = true
end
converged = stop_condition(W, preW, H, preH, tol)

# display info
if verbose
Expand All @@ -85,3 +82,23 @@ function nmf_skeleton!(updater::NMFUpdater{T},
end
return Result{T}(W, H, t, converged, objv)
end


function stop_condition(W::AbstractArray{T}, preW::AbstractArray, H::AbstractArray, preH::AbstractArray, eps::AbstractFloat) where T
for j in axes(W,2)
dev_w = sum_w = zero(T)
for i in axes(W,1)
dev_w += (W[i,j] - preW[i,j])^2
sum_w += (W[i,j] + preW[i,j])^2
end
dev_h = sum_h = zero(T)
for i in axes(H,2)
dev_h += (H[j,i] - preH[j,i])^2
sum_h += (H[j,i] + preH[j,i])^2
end
if sqrt(dev_w) > eps*sqrt(sum_w) || sqrt(dev_h) > eps*sqrt(sum_h)
return false
end
end
return true
end

0 comments on commit 0a08fca

Please sign in to comment.