Initialize the factor matrices using vanilla ALS
(self, X, W, H)
| 225 | } |
| 226 | |
| 227 | def _init_factor_matrices(self, X, W, H): |
| 228 | """Initialize the factor matrices using vanilla ALS""" |
| 229 | ALS = None |
| 230 | N, M = X.shape |
| 231 | |
| 232 | # initialize factors using ALS if not already defined |
| 233 | if W is None: |
| 234 | ALS = VanillaALS(self.K, alpha=0, max_iter=200) |
| 235 | ALS.fit(X, verbose=False) |
| 236 | W = ALS.W / np.linalg.norm(ALS.W, axis=0) |
| 237 | |
| 238 | if H is None: |
| 239 | H = np.abs(np.random.rand(self.K, M)) if ALS is None else ALS.H |
| 240 | |
| 241 | assert W.shape == (N, self.K) |
| 242 | assert H.shape == (self.K, M) |
| 243 | |
| 244 | self.H = H |
| 245 | self.W = W |
| 246 | |
| 247 | def _loss(self, X, Xhat): |
| 248 | """Return the least-squares reconstruction loss between X and Xhat""" |
no test coverage detected