From c71461e2689f06659d7e88833f5ce3c5e14e9e3c Mon Sep 17 00:00:00 2001 From: Peter Carbonetto Date: Mon, 25 Mar 2024 09:41:00 -0500 Subject: [PATCH] Added warning if stopping condition is not met. --- DESCRIPTION | 4 ++-- R/fit_poisson_nmf.R | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ddc6a06..65591a6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Encoding: UTF-8 Type: Package Package: fastTopics -Version: 0.6-169 -Date: 2024-03-18 +Version: 0.6-171 +Date: 2024-03-25 Title: Fast Algorithms for Fitting Topic Models and Non-Negative Matrix Factorizations to Count Data Authors@R: c(person("Peter","Carbonetto",role=c("aut","cre"), diff --git a/R/fit_poisson_nmf.R b/R/fit_poisson_nmf.R index fb52d24..812a36a 100644 --- a/R/fit_poisson_nmf.R +++ b/R/fit_poisson_nmf.R @@ -440,7 +440,7 @@ fit_poisson_nmf <- function (X, k, fit0, numiter = 100, cat(sprintf("Running at most %d %s updates, %s extrapolation ", numiter,method.text, ifelse(control$extrapolate,"with","without"))) - cat("(fastTopics 0.6-169).\n") + cat("(fastTopics 0.6-171).\n") } # INITIALIZE ESTIMATES @@ -495,6 +495,7 @@ fit_poisson_nmf_main_loop <- function (X, fit, numiter, update.factors, "extrapolate","beta","betamax","timing") # Iterate the updates of the factors and loadings. + converged <- FALSE for (i in 1:numiter) { fit0 <- fit t1 <- proc.time() @@ -556,20 +557,28 @@ fit_poisson_nmf_main_loop <- function (X, fit, numiter, update.factors, if (control$min.res >= 0 & res < control$min.res) { if (verbose == "progressbar") pb$terminate() - cat("Stopping condition is satisfied: ") - cat(sprintf("maximum KKT residual < %0.2e\n",control$min.res)) + cat(sprintf("Stopping condition is met at iteration %d: ",i)) + cat(sprintf("max. KKT residual < %0.2e\n",control$min.res)) + converged <- TRUE break } else if (control$min.delta.loglik >= 0 & loglik.diff < control$min.delta.loglik) { if (verbose == "progressbar") pb$terminate() - cat("Stopping condition is satisfied: ") - cat(sprintf("change in Poisson NMF loglik < %0.2e\n", + cat(sprintf("Stopping condition is met at iteration %d: ",i)) + cat(sprintf("change in NMF loglik < %0.2e\n", control$min.delta.loglik)) + converged <- TRUE break } } + # Print a warning if one or more of the stopping criteria were not + # satisfied. + if (!converged & with(control,min.res >= 0 | min.delta.loglik >= 0)) + warning(sprintf(paste("One or both stopping conditions were not met", + "within %d iterations"),numiter)) + # Output the updated "fit". progress <- progress[1:i,] fit$progress <- progress