Exemple #1
0
def test_lbp():
    exms = 30
    train = 10
    x, y, z = get_1d_toy_data(exms, plot=False)
    x -= np.mean(x, axis=0)
    x = np.hstack([x, np.ones((exms, 1))])
    print x.shape
    # ...and corresponding transition matrix
    A = np.zeros((exms, exms), dtype=np.int32)
    for j in range(1, 2):
        for i in range(j, exms):
            A[i - j, i] = 1
            A[i, i - j] = 1
    A = co.sparse(co.matrix(A, tc='i'))
    inds = np.random.permutation(exms)
    uinds = inds[train:]
    linds = inds[:train]
    cluster = [np.arange(exms)]

    qp = TCRFR_QP(x.T.copy(),
                  y[linds].copy(),
                  linds,
                  states=2,
                  A=A,
                  reg_gamma=10.,
                  reg_theta=0.9,
                  trans_sym=[1])
    lbpa = TCRFR_lbpa(x.T.copy(),
                      y[linds].copy(),
                      linds,
                      states=2,
                      A=A,
                      reg_gamma=10.,
                      reg_theta=0.9,
                      trans_sym=[1])
    lbpa_iset = TCRFR_lbpa_iset(cluster,
                                x.T.copy(),
                                y[linds].copy(),
                                linds,
                                states=2,
                                A=A,
                                reg_gamma=10.,
                                reg_theta=0.9,
                                trans_sym=[1])
    qp.fit()
    lbpa.fit()
    lbpa_iset.fit()

    # lbpa_iset.map_inference(lbpa.u, lbpa.unpack_v(lbpa.v))
    lid = lbpa.map_indep(qp.u, lbpa.unpack_v(qp.v))

    print 'STATES ------------------------'
    foo = np.zeros(lbpa.samples, dtype=np.int8)
    foo[lbpa.label_inds] = 1
    print 'Train  = ', foo
    print 'True   = ', z
    print 'QP     = ', qp.latent
    print 'LBPA   = ', lbpa.latent
    print 'LBPAc  = ', lbpa_iset.latent
    print 'Indep  = ', lid
Exemple #2
0
def method_tcrfr_qp(vecX, vecy, train, test, states=2, params=[0.9, 0.00001, 0.5, 10], true_latent=None, plot=False):
    A = co.spmatrix(0, [], [], (vecX.shape[0], vecX.shape[0]), tc='d')
    for k in range(1, params[3]):
        for i in range(vecX.shape[0]-k):
            A[i, i+k] = 1
            A[i+k, i] = 1

    tcrfr = TCRFR_QP(data=vecX.T, labels=vecy[train], label_inds=train, states=states, A=A,
                  reg_theta=params[0], reg_lambda=params[1], reg_gamma=params[2]*float(len(train)+len(test)),
                  trans_regs=[[1., 1.]], trans_sym=[1])

    tcrfr.fit(max_iter=20, use_grads=False, auto_adjust=False)
    y_preds, lats = tcrfr.predict()
    print lats
    if plot:
        plt.figure(1)
        plt.subplot(1, 2, 1)
        plt.plot(vecX[:, 0], vecy, '.g', alpha=0.1, markersize=10.0)
        plt.plot(vecX[test, 0], vecy[test], 'or', alpha=0.6, markersize=10.0)
        plt.plot(vecX[test, 0], y_preds[test], 'oc', alpha=0.6, markersize=6.0)
        plt.plot(vecX[test, 0], lats[test], 'ob', alpha=0.6, markersize=6.0)

        plt.subplot(1, 2, 2)
        plt.plot(vecX[train, 0], vecy[train], 'or', alpha=0.6, markersize=10.0)
        plt.plot(vecX[train, 0], lats[train], 'ob', alpha=0.6, markersize=6.0)
        plt.plot(vecX[train, 0], y_preds[train], 'xg', alpha=0.8, markersize=10.0)

        print('Test performance: ')
        print evaluate(vecy[test], y_preds[test], true_latent[test], lats[test])
        print('Training performance: ')
        print evaluate(vecy[train], y_preds[train], true_latent[train], lats[train])

        plt.show()

    return 'TCRFR-QP', y_preds[test], lats[test]
