def _solve(self, HH, HY): """Compute output weights from HH and HY using Dask functionality. """ # make HH/HY divisible by chunk size n_features, _ = HH.shape padding = 0 if n_features > self.bsize_ and n_features % self.bsize_ > 0: print("Adjusting batch size {} to n_features {}".format( self.bsize_, n_features)) padding = self.bsize_ - (n_features % self.bsize_) P01 = da.zeros((n_features, padding)) P10 = da.zeros((padding, n_features)) P11 = da.zeros((padding, padding)) HH = da.block([[HH, P01], [P10, P11]]) P1 = da.zeros((padding, HY.shape[1])) HY = da.block([[HY], [P1]]) # rechunk, add bias, and solve HH = HH.rechunk( self.bsize_) + self.alpha * da.eye(HH.shape[1], chunks=self.bsize_) HY = HY.rechunk(self.bsize_) B = da.linalg.solve(HH, HY, sym_pos=True) if padding > 0: B = B[:n_features] return B
def mvn_random_DASK(mean, cov, N, dim): da.random.seed(10) epsilon = 0.0001 A = da.linalg.cholesky(cov + epsilon * da.eye(dim), lower=True) z = da.random.standard_normal(size=(N, dim)) x = da.outer(da.ones((N, )), mean).transpose() + da.dot(A, z.transpose()) return x
def eig_dask(A, nofIt=None): """ A dask eigenvalue solver: assumes A is symmetric and used the QR method to find eigenvalues and eigenvectors. nofIt: number of iterations (default is the size of A) """ A_new = A if nofIt is None: nofIt = A.shape[0] V = da.eye(A.shape[0], 100) for i in range(nofIt): Q, R = da.linalg.qr(A_new) A_new = da.dot(R, Q) V = da.dot(V, Q) return (da.diag(A_new), V)
def nearestPD(A, threads=1): """ Find the nearest positive-definite matrix to input A Python/Numpy port of John D'Errico's `nearestSPD` MATLAB code [1], which credits [2] from Ahmed Fasih [1] https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd [2] N.J. Higham, "Computing a nearest symmetric positive semidefinite matrix" (1988): https://doi.org/10.1016/0024-3795(88)90223-6 """ isPD = lambda x: da.all(np.linalg.eigvals(x) > 0).compute() B = (A + A.T) / 2 _, s, V = da.linalg.svd(B) H = da.dot(V.T, da.dot(da.diag(s), V)) A2 = (B + H) / 2 A3 = (A2 + A2.T) / 2 if isPD(A3): return A3 spacing = da.spacing(da.linalg.norm(A)) # The above is different from [1]. It appears that MATLAB's `chol` Cholesky # decomposition will accept matrixes with exactly 0-eigenvalue, whereas # Numpy's will not. So where [1] uses `eps(mineig)` (where `eps` is Matlab # for `np.spacing`), we use the above definition. CAVEAT: our `spacing` # will be much larger than [1]'s `eps(mineig)`, since `mineig` is usually on # the order of 1e-16, and `eps(1e-16)` is on the order of 1e-34, whereas # `spacing` will, for Gaussian random matrixes of small dimension, be on # othe order of 1e-16. In practice, both ways converge, as the unit test # below suggests. eye_chunk = estimate_chunks((A.shape[0], A.shape[0]), threads=threads)[0] I = da.eye(A.shape[0], chunks=eye_chunk) k = 1 while not isPD(A3): mineig = da.min(da.real(np.linalg.eigvals(A3))) A3 += I * (-mineig * k**2 + spacing) k += 1 return A3
def test_subspace_to_SVD_case2(): """ A = USV U = [e1 e2, ..., ek] \ [0, 0, ..., 0] | N by K [1, 1, ..., 1] / S = np.range(N, 0, -1) V = I A = np.diag(np.range(N, 0, -1)) subspace_to_SVD should recover I and S from sections of A """ for N in range(2, 10): for K in range(2, N + 1): U = np.zeros((N, K)) U[N - 1, :] = np.ones(K) U[:K, :K] = np.eye(K) V = da.eye(K) U = da.array(U) U_q, _ = da.linalg.qr(U) S = da.arange(K, 0, -1) A = U.dot(da.diag(S)) for j in range(K, N + 1): subspace = A[:, 0:j] U_s, S_s, V_s = subspace_to_SVD(subspace, A, full_v=True) np.testing.assert_almost_equal(subspace_dist(V_s, V, S), 0, decimal=decimals) _, l, _ = da.linalg.svd(U_q.dot(U_s.T)) np.testing.assert_almost_equal(l[:K].compute(), np.ones(K)) np.testing.assert_almost_equal(l[K:].compute(), np.zeros(N - K))
def test_eye(): assert_eq(da.eye(9, chunks=3), np.eye(9)) assert_eq(da.eye(9), np.eye(9)) assert_eq(da.eye(10, chunks=3), np.eye(10)) assert_eq(da.eye(9, chunks=3, M=11), np.eye(9, M=11)) assert_eq(da.eye(11, chunks=3, M=9), np.eye(11, M=9)) assert_eq(da.eye(7, chunks=3, M=11), np.eye(7, M=11)) assert_eq(da.eye(11, chunks=3, M=7), np.eye(11, M=7)) assert_eq(da.eye(9, chunks=3, k=2), np.eye(9, k=2)) assert_eq(da.eye(9, chunks=3, k=-2), np.eye(9, k=-2)) assert_eq(da.eye(7, chunks=3, M=11, k=5), np.eye(7, M=11, k=5)) assert_eq(da.eye(11, chunks=3, M=7, k=-6), np.eye(11, M=7, k=-6)) assert_eq(da.eye(6, chunks=3, M=9, k=7), np.eye(6, M=9, k=7)) assert_eq(da.eye(12, chunks=3, M=6, k=-3), np.eye(12, M=6, k=-3)) assert_eq(da.eye(9, chunks=3, dtype=int), np.eye(9, dtype=int)) assert_eq(da.eye(10, chunks=3, dtype=int), np.eye(10, dtype=int)) with dask.config.set({"array.chunk-size": "50 MiB"}): x = da.eye(10000, "auto") assert 4 < x.npartitions < 32
def test_map_blocks(): x = da.eye(10, chunks=5) y = x.map_blocks(sparse.COO.from_numpy, meta=sparse.COO.from_numpy(np.eye(1))) assert isinstance(y._meta, sparse.COO) assert_eq(y, y)
def test_eye(): assert_eq(da.eye(9, chunks=3), np.eye(9)) assert_eq(da.eye(10, chunks=3), np.eye(10)) assert_eq(da.eye(9, chunks=3, M=11), np.eye(9, M=11)) assert_eq(da.eye(11, chunks=3, M=9), np.eye(11, M=9)) assert_eq(da.eye(7, chunks=3, M=11), np.eye(7, M=11)) assert_eq(da.eye(11, chunks=3, M=7), np.eye(11, M=7)) assert_eq(da.eye(9, chunks=3, k=2), np.eye(9, k=2)) assert_eq(da.eye(9, chunks=3, k=-2), np.eye(9, k=-2)) assert_eq(da.eye(7, chunks=3, M=11, k=5), np.eye(7, M=11, k=5)) assert_eq(da.eye(11, chunks=3, M=7, k=-6), np.eye(11, M=7, k=-6)) assert_eq(da.eye(6, chunks=3, M=9, k=7), np.eye(6, M=9, k=7)) assert_eq(da.eye(12, chunks=3, M=6, k=-3), np.eye(12, M=6, k=-3)) assert_eq(da.eye(9, chunks=3, dtype=int), np.eye(9, dtype=int)) assert_eq(da.eye(10, chunks=3, dtype=int), np.eye(10, dtype=int))
def _setup_cluster_matrix(n_clusters, cluster_idx): """ Set cluster occupation matrix """ return da.eye(n_clusters, dtype=np.bool, chunks=n_clusters)[cluster_idx]