Skip to content

Commit

Permalink
add first sketch of the factorization module
Browse files Browse the repository at this point in the history
  • Loading branch information
ddbourgin committed May 9, 2020
1 parent 5ffd87b commit 28fef84
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ that in mind, don't just read the docs -- read the source!

numpy_ml.nonparametric

numpy_ml.factorization

numpy_ml.trees

numpy_ml.neural_nets
Expand Down
11 changes: 11 additions & 0 deletions docs/numpy_ml.factorization.factors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
``VanillaALS``
--------------
.. autoclass:: numpy_ml.factorization.VanillaALS
:members:
:undoc-members:

``NMF``
--------
.. autoclass:: numpy_ml.factorization.NMF
:members:
:undoc-members:
7 changes: 7 additions & 0 deletions docs/numpy_ml.factorization.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Matrix factorization
####################

.. toctree::
:maxdepth: 3

numpy_ml.factorization.factors
4 changes: 4 additions & 0 deletions numpy_ml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# noqa
"""Common ML and ML-adjacent algorithms implemented in NumPy"""

from . import utils
from . import preprocessing

Expand All @@ -11,3 +14,4 @@
from . import rl_models
from . import trees
from . import bandits
from . import factorization
23 changes: 12 additions & 11 deletions numpy_ml/factorization/factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, K, alpha=1, max_iter=200, tol=1e-4):
where :math:`||\cdot||` denotes the Frobenius norm, **X** is the
:math:`N \times M` data matrix, :math:`\mathbf{W}` and
:math:`\mathbf{H}` are learned factor matrices with dimensions :math:`N
\times K` and :math:`K \times M`, respectively. :math:`\alpha` is a
\times K` and :math:`K \times M`, respectively, and :math:`\alpha` is a
user-defined regularization weight.
ALS proceeds by alternating between fixing **W** and optimizing for
Expand All @@ -36,8 +36,8 @@ def __init__(self, K, alpha=1, max_iter=200, tol=1e-4):
References
----------
.. [1] Gillis, N. (2014). The why and how of nonnegative matrix
factorization. *Regularization, optimization, kernels, and support
vector machines*, 12(257), 257-291.
factorization. *Regularization, optimization, kernels, and support
vector machines, 12(257)*, 257-291.
Parameters
----------
Expand Down Expand Up @@ -294,14 +294,15 @@ def fit(self, X, W=None, H=None, n_initializations=10, verbose=False):
\mathbf{X}^{(j)} :=
\mathbf{X} - \mathbf{WH}^\top + \mathbf{w}_j \mathbf{h}_j^\top
where :math:`\mathbf{X}^{(j)}` is the `j`th residue, **X** is the input
data matrix, and :math:`\mathbf{w}_j` and :math:`\mathbf{h}_j` are the
`j`th columns of the current factor matrices **W** and **H**. HALS
proceeds by minimizing the cost for each residue, first with respect to
:math:`\mathbf{w}_j` holding :math:`\mathbf{h}_j` fixed, and then with
respect to :math:`\mathbf{h}_j`, holding the newly updated
:math:`\mathbf{w}_j` fixed. The residue cost :math:`\mathcal{L}^{(j)}`
for :math:`\mathbf{X}^{j}` is simply:
where :math:`\mathbf{X}^{(j)}` is the :math:`j^{th}` residue, **X** is
the input data matrix, and :math:`\mathbf{w}_j` and
:math:`\mathbf{h}_j` are the :math:`j^{th}` columns of the current
factor matrices **W** and **H**. HALS proceeds by minimizing the cost
for each residue, first with respect to :math:`\mathbf{w}_j` holding
:math:`\mathbf{h}_j` fixed, and then with respect to
:math:`\mathbf{h}_j`, holding the newly updated :math:`\mathbf{w}_j`
fixed. The residue cost :math:`\mathcal{L}^{(j)}` for
:math:`\mathbf{X}^{j}` is simply:
.. math::
Expand Down

0 comments on commit 28fef84

Please sign in to comment.