Exemple #3
0
def method_tcrfr_qp(vecX,
                    vecy,
                    train,
                    test,
                    states=2,
                    params=[0.9, 0.00001, 0.5, 10],
                    true_latent=None,
                    plot=False):
    A = co.spmatrix(0, [], [], (vecX.shape[0], vecX.shape[0]), tc='d')
    for k in range(1, params[3]):
        for i in range(vecX.shape[0] - k):
            A[i, i + k] = 1
            A[i + k, i] = 1

    tcrfr = TCRFR_QP(data=vecX.T,
                     labels=vecy[train],
                     label_inds=train,
                     states=states,
                     A=A,
                     reg_theta=params[0],
                     reg_lambda=params[1],
                     reg_gamma=params[2] * float(len(train) + len(test)),
                     trans_regs=[[1., 1.]],
                     trans_sym=[1])

    tcrfr.fit(max_iter=20, use_grads=False, auto_adjust=False)
    y_preds, lats = tcrfr.predict()
    print lats
    if plot:
        plt.figure(1)
        plt.subplot(1, 2, 1)
        plt.plot(vecX[:, 0], vecy, '.g', alpha=0.1, markersize=10.0)
        plt.plot(vecX[test, 0], vecy[test], 'or', alpha=0.6, markersize=10.0)
        plt.plot(vecX[test, 0], y_preds[test], 'oc', alpha=0.6, markersize=6.0)
        plt.plot(vecX[test, 0], lats[test], 'ob', alpha=0.6, markersize=6.0)

        plt.subplot(1, 2, 2)
        plt.plot(vecX[train, 0], vecy[train], 'or', alpha=0.6, markersize=10.0)
        plt.plot(vecX[train, 0], lats[train], 'ob', alpha=0.6, markersize=6.0)
        plt.plot(vecX[train, 0],
                 y_preds[train],
                 'xg',
                 alpha=0.8,
                 markersize=10.0)

        print('Test performance: ')
        print evaluate(vecy[test], y_preds[test], true_latent[test],
                       lats[test])
        print('Training performance: ')
        print evaluate(vecy[train], y_preds[train], true_latent[train],
                       lats[train])

        plt.show()

    return 'TCRFR-QP', y_preds[test], lats[test]
Exemple #4
0
def test_cluster_fixed_latent_states():
    print "Test Cluster setting."
    # setup cluster 0 = labeled, 1 = unlabeled (2 latent states)
    cluster = [np.arange(0, 32)]
    map_types = [TCRFR_lbpa_iset.MAP_ISET_FULL]

    labeled_inds = np.random.permutation(32)
    labeled_inds = labeled_inds[:8]

    qp = TCRFR_QP(x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.8, reg_gamma=1., trans_sym=[1])
    qp.fit(use_grads=False)

    lbpa_iset = TCRFR_lbpa_iset(cluster, map_types, x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.8, reg_gamma=1., trans_sym=[1], verbosity_level=3)
    fixed = -np.ones(y.size, dtype=np.int)
    fixed[4:10] = z[4:10]
    lbpa_iset.set_fixed_latent_states(fixed)
    lbpa_iset.fit(use_grads=False)

    print '------------'
    inds = np.zeros(y.size, dtype=np.int8)
    inds[labeled_inds] = 1
    print inds
    print '------------'
    print fixed
    print '------------'
    print 'Truth: ', z
    print 'Lbpa : ', lbpa_iset.latent
    print 'Qp   : ', qp.latent
    print '------------'
    print lbpa_iset.log_partition(qp.unpack_v(qp.v))
    print qp.log_partition_pl(qp.unpack_v(qp.v))
    print '------------'
