Exemplo n.º 1
0
def myTest_model():
    N = 4
    Dx = 3
    Dy = 4
    tau = 0.01
    X = common.randn((N, Dx))
    Y = common.randn((N, Dy))
    #X = np.eye(Dx)
    #Y = diag(np.ones(Dx), Dx, Dy)
    model = learn(X, Y, tau)
    # it should be the case that Ux*Sxx*Ux' = I and by symmetry for y too.
    (Ux, Uy) = (model.Ux, model.Uy)
    Z = np.c_[X, Y]  # stack X Y by rows
    C = np.cov(Z.T)
    Sxx = np.mat((1-tau)*C[:Dx, :Dx] + tau*np.eye(Dx))
    Syy = np.mat((1-tau)*C[Dx:, Dx:] + tau*np.eye(Dy))
    Sxy = np.mat(C[:Dx, Dx:])

    # validate that Ux is Sxx-orthonormal and Uy is Syy-orthonormal
    Jx = Ux.T * Sxx * Ux
    Jy = Uy.T * Syy * Uy
    Q = Uy.T * Sxy.T * Ux
    Np = len(model.p)
    bx = np.allclose(Jx, np.eye(Np))
    by = np.allclose(Jy, np.eye(Np))
    bp = np.allclose(Q, model.P.T)
    print 'pcca model test passed?', bx, by, bp

    Z = project(model, X, Y)
    print Z
Exemplo n.º 2
0
def test2():
    N = 3000
    D = 50
    X = common.randn((N, D))
    Y = common.randn((N, D))
    U = common.dist(X, Y)

    import cProfile
    #cProfile.runctx('(cost, pi, edge_cost) = approxMatch(U)', globals(), locals())
    cProfile.runctx('(cy_cost0, cy_pi0, cy_edge_cost0) = cy_ApproxMatch(U)', globals(), locals())
    print cy_cost0
Exemplo n.º 3
0
def test1():
    # test
    C = np.matrix('1 -1 1 1; 1 1 1 0; 0 1 1 1 ;1 -3 -2 1')
    (cost, pi, edge_cost) = approxMatch(C)
    (cy_cost, cy_pi, cy_edge_cost) = fast_approxMatch(C)
    print C
    print "cost:", cost, cy_cost-cost
    print pi, pi-cy_pi
    print edge_cost, cy_edge_cost - edge_cost

    D = 2000
    np.random.seed(1)
    C = common.randn((D, D))
    import cProfile
    cProfile.runctx('(cost, pi, edge_cost) = approxMatch(C)', globals(), locals())
    cProfile.runctx('(cy_cost0, cy_pi0, cy_edge_cost0) = cy_ApproxMatch(C)', globals(), locals())
    (cost, pi, edge_cost) = approxMatch(C)
    (cy_cost0, cy_pi0, cy_edge_cost0) = cy_ApproxMatch(C)

    assert np.linalg.norm(cy_cost0-cost) == 0
    assert np.linalg.norm(pi-cy_pi0) == 0
    assert np.linalg.norm(cy_edge_cost0 - edge_cost) == 0

    X = np.array([[ 0.17034787, -1.11852005,  2.3723583 ],
       [ 0.40587496, -0.71610151,  0.24853477],
       [ 0.28721089, -1.62157422,  0.33806607],
       [ 0.88027416,  0.30368357, -1.15908568],
       [-0.50893908, -0.61650787, -1.10849268]])

    Y = np.array([[ 0.49316125, -1.6459511 ,  0.03514289],
       [ 0.16211477,  0.41796482, -0.19066103],
       [-0.68095143,  0.48752118,  1.08161025],
       [-0.44147856,  0.43943179,  1.05625251]])

    GX = np.array([[0, 1, 1, 0, 1],
       [0, 1, 1, 1, 0],
       [0, 1, 0, 1, 1],
       [0, 1, 1, 0, 0],
       [1, 0, 0, 0, 1]])

    GY = np.array([[0, 1, 1, 0],
       [0, 1, 0, 0],
       [1, 0, 1, 1],
       [0, 0, 0, 1]])

    options = common.Struct()
    options.weight_type = 'dist'
    options.K = 1  # K is not really 1 here, but a non-zero value is required.
    options.alpha = 0.3
    options.normalize_projections = 1
    W, U, Z = makeWeights(options, X, Y, GX, GY)
    print 'W shape: ', W.shape
    print W
Exemplo n.º 4
0
        print 'r0', np.linalg.norm(r0-t0), r0
        print 'r1', np.linalg.norm(r1-t1), r1
        print 'r2', np.linalg.norm(r2-t2), r2
        print 'r', np.linalg.norm(r-t), r
        print 'eta', eta

        R = np.mat(model.R)
        model.RT = R.T
        # both should be roughly the same (maybe even exactly the same for r0-R0)
        assert common.norm(R.T.dot(R)-K) < 1e-10
        assert common.norm(model.RT[0, :] - r0) < 1e-20
        # 7/7/2013 TL: implementation seems to be working correctly.
    elif nargs == 2:
        N = 1000
        D = 1000
        X = common.randn((N, D))
        U = X.dot(X.T)
        import cProfile
        cProfile.runctx('model = ICD.fast_ichol(U, 0.00001)', globals(), locals())

    elif nargs == 3:
        filename1 = sys.argv[1]
        filename2 = sys.argv[2]
        W1 = IO.readPickledWords(filename1)
        W1.setupFeatures()
        eta = 0.001
        model1 = W1.ICD_representation(0, eta, 0)

        W2 = IO.readPickledWords(filename2)
        W2.setupFeatures()
        eta = 0.001
Exemplo n.º 5
0
 def addReprNoise(self, noise):
     for k in self.repr.keys():
         d = self.repr[k]
         for kk in d.keys():
             d[kk] = d[kk] + noise*common.randn((1, 1))
Exemplo n.º 6
0
    return G, idx


def toSymmetricStochastic(G, sym=True, stochastic=True, norm='l1'):
    if sym:
        print >> sys.stderr, 'symmetrizing'
        G = (G + G.T) / 2
    if stochastic:
        print >> sys.stderr, 'normalizing'
        G = normalize(G, norm, axis=1)  # make stochastic matrix.
    return G


# def permute(G, pi):
#     (N, _) = G.shape
#     M = len(pi)
#     pi = np.append(pi, np.arange(M, N))
#     G = G[:, pi]
#     G = G[pi, :]
#     return G

if __name__ == '__main__':
    A = np.mat(common.randn((4, 5)))
    K = A * A.T
    D = np.mat(common.dist(A))
    k = 2
    (G, I) = knn_graph(A, k)
    (KG, KI) = kernel_knn_graph(K, k)
    print "G-KG", np.linalg.norm((G-KG).todense())
    print 'G:\n', G.todense()
    print 'D:\n', D