Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions news/add-convergence-flag.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**Added:**

* SNMFOptimizer.converged_ attribute to indicate whether the optimization
successfully reached the convergence tolerance (True) or stopped because the
maximum number of iterations was reached (False).

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
16 changes: 10 additions & 6 deletions src/diffpy/stretched_nmf/snmf_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def fit(self, rho=0, eta=0, reset=True):
the output of the previous
fit() as their input.
"""
self.converged_ = False

if reset:
self.components_ = self.init_components.copy()
Expand Down Expand Up @@ -251,11 +252,12 @@ def fit(self, rho=0, eta=0, reset=True):
sparsity_term = self.eta * np.sum(
np.sqrt(self.components_)
) # Square root penalty
obj_diff = (
self.objective_function - regularization_term - sparsity_term
)
print(
f"Start, Objective function: {self.objective_function:.5e}"
f", Obj - reg/sparse: {self.objective_function
- regularization_term
- sparsity_term:.5e}"
f", Obj - reg/sparse: {obj_diff:.5e}"
)

# Main optimization loop
Expand All @@ -274,11 +276,12 @@ def fit(self, rho=0, eta=0, reset=True):
sparsity_term = self.eta * np.sum(
np.sqrt(self.components_)
) # Square root penalty
obj_diff = (
self.objective_function - regularization_term - sparsity_term
)
print(
f"Obj fun: {self.objective_function:.5e}, "
f"Obj - reg/sparse: {self.objective_function
- regularization_term
- sparsity_term:.5e}, "
f", Obj - reg/sparse: {obj_diff:.5e}"
f"Iter: {self.outiter}"
)

Expand All @@ -294,6 +297,7 @@ def fit(self, rho=0, eta=0, reset=True):
self.objective_difference < self.objective_function * self.tol
and outiter >= self.min_iter
):
self.converged_ = True
break

self.normalize_results()
Expand Down
Loading