Exemple #5
0
def test_cluster_setting():
    print "Test Cluster setting."
    # setup cluster 0 = labeled, 1 = unlabeled (2 latent states)
    cluster = [np.arange(0, 10), np.arange(10, 16)]
    A[9, 10] = 0
    A[10, 9] = 0
    print z
    assert any(z[:6] == 0) and any(z[6:16] == 1)
    labeled_inds = np.arange(0, 10)

    qp = TCRFR_QP(x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.6, reg_gamma=1., trans_sym=[1])
    qp.fit(use_grads=False)

    map_types = [TCRFR_lbpa_iset.MAP_ISET_FULL, TCRFR_lbpa_iset.MAP_ISET_FULL]
    lbpa_iset = TCRFR_lbpa_iset(cluster, map_types, x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.6, reg_gamma=1., trans_sym=[1])
    lbpa_iset.fit(use_grads=False)

    print '------------'
    inds = np.zeros(y.size, dtype=np.int8)
    inds[labeled_inds] = 1
    print inds
    print '------------'
    print lbpa_iset.latent
    print qp.latent
    print z
    print '------------'
    print lbpa_iset.log_partition_pl(qp.unpack_v(qp.v))
    print qp.log_partition_pl(qp.unpack_v(qp.v))
    print '------------'
Exemple #6
0
def test_cluster_large_setting():
    print "Test Cluster setting."
    # setup cluster 0 = labeled, 1 = unlabeled (2 latent states)
    cluster = [np.arange(0, 2501), np.arange(2501, 5000)]
    A[2500, 2501] = 0
    A[2501, 2500] = 0
    labeled_inds = np.random.permutation(y.size)
    labeled_inds = labeled_inds[:1500]

    qp = TCRFR_QP(x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.9, reg_gamma=1., trans_sym=[1])
    #qp.fit(use_grads=False)

    map_types = [TCRFR_lbpa_iset.MAP_ISET_FULL, TCRFR_lbpa_iset.MAP_ISET_FULL]
    lbpa_iset = TCRFR_lbpa_iset(cluster, map_types, x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.9, reg_gamma=1., trans_sym=[1])
    lbpa_iset.fit(use_grads=False)

    print '------------'
    print np.sum(np.abs(lbpa_iset.latent - z))
    print np.sum(np.abs((1 - lbpa_iset.latent) - z))
    print '------------'
    print np.sum(np.abs(qp.latent - z))
    print np.sum(np.abs((1 - qp.latent) - z))
    print '------------'
def test_cluster_fixed_latent_states():
    print "Test Cluster setting."
    # setup cluster 0 = labeled, 1 = unlabeled (2 latent states)
    cluster = [np.arange(0, 32)]
    map_types = [TCRFR_lbpa_iset.MAP_ISET_FULL]

    labeled_inds = np.random.permutation(32)
    labeled_inds = labeled_inds[:8]

    qp = TCRFR_QP(x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.8, reg_gamma=1., trans_sym=[1])
    qp.fit(use_grads=False)

    lbpa_iset = TCRFR_lbpa_iset(cluster, map_types, x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.8, reg_gamma=1., trans_sym=[1], verbosity_level=3)
    fixed = -np.ones(y.size, dtype=np.int)
    fixed[4:10] = z[4:10]
    lbpa_iset.set_fixed_latent_states(fixed)
    lbpa_iset.fit(use_grads=False)

    print '------------'
    inds = np.zeros(y.size, dtype=np.int8)
    inds[labeled_inds] = 1
    print inds
    print '------------'
    print fixed
    print '------------'
    print 'Truth: ', z
    print 'Lbpa : ', lbpa_iset.latent
    print 'Qp   : ', qp.latent
    print '------------'
    print lbpa_iset.log_partition(qp.unpack_v(qp.v))
    print qp.log_partition_pl(qp.unpack_v(qp.v))
    print '------------'
