Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change design for dispatch #22

Merged
merged 4 commits into from
Oct 12, 2023
Merged

Change design for dispatch #22

merged 4 commits into from
Oct 12, 2023

Conversation

dahong67
Copy link
Owner

@dahong67 dahong67 commented Oct 12, 2023

Currently we define two methods for gcp:

  1. gcp(X::Array, r, loss::AbstractLoss = LeastSquaresLoss()) = _gcp(
    X,
    r,
    (x, m) -> value(loss, x, m),
    (x, m) -> deriv(loss, x, m),
    _factor_matrix_lower_bound(loss),
    (;),
    )
  2. GCPDecompositions.gcp(X::Array, r, loss::SupportedLosses) = GCPDecompositions._gcp(
    X,
    r,
    (x, m) -> loss(m, x),
    (x, m) -> LossFunctions.deriv(loss, m, x),
    _factor_matrix_lower_bound(loss),
    (;),
    )

These call the following _gcp method:

function _gcp(X::Array{TX,N}, r, func, grad, lower, lbfgsopts) where {TX,N}

Idea: unify the two gcp methods and pass the inputs directly to _gcp so we just have gcp(X, r, loss = LeastSquaresLoss()) = _gcp(X, r, loss). Handle LossFunctions.jl losses by adding methods for value, deriv and domain.

With this design:

  • gcp provides the user-friendly interface and handles defaults
  • _gcp does not handle defaults - it only implements the algorithms

Separating concerns this way has some potential benefits:

  • reduces duplication in LossFunctionsExt - don't need to duplicate code for defaults, etc.
  • prepares for adding specialized _gcp methods, e.g., ALS for least-squares (ALS doesn't apply to other loss functions), etc.

Reduces duplication and is probably a more natural way to implement this
package extension.
Provide richer info to _gcp so we can dispatch at that level. Preparing
the way for having various algorithms.
@dahong67 dahong67 changed the title Change main _gcp signature Change design for dispatch Oct 12, 2023
@codecov
Copy link

codecov bot commented Oct 12, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (6c3c18d) 100.00% compared to head (4b5431a) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #22   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          107       106    -1     
=========================================
- Hits           107       106    -1     
Files Coverage Δ
ext/LossFunctionsExt.jl 100.00% <100.00%> (ø)
src/gcp-opt.jl 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Implement fall-back using `UserDefinedLoss`.
@dahong67
Copy link
Owner Author

dahong67 commented Oct 12, 2023

One more TODO remaining:

# TODO: remove this `func, grad, lower` signature
# will require reworking how we do testing
_gcp(X::Array{TX,N}, r, func, grad, lower, lbfgsopts) where {TX,N} = _gcp(
X,
r,
UserDefinedLoss(func; deriv = grad, domain = Interval(lower, +Inf)),
lbfgsopts,
)

Will require reworking how we do the testing since we currently use that signature. For example:

Mr = GCPDecompositions._gcp(
X,
r,
(x, m) -> m - x * log(m + 1e-10),
(x, m) -> 1 - x / (m + 1e-10),
0.0,
(;),
)

Since this would affect ongoing work in #20, can address this in a separate PR after #20 is merged. Should probably rethink how we do this testing anyway.

[UPDATE 10/12/2023: now tracking this TODO in #23]

@dahong67 dahong67 merged commit 34db0c6 into master Oct 12, 2023
12 checks passed
@dahong67 dahong67 deleted the gcp-dispatch branch October 12, 2023 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant