Example #1
0
    def get_projected(self, data, params):

        (X1, X2) = data
        (Phi1, Phi2) = params

        # Get empirical covariance matrices
        zmuvX1 = get_zm_uv(X1)
        zmuvX2 = get_zm_uv(X2)

        # Get projected data
        XPhi1 = np.dot(zmuvX1, Phi1)
        XPhi2 = np.dot(zmuvX2, Phi2)

        # Get gram matrices
        S1 = np.dot(XPhi1.T, XPhi1)
        S2 = np.dot(XPhi2.T, XPhi2)

        # Get normalizers
        inv_srqt1 = get_svd_power(S1, -0.5)
        inv_srqt2 = get_svd_power(S2, -0.5)

        # Get normalized
        normed1 = np.dot(Phi1, inv_sqrt1)
        normed2 = np.dot(Phi2, inv_sqrt2)

        return (normed1, normed2)
Example #2
0
    def _update_Phi(self):

        pre_sqrt = np.dot(self.X_unn_Phi.T, self.X_unn_Phi)
        pre_sqrt_norm = np.thelineg.norm(pre_sqrt)
        normalizer = get_svd_power(pre_sqrt, -0.5)

        self.Phi = np.dot(self.unn_Phi, normalizer)
Example #3
0
    def __init__(self,
        dynamics, 
        W, 
        Psi, 
        mu, 
        Z, 
        lazy=True):
        
        self.dynamics = dynamics
        self.W = W
        self.Psi = Psi
        self.mu = mu
        self.Z = Z
        self.lazy = lazy

        self.d = self.W.shape[0]
        (self.k, self.num_data) = self.Z.shape
        (lam, self.Q) = np.linalg.eig(dynamics)
        self.lam = lam[:,np.newaxis]

        QW = np.dot(self.Q, self.W)

        self.mean = np.dot(QW, Z) + self.mu

        for t in range(Z.shape[0]):
            self.mean[:,t:] *= self.lam

        self.mean = np.dot(self.Q.H, self.mean)
        self.sd = get_svd_power(self.Psi, 0.5)
        self.data = None

        if not self.lazy:
            self._set_data()
Example #4
0
def get_woodbury_inversion(H, rho):

    (m, d) = H.shape
    I_m = np.identity(m)
    I_d = np.identity(d)
    to_invert = rho*I_m + np.dot(H, H.T)
    inversion = get_svd_power(to_invert, -1)
    quad = gq(H, inversion)
    unscaled = I_d - quad

    return rho**(-1) * unscaled
Example #5
0
def get_rotation(angle, P, P_inv=None):

    if P_inv is None:
        P_inv = get_svd_power(P, -1)

    A = np.eye(P.shape[0])

    A[0, 0] = np.cos(angle)
    A[1, 1] = np.cos(angle)
    A[0, 1] = -np.sin(angle)
    A[1, 0] = np.sin(angle)

    return get_multi_dot([P_inv, A, P])
Example #6
0
def get_SPC_SCCAPMLs(num_data, k, ds, rho=0.8, seed=None, lazy=True):

    loader = GL(num_data, k)
    Y = loader.get_data().T
    Psi_z = get_spc(k, rho=rho)
    sqrt_Psi_z = get_svd_power(Psi_z, 0.5)
    Z = np.dot(sqrt_Psi_z, Y)
    Psi_inits = [np.random.randn(d * 2, d) for d in ds]
    Psis = [np.dot(Pi.T, Pi) for Pi in Psi_inits]
    Ws = [np.random.randn(d, k) for d in ds]
    mus = [np.random.randn(d, 1) for d in ds]
    zipped = zip(Ws, Psis, mus)
    SCCAPML = StaticCCAProbabilisticModelLoader

    return [SCCAPML(W, Psi, mu, Z, lazy=lazy) for (W, Psi, mu) in zipped]
Example #7
0
    def _get_primal(self, dual_update):

        if self.lower is not None:
            dus = dual_update.shape

            if len(dus) == 2 and not 1 in set(dus):
                (U, s, V) = np.linalg.svd(dual_update)
                sparse_s = get_st(s, lower=self.lower)
                dual_update = get_multiplied_svd(U, s, V)
            else:
                dual_update = get_st(dual_update, lower=self.lower)

        H_inv = get_svd_power(self.H, -1)

        return np.dot(H_inv, dual_update)
Example #8
0
    def get_update(self, parameters, gradient, eta):

        self.num_rounds += 1

        if self.G is None:
            self.G = np.dot(gradient, gradient.T)
            self.d = gradient.shape[0]
        else:
            self.G += np.dot(gradient, gradient.T)

        self.S = get_svd_power(self.G, 0.5)
        self.H = self.S + np.eye(self.d) * self.delta

        return get_mu(parameters, eta, gradient, self._get_dual,
                      self._get_primal)
Example #9
0
    def __init__(self, W, Psi, mu, Z, lazy=True):

        self.W = W
        self.Psi = Psi
        self.mu = mu
        self.Z = Z
        self.lazy = lazy

        self.d = self.W.shape[0]
        (self.k, self.num_data) = self.Z.shape
        self.mean = np.dot(self.W, Z) + self.mu
        self.sd = get_svd_power(self.Psi, 0.5)
        self.data = None

        if not self.lazy:
            self._set_data()
Example #10
0
    def get_data(self):

        # Calculate current means
        scale = get_svd_power(self.rate, self.num_rounds)
        current_mean = np.dot(self.mean, scale)

        # Get batch
        batch = _get_batch(self.bs,
                           self.p,
                           self.k,
                           current_mean,
                           unit_norm=self.unit_norm)

        # Update global state variable
        self.num_rounds += 1

        return batch
    def run(self):

        theta = None

        if len(self.d) == 1 or self.d[1] == 1:
            theta = np.zeros((self.d, 1))
        else:
            theta = np.zeros(self.d)

        w_t = None

        for t in range(self.max_rounds):

            # Update parameter estimate
            w_t = self.drdw_inv(theta)

            # Sample search direction
            sphere_sample = None

            if len(self.d) == 1 or self.d[1] == 1:
                normal_sample = np.random.randn(d, 1)
                sphere_sample = normal_sample / np.linalg.norm(normal_sample)
            else:
                normal_sample = np.random.randn(*d)
                quad = np.dot(normal_sample.T, normal_sample)
                normalizer = get_svd_power(quad, -0.5)
                shere_sample = np.dot(normal_sample, normalizer)

            # Compute one-dimensional finite difference approximation
            delta_t = self.delta_scheduler.get_stepsize()
            delta_plus = self.get_objective(w_t + delta_t * sphere_sample)
            delta_minus = self.get_objective(w_t - delta_t * sphere_sample)

            # Compute gradient scale and gradient
            grad_scale = 0.5 * delta_t**(-1) * self.d * (delta_plus -
                                                         delta_minus)
            grad_approx = grad_scale * sphere_sample

            # Update theta
            theta -= self.eta * grad_approx

        self.w = w_t
Example #12
0
def get_gram_normed(unnormed, S):

    basis_quad = get_quad(unnormed, S)
    normalizer = get_svd_power(basis_quad, -0.5)

    return np.dot(unnormed, normalizer)
Example #13
0
def get_cca_vecs(X1, X2, n_components=1, num_nonzero=None):

    (n1, p1) = X1.shape
    (n2, p2) = X2.shape
    n = None

    if not n1 == n2:
        raise ValueError(
            'X1 and X2 must have same 1st dimension.')
    else:
        n = n1

    # Get means, zero-mean, normed vars
    mu1 = np.nanmean(X1, axis=0)
    mu2 = np.nanmean(X2, axis=0)
    normalizer = n**(0.5)
    zm_X1 = (X1 - mu1) / normalizer
    zm_X2 = (X2 - mu2) / normalizer

    # Get sample covariance matrices
    CX1 = np.dot(zm_X1.T, zm_X1)
    CX2 = np.dot(zm_X2.T, zm_X2)
    CX12 = np.dot(zm_X1.T, zm_X2)

    # Get inverse sqrts and normed sample cross-covariance
    CX1_inv_sqrt = get_svd_power(CX1, -0.5)
    CX2_inv_sqrt = get_svd_power(CX2, -0.5)

    Omega = get_multi_dot([
        CX1_inv_sqrt,
        CX12,
        CX2_inv_sqrt])
    (Phi1, Phi2) = [None] * 2

    if num_nonzero is None:
        (U, s, V) = np.linalg.svd(Omega)
        unnormed_Phi1 = U[:,:n_components]
        unnormed_Phi2 = V[:,:n_components]
        Phi1 = np.dot(CX1_inv_sqrt, unnormed_Phi1)
        Phi2 = np.dot(CX2_inv_sqrt, unnormed_Phi2)
    else:
        x_project = spancca.projections.setup_sparse(
            nnz=num_nonzero)
        y_project = spancca.projections.setup_sparse(
            nnz=num_nonzero)
        rank = min([
            num_nonzero * 2 + 1,
            min(Omega.shape) - 1])
        (Phi1, Phi2) = spancca.cca(
            Omega,
            rank,
            n,
            x_project,
            y_project,
            verbose=False)

    projected1 = np.dot(X1 - mu1, Phi1)
    projected2 = np.dot(X2 - mu2, Phi2)
    cc = np.sum(
        projected1 * projected2, 
        axis=1)[:,np.newaxis] / n_components

    return (Phi1, Phi2, cc)