def test_cluster_setting():
    print "Test Cluster setting."
    # setup cluster 0 = labeled, 1 = unlabeled (2 latent states)
    cluster = [np.arange(0, 10), np.arange(10, 16)]
    A[9, 10] = 0
    A[10, 9] = 0
    print z
    assert any(z[:6]==0) and any(z[6:16]==1)
    labeled_inds = np.arange(0, 10)

    qp = TCRFR_QP(x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.6, reg_gamma=1., trans_sym=[1])
    qp.fit(use_grads=False)

    map_types = [TCRFR_lbpa_iset.MAP_ISET_FULL, TCRFR_lbpa_iset.MAP_ISET_FULL]
    lbpa_iset = TCRFR_lbpa_iset(cluster, map_types, x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.6, reg_gamma=1., trans_sym=[1])
    lbpa_iset.fit(use_grads=False)

    print '------------'
    inds = np.zeros(y.size, dtype=np.int8)
    inds[labeled_inds] = 1
    print inds
    print '------------'
    print lbpa_iset.latent
    print qp.latent
    print z
    print '------------'
    print lbpa_iset.log_partition_pl(qp.unpack_v(qp.v))
    print qp.log_partition_pl(qp.unpack_v(qp.v))
    print '------------'
Exemple #9
0
def test_transition_conversion():
    print "Test transition conversion."
    num_exms = 100
    num_edges = 200

    # create a (valid) random transition matrix
    edges = np.zeros((num_edges, 3), dtype=np.int64)
    neighbors = np.zeros(num_edges, dtype=np.int32)
    A = co.spmatrix(0, [], [], (num_exms, num_exms), tc='d')
    cnt_edges = 0
    while cnt_edges < num_edges:
        e1 = np.random.randint(0, num_exms)
        e2 = np.random.randint(0, num_exms)
        if e1 != e2 and A[e1, e2] == 0:
            val = np.random.randint(1, 3)
            A[e1, e2] = val
            A[e2, e1] = val
            edges[cnt_edges, :] = (e1, e2, val)
            neighbors[e1] += 1
            neighbors[e2] += 1
            cnt_edges += 1

    qp = TCRFR_QP(x.T, y[labeled_inds], labeled_inds, states=2, A=A, \
                           reg_theta=0.9, reg_gamma=1., trans_sym=[1])

    assert qp.N.shape[1] == np.max(neighbors)
    assert qp.E.shape[0] == num_edges  # correct number of edges detected?

    # check if the correct edges are in the edge-array
    for (e1, e2, t) in edges:
        cnt_1 = 0
        cnt_2 = 0
        for e in range(qp.E.shape[0]):
            # check edges in both direction (only one should be in there)
            if qp.E[e, 0] == e1 and qp.E[e, 1] == e2 and qp.E[e, 2] == t:
                cnt_1 += 1
            if qp.E[e, 0] == e2 and qp.E[e, 1] == e1 and qp.E[e, 2] == t:
                cnt_2 += 1
        assert cnt_1 == 1 or cnt_2 == 1
        assert cnt_1 + cnt_2 == 1

    for i in range(num_exms):
        assert neighbors[i] == np.sum(qp.N_weights[i, :])
        for (e1, e2, t) in edges:
            if e1 == i or e2 == i:
                if e2 == i:
                    e2 = e1
                    e1 = i
                assert e2 in qp.N[i, :neighbors[i]]
                ind = np.where(qp.N[i, :neighbors[i]] == e2)[0][0]
                # print qp.N_inv[i, ind]
                assert qp.N[e2, qp.N_inv[i, ind]] == i
Exemple #10
0
def test_lbp():
    exms = 30
    train = 10
    x, y, z = get_1d_toy_data(exms, plot=False)
    x -= np.mean(x, axis=0)
    x = np.hstack([x, np.ones((exms, 1))])
    print x.shape
    # ...and corresponding transition matrix
    A = np.zeros((exms, exms), dtype=np.int32)
    for j in range(1, 2):
        for i in range(j, exms):
            A[i-j, i] = 1
            A[i, i-j] = 1
    A = co.sparse(co.matrix(A, tc='i'))
    inds = np.random.permutation(exms)
    uinds = inds[train:]
    linds = inds[:train]
    cluster = [np.arange(exms)]

    qp = TCRFR_QP(x.T.copy(), y[linds].copy(), linds, states=2, A=A, reg_gamma=10., reg_theta=0.9, trans_sym=[1])
    lbpa = TCRFR_lbpa(x.T.copy(), y[linds].copy(), linds, states=2, A=A, reg_gamma=10., reg_theta=0.9, trans_sym=[1])
    lbpa_iset = TCRFR_lbpa_iset(cluster, x.T.copy(), y[linds].copy(), linds, states=2, A=A, reg_gamma=10., reg_theta=0.9, trans_sym=[1])
    qp.fit()
    lbpa.fit()
    lbpa_iset.fit()

    # lbpa_iset.map_inference(lbpa.u, lbpa.unpack_v(lbpa.v))
    lid = lbpa.map_indep(qp.u, lbpa.unpack_v(qp.v))

    print 'STATES ------------------------'
    foo = np.zeros(lbpa.samples, dtype=np.int8)
    foo[lbpa.label_inds] = 1
    print 'Train  = ', foo
    print 'True   = ', z
    print 'QP     = ', qp.latent
    print 'LBPA   = ', lbpa.latent
    print 'LBPAc  = ', lbpa_iset.latent
    print 'Indep  = ', lid
