def test_remove_weighted_components_inplace(self): m = np.ones((500, 10), dtype=np.float32) c = np.copy(m) out = compute_principal_components(vectors=m) remove_principal_components(m, svd_res=out, weights=np.array([0.5])) assert_allclose(m, 0.75, atol=1e-5) with assert_raises(AssertionError): assert_allclose(m, c)
def _post_inference_calls(self, output:ndarray): """ Function calls to perform after training & inference """ if self.svd_res is None: raise RuntimeError("You must first train the model to obtain SVD components") elif self.components > 0: remove_principal_components(output, svd_res=self.svd_res, weights=self.svd_weights, inplace=True) else: logger.info(f"no removal of principal components")
def _post_train_calls(self): """ Function calls to perform after training, such as computing eigenvectors """ if self.components > 0: self.svd_res = compute_principal_components(self.sv.vectors, components=self.components) self.svd_weights = (self.svd_res[0] ** 2) / (self.svd_res[0] ** 2).sum().astype(REAL) remove_principal_components(self.sv.vectors, svd_res=self.svd_res, weights=self.svd_weights, inplace=True) else: self.svd_res = 0 logger.info(f"no removal of principal components")
def _post_train_calls(self): """ Function calls to perform after training, such as computing eigenvectors """ if self.components > 0: self.svd_res = compute_principal_components( self.sv.vectors, components=self.components, cache_size_gb=self.cache_size_gb, ) remove_principal_components( self.sv.vectors, svd_res=self.svd_res, inplace=True ) else: self.svd_res = 0 logger.info(f"no removal of principal components")
def test_remove_components(self): m = np.ones((500, 10), dtype=np.float32) c = np.copy(m) out = compute_principal_components(vectors=m) res = remove_principal_components(m, svd_res=out, inplace=False) assert_allclose(res, 0.0, atol=1e-5) assert_allclose(m, c)
def test_remove_weighted_components(self): m = np.ones((500,10), dtype=np.float32) out = compute_principal_components(vectors = m) remove_principal_components(m, svd_res=out, weights=np.array([0.5])) self.assertTrue(np.allclose(0.75, m))
def test_remove_components(self): m = np.ones((500,10), dtype=np.float32) out = compute_principal_components(vectors = m) res = remove_principal_components(m, svd_res=out, inplace=False) self.assertTrue(np.allclose(1., res, atol=1e-5))