Exemple #11
0
def get_test_data(exms, train):
    # generate toy niidbox-data
    x, y, z = get_1d_toy_data(exms, plot=False)
    y -= np.mean(y, axis=0)
    x -= np.mean(x, axis=0)
    x = np.hstack([x, np.ones((exms, 1))])
    print x.shape
    # ...and corresponding transition matrix
    A = np.zeros((exms, exms), dtype=np.int32)
    for j in range(1, 2):
        for i in range(j, exms):
            A[i - j, i] = 1
            A[i, i - j] = 1
    A = co.sparse(co.matrix(A))
    inds = np.random.permutation(exms)
    linds = inds[:train]
    lbpa = TCRFR_lbpa(x.T.copy(),
                      y[linds].copy(),
                      linds,
                      states=2,
                      A=A,
                      reg_gamma=10.,
                      reg_theta=0.8,
                      trans_sym=[1])
    qp = TCRFR_QP(x.T.copy(),
                  y[linds].copy(),
                  linds,
                  states=2,
                  A=A,
                  reg_gamma=10.,
                  reg_theta=0.8,
                  trans_sym=[1])
    bf = TCRFR_BF(x.T.copy(),
                  y[linds].copy(),
                  linds,
                  states=2,
                  A=A,
                  reg_gamma=10.,
                  reg_theta=0.8,
                  trans_sym=[1])
    return lbpa, qp, bf, x, y, z
Exemple #12
0
def test_smiley():
    # x = np.loadtxt('../../Projects/si.txt')
    # y = np.loadtxt('../../Projects/phi.txt')
    # z = np.loadtxt('../../Projects/facies.txt')
    # height, width = x.shape
    # exms = x.size
    #
    # x = x.reshape((x.size, 1), order='C')
    # y = y.reshape(y.size, order='C')
    # z = z.reshape(z.size, order='C')
    #
    # x[np.where(z==0)] -= 0.9
    #
    # y -= np.mean(y, axis=0)
    # x -= np.mean(x, axis=0)
    # y /= np.max(np.abs(y))
    # y *= 1.0
    # x /= np.max(np.abs(x))
    #
    # x = np.hstack([x, np.ones((exms, 1))])
    # print x.shape, y.shape, z.shape

    data = np.load('niidbox-data/data_smiley.npz')
    x = data['x']
    y = data['y']
    z = data['latent']
    width = data['width']
    height = data['height']
    exms = x.shape[0]
    linds = np.random.permutation(exms)[:np.int(0.1 * exms)]

    A = co.spmatrix(0, [], [], (exms, exms), tc='d')
    for k in range(1, 3):
        for i in range(height):
            for j in range(width):
                idx = i * width + j
                idx1 = (i + k) * width + j
                idx2 = i * width + j + k
                if k == 1 or (k > 1 and idx in linds) or (
                        k > 1 and idx1 in linds) or (k > 1 and idx2 in linds):
                    if i < height - k:
                        A[idx, idx1] = 1
                        A[idx1, idx] = 1
                    if j < width - k:
                        A[idx, idx2] = 1
                        A[idx2, idx] = 1

    # qp   = TCRFR_QP(x.T.copy(), y[linds].copy(), linds, states=2, A=A,
    #                 reg_gamma=10000., reg_theta=0.95, trans_sym=[1], trans_regs=[[20., 4.]])

    yyy = 1.0 * np.ones(x.shape[0])
    print x.shape
    qp = TCRFR_QP(x[:, 0].reshape((x.shape[0], 1)).T.copy(),
                  yyy.copy(),
                  np.arange(x.shape[0]),
                  states=2,
                  A=A,
                  reg_lambda=2. * (x.shape[0] * 0.99),
                  reg_gamma=10000.,
                  reg_theta=0.85,
                  trans_sym=[1],
                  trans_regs=[[20., 4.]])
    qp.set_log_partition(qp.LOGZ_PL_SUM)
    # u = np.random.randn(qp.get_num_feats()*qp.S)
    # v = np.random.randn(qp.get_num_compressed_dims())
    #np.savez('../../Projects/hotstart.npz', start=(u, v, linds))
    (u, v, linds) = np.load('../../Projects/hotstart.npz')['start']

    # qp.fit(use_grads=False, hotstart=(u, v), auto_adjust=True)
    qp.fit(use_grads=False, hotstart=None, auto_adjust=False)

    lbpa = TCRFR_lbpa(x.T.copy(),
                      y[linds].copy(),
                      linds,
                      states=2,
                      A=A,
                      reg_gamma=10000.,
                      reg_theta=0.49995,
                      trans_sym=[1],
                      trans_regs=[[20., 4.]])
    lbpa.verbosity_level = 3
    lbpa.set_log_partition(lbpa.LOGZ_PL_SUM)
    lbpa.fit(use_grads=False, hotstart=(u, v), auto_adjust=False)
    #lbpa.fit(use_grads=False, hotstart=None, auto_adjust=True)
    #lbpa.map_inference(qp.u, lbpa.unpack_v(qp.v))

    # initialize all non-fixed latent variables with random states
    import sklearn.cluster as cl
    kmeans = cl.KMeans(n_clusters=2,
                       init='random',
                       n_init=4,
                       max_iter=100,
                       tol=0.0001)
    kmeans.fit(x)

    import matplotlib.pyplot as plt
    plt.figure(1)
    plt.subplot(1, 7, 1)
    plt.imshow(y.reshape((height, width), order='C'))
    plt.subplot(1, 7, 2)
    plt.imshow(x[:, 0].reshape((height, width), order='C'))
    plt.subplot(1, 7, 3)
    plt.imshow(z.reshape((height, width), order='C'))
    plt.subplot(1, 7, 4)
    plt.imshow(lbpa.latent.reshape((height, width), order='C'))
    plt.title('LBPA')
    plt.subplot(1, 7, 5)
    plt.imshow(kmeans.labels_.reshape((height, width), order='C'))
    plt.title('KMeans')
    plt.subplot(1, 7, 6)
    plt.imshow(qp.latent.reshape((height, width), order='C'))
    plt.title('QP')
    plt.subplot(1, 7, 7)
    res, _ = lbpa.predict()
    plt.title('LBPA prediction')

    print 'Labels:', linds.size
    print 'RESULT:', np.sum((res - y) * (res - y)) / y.size

    plt.imshow(res.reshape((height, width), order='C'))
    plt.show()
Exemple #13
0
def test_smiley():
    # x = np.loadtxt('../../Projects/si.txt')
    # y = np.loadtxt('../../Projects/phi.txt')
    # z = np.loadtxt('../../Projects/facies.txt')
    # height, width = x.shape
    # exms = x.size
    #
    # x = x.reshape((x.size, 1), order='C')
    # y = y.reshape(y.size, order='C')
    # z = z.reshape(z.size, order='C')
    #
    # x[np.where(z==0)] -= 0.9
    #
    # y -= np.mean(y, axis=0)
    # x -= np.mean(x, axis=0)
    # y /= np.max(np.abs(y))
    # y *= 1.0
    # x /= np.max(np.abs(x))
    #
    # x = np.hstack([x, np.ones((exms, 1))])
    # print x.shape, y.shape, z.shape

    data = np.load('niidbox-data/data_smiley.npz')
    x = data['x']
    y = data['y']
    z = data['latent']
    width = data['width']
    height = data['height']
    exms = x.shape[0]
    linds = np.random.permutation(exms)[:np.int(0.1*exms)]

    A = co.spmatrix(0, [], [], (exms, exms), tc='d')
    for k in range(1, 3):
        for i in range(height):
            for j in range(width):
                idx = i*width + j
                idx1 = (i+k)*width + j
                idx2 = i*width + j + k
                if k == 1 or (k>1 and idx in linds) or (k>1 and idx1 in linds) or (k>1 and idx2 in linds):
                    if i < height-k:
                        A[idx, idx1] = 1
                        A[idx1, idx] = 1
                    if j < width-k:
                        A[idx, idx2] = 1
                        A[idx2, idx] = 1

    # qp   = TCRFR_QP(x.T.copy(), y[linds].copy(), linds, states=2, A=A,
    #                 reg_gamma=10000., reg_theta=0.95, trans_sym=[1], trans_regs=[[20., 4.]])

    yyy = 1.0*np.ones(x.shape[0])
    print x.shape
    qp   = TCRFR_QP(x[:, 0].reshape((x.shape[0], 1)).T.copy(), yyy.copy(), np.arange(x.shape[0]), states=2, A=A,
                    reg_lambda=2.*(x.shape[0]*0.99), reg_gamma=10000., reg_theta=0.85, trans_sym=[1], trans_regs=[[20., 4.]])
    qp.set_log_partition(qp.LOGZ_PL_SUM)
    # u = np.random.randn(qp.get_num_feats()*qp.S)
    # v = np.random.randn(qp.get_num_compressed_dims())
    #np.savez('../../Projects/hotstart.npz', start=(u, v, linds))
    (u, v, linds) = np.load('../../Projects/hotstart.npz')['start']

    # qp.fit(use_grads=False, hotstart=(u, v), auto_adjust=True)
    qp.fit(use_grads=False, hotstart=None, auto_adjust=False)

    lbpa = TCRFR_lbpa(x.T.copy(), y[linds].copy(), linds,  states=2, A=A,
                      reg_gamma=10000., reg_theta=0.49995, trans_sym=[1], trans_regs=[[20., 4.]])
    lbpa.verbosity_level = 3
    lbpa.set_log_partition(lbpa.LOGZ_PL_SUM)
    lbpa.fit(use_grads=False, hotstart=(u, v), auto_adjust=False)
    #lbpa.fit(use_grads=False, hotstart=None, auto_adjust=True)
    #lbpa.map_inference(qp.u, lbpa.unpack_v(qp.v))

    # initialize all non-fixed latent variables with random states
    import sklearn.cluster as cl
    kmeans = cl.KMeans(n_clusters=2, init='random', n_init=4, max_iter=100, tol=0.0001)
    kmeans.fit(x)

    import matplotlib.pyplot as plt
    plt.figure(1)
    plt.subplot(1, 7, 1)
    plt.imshow(y.reshape((height, width), order='C'))
    plt.subplot(1, 7, 2)
    plt.imshow(x[:, 0].reshape((height, width), order='C'))
    plt.subplot(1, 7, 3)
    plt.imshow(z.reshape((height, width), order='C'))
    plt.subplot(1, 7, 4)
    plt.imshow(lbpa.latent.reshape((height, width), order='C'))
    plt.title('LBPA')
    plt.subplot(1, 7, 5)
    plt.imshow(kmeans.labels_.reshape((height, width), order='C'))
    plt.title('KMeans')
    plt.subplot(1, 7, 6)
    plt.imshow(qp.latent.reshape((height, width), order='C'))
    plt.title('QP')
    plt.subplot(1, 7, 7)
    res, _ = lbpa.predict()
    plt.title('LBPA prediction')

    print 'Labels:', linds.size
    print 'RESULT:', np.sum((res - y)*(res - y))/y.size

    plt.imshow(res.reshape((height, width), order='C'))
    plt.show()