Ejemplo n.º 1
0
def test_batch_solvers_3():
    A_ph = tf.placeholder('float32', [None, None, None], name='A')
    B_ph = tf.placeholder('float32', [None, None, None], name='B')

    es = ExactSolver()
    js = JacobiSolver(nb_iterations=10)

    eX = es.solve(A=A_ph, B=B_ph)
    jX = js.solve(A=A_ph, B=B_ph)

    A_value = np.array([[4.0, -1.0, 1.0],
                        [4.0, -8.0, 1.0],
                        [-2.0, 1.0, 5.0]]).reshape(3, 3)

    B_value = np.array([[7.0, -21.0, 15.0],
                        [7.0, -21.0, 15.0]]).reshape(3, 2)

    batch_A_value = np.zeros(shape=[2, 3, 3], dtype='float32')
    batch_B_value = np.zeros(shape=[2, 3, 2], dtype='float32')

    batch_A_value[0, :, :] = A_value
    batch_B_value[0, :, :] = B_value

    batch_A_value[1, :, :] = A_value
    batch_B_value[1, :, :] = B_value

    with tf.Session() as session:
        feed_dict = {A_ph: batch_A_value, B_ph: batch_B_value}

        eX_value = session.run(eX, feed_dict=feed_dict)
        jX_value = session.run(jX, feed_dict=feed_dict)

        np.testing.assert_allclose(eX_value, jX_value, rtol=1e-4)
Ejemplo n.º 2
0
def test_model():
    rows, cols = 4, 4
    N = rows * cols

    edges = [[(i, j), (i, j + 1)] for i in range(rows) for j in range(cols)
             if i < rows and j < cols - 1]
    edges += [[(i, j), (i + 1, j)] for i in range(rows) for j in range(cols)
              if i < rows - 1 and j < cols]

    W = np.zeros((N, N))

    for [(i, j), (k, l)] in edges:
        row, col = i * rows + j, k * cols + l
        W[row, col] = W[col, row] = 1

    l, y = np.zeros(shape=[
        N,
    ], dtype='int8'), np.zeros(shape=[
        N,
    ])
    l[0], y[0] = 1, 1
    l[rows - 1], y[rows - 1] = 1, 1

    l[N - 1], y[N - 1] = 1, -1
    l[N - rows], y[N - rows] = 1, -1

    mu, eps = 1.0, 1e-8

    l_ph = tf.placeholder('float32', shape=[None, None], name='l')
    y_ph = tf.placeholder('float32', shape=[None, None], name='y')

    mu_ph = tf.placeholder('float32', [None], name='mu')
    eps_ph = tf.placeholder('float32', [None], name='eps')

    W_ph = tf.placeholder('float32', shape=[None, None, None], name='W')

    with tf.Session() as session:
        solver = ExactSolver()

        feed_dict = {
            l_ph: l.reshape(1, N),
            y_ph: y.reshape(1, N),
            W_ph: W.reshape(1, N, N),
            mu_ph: np.array([mu] * 1),
            eps_ph: np.array([eps] * 1),
        }

        model = GaussianFields(l_ph, y_ph, mu_ph, W_ph, eps_ph, solver=solver)

        f = model.minimize()

        f_value = session.run(f, feed_dict=feed_dict)

        assert f_value[0, 1] > 0
        assert f_value[0, N - 2] < 0
Ejemplo n.º 3
0
def main(argv):
    tf.set_random_seed(0)

    nb_rows, nb_cols = 8, 8
    nb_nodes = nb_rows * nb_cols

    edges_horizontal = [[(i, j), (i, j + 1)] for i in range(nb_rows)
                        for j in range(nb_cols)
                        if i < nb_rows and j < nb_cols - 1]

    edges_vertical = [[(i, j), (i + 1, j)] for i in range(nb_rows)
                      for j in range(nb_cols)
                      if i < nb_rows - 1 and j < nb_cols]

    edges = [(e, 0) for e in edges_horizontal] + [(e, 1)
                                                  for e in edges_vertical]

    # We do not have an adjacency matrix, but rather a multi-relational adjacency tensor
    T = np.zeros(shape=[nb_nodes, nb_nodes, 2], dtype='float32')

    for ([(i, j), (k, l)], g) in edges:
        row, col = i * nb_rows + j, k * nb_cols + l
        T[row, col, g] = T[col, row, g] = 1.0

    l = np.zeros(shape=[nb_rows, nb_cols], dtype='int8')
    y = np.zeros(shape=[nb_rows, nb_cols], dtype='float32')

    l[0, 0] = 1
    y[0, 0] = 1.0

    l[0, nb_cols - 1] = 1
    y[0, nb_cols - 1] = 1.0

    l[nb_rows - 1, 0] = 1
    y[nb_rows - 1, 0] = -1.0

    l[nb_rows - 1, nb_cols - 1] = 1
    y[nb_rows - 1, nb_cols - 1] = -1.0

    mu, eps = 10.0, 1e-2

    batch_l = np.zeros(shape=[1, nb_nodes], dtype='float32')
    batch_y = np.zeros(shape=[1, nb_nodes], dtype='float32')
    batch_T = np.zeros(shape=[1, nb_nodes, nb_nodes, T.shape[2]],
                       dtype='float32')

    batch_l[0, :] = l.reshape(nb_nodes)
    batch_y[0, :] = y.reshape(nb_nodes)
    batch_T[0, :, :, :] = T

    l_ph = tf.placeholder('float32', shape=[None, None], name='l')
    y_ph = tf.placeholder('float32', shape=[None, None], name='y')

    mu_ph = tf.placeholder('float32', [None], name='mu')
    eps_ph = tf.placeholder('float32', [None], name='eps')

    alpha = tf.get_variable('alpha',
                            shape=[T.shape[2]],
                            initializer=tf.contrib.layers.xavier_initializer())

    T_ph = tf.placeholder('float32', shape=[None, None, None, None], name='T')

    W = tf.einsum('a,bmna->bmn', alpha, T_ph)

    solver = ExactSolver()
    # solver = JacobiSolver(100)

    l_idxs = tf.where(l_ph > 0)

    def clip(x, epsilon=1e-10):
        return tf.clip_by_value(x, epsilon, 1.0)

    def leave_one_out_loss(a, x, epsilon=1e-4):
        idx_int = tf.cast(x, tf.int32)
        row_idx = tf.cast(l_idxs[idx_int, 0], tf.int32)
        col_idx = tf.cast(l_idxs[idx_int, 1], tf.int32)

        mask = tf.sparse_to_dense(sparse_indices=[[row_idx, col_idx]],
                                  output_shape=tf.shape(l_ph),
                                  sparse_values=0,
                                  default_value=1)
        mask = tf.cast(mask, tf.float32)

        mask = tf.nn.dropout(mask, keep_prob=0.8)

        model = GaussianFields(l=l_ph * mask,
                               y=y_ph,
                               mu=mu_ph,
                               W=W,
                               eps=eps_ph,
                               solver=solver)
        f_star = model.minimize()

        f_value = tf.cast(f_star[row_idx, col_idx], tf.float32)
        y_value = tf.cast(y_ph[row_idx, col_idx], tf.float32)

        loss_value = (y_value - f_value)**2.0
        # loss_value = abs(f_value - y_value)
        # loss_value = - y_value * tf.log(clip(f_value, epsilon))\
        #              - (1 - y_value) * tf.log(clip(1 - f_value, epsilon))
        res = a + loss_value
        return res

    elems = tf.range(tf.shape(l_idxs)[0])
    elems = tf.identity(elems)

    initializer = tf.constant(0.0, dtype=tf.float32)

    loo_losses = tf.scan(lambda a, x: leave_one_out_loss(a, x),
                         elems=elems,
                         initializer=initializer)
    loo_loss = loo_losses[-1]

    regularized_loo_loss = loo_loss + 1e-6 * tf.nn.l2_loss(alpha)

    inf_model = GaussianFields(l=l_ph,
                               y=y_ph,
                               mu=mu_ph,
                               W=W,
                               eps=eps_ph,
                               solver=solver)
    inf_f_star = inf_model.minimize()

    optimizer = tf.train.AdamOptimizer(learning_rate=0.05)
    train_op = optimizer.minimize(regularized_loo_loss, var_list=[alpha])

    init_op = tf.global_variables_initializer()

    feed_dict = {
        l_ph: batch_l,
        y_ph: batch_y,
        T_ph: batch_T,
        mu_ph: np.array([mu]),
        eps_ph: np.array([eps]),
    }

    session_config = tf.ConfigProto()
    session_config.gpu_options.allow_growth = True

    with tf.Session(config=session_config) as session:
        session.run(init_op)

        for i in range(128):
            session.run(train_op, feed_dict=feed_dict)

            loo_loss_value = session.run(loo_loss, feed_dict=feed_dict)
            # alpha_value = session.run(alpha, feed_dict=feed_dict)

            # print(loo_loss_value, alpha_value)

            hd = HintonDiagram()
            inf_f_value = session.run(inf_f_star, feed_dict=feed_dict)

            diagram = hd(inf_f_value[0, :].reshape((nb_rows, nb_cols)))
            os.system('clear')

            print(diagram)
            print('Leave One Out Loss: {:.6}'.format(loo_loss_value))
Ejemplo n.º 4
0
def test_minimize():
    nb_rows, nb_cols = 40, 40
    nb_nodes = nb_rows * nb_cols

    edges = []

    edges += [[(i, j), (i, j + 1)] for i in range(nb_rows)
              for j in range(nb_cols) if i < nb_rows and j < nb_cols - 1]

    edges += [[(i, j), (i + 1, j)] for i in range(nb_rows)
              for j in range(nb_cols) if i < nb_rows - 1 and j < nb_cols]

    # edges += [[(i, j), (i + 1, j + 1)]
    #           for i in range(nb_rows) for j in range(nb_cols)
    #           if i < nb_rows - 1 and j < nb_cols - 1]

    W = np.zeros(shape=[nb_nodes, nb_nodes], dtype='float32')

    for [(i, j), (k, l)] in edges:
        row, col = i * nb_rows + j, k * nb_cols + l
        W[row, col] = W[col, row] = 1

    mu, eps = 1.0, 1e-8

    l_ph = tf.placeholder('float32', shape=[None, None], name='l')
    y_ph = tf.placeholder('float32', shape=[None, None], name='y')

    mu_ph = tf.placeholder('float32', [None], name='mu')
    eps_ph = tf.placeholder('float32', [None], name='eps')

    W_ph = tf.placeholder('float32', shape=[None, None, None], name='W')

    f_ph = tf.placeholder('float32', shape=[None, None], name='f')

    solver = ExactSolver()
    model = GaussianFields(l_ph, y_ph, mu_ph, W_ph, eps_ph, solver=solver)
    e = model(f_ph)

    f_star = model.minimize()

    rs = np.random.RandomState(0)

    with tf.Session() as session:

        for _ in range(8):
            l = np.zeros(shape=[nb_rows, nb_cols], dtype='int8')
            y = np.zeros(shape=[nb_rows, nb_cols], dtype='float32')

            for i in range(l.shape[0]):
                for j in range(l.shape[1]):
                    l[i, j] = rs.randint(2)
                    y[i, j] = rs.rand()

            batch_l = np.zeros(shape=[2, nb_nodes])
            batch_y = np.zeros(shape=[2, nb_nodes])
            batch_W = np.zeros(shape=[2, nb_nodes, nb_nodes])

            batch_l[0, :] = l.reshape(nb_nodes)
            batch_y[0, :] = y.reshape(nb_nodes)
            batch_W[0, :, :] = W

            batch_l[1, :] = l.reshape(nb_nodes)
            batch_y[1, :] = y.reshape(nb_nodes)
            batch_W[1, :, :] = -W

            feed_dict = {
                l_ph: batch_l,
                y_ph: batch_y,
                W_ph: batch_W,
                mu_ph: np.array([mu] * 2),
                eps_ph: np.array([eps] * 2)
            }

            f_value = session.run(f_star, feed_dict=feed_dict)

            feed_dict = {
                l_ph: batch_l,
                y_ph: batch_y,
                W_ph: batch_W,
                mu_ph: np.array([mu] * 2),
                eps_ph: np.array([eps] * 2),
                f_ph: f_value
            }

            minimum_e_value = session.run(e, feed_dict=feed_dict)

            for _ in range(8):
                new_f_value = np.copy(f_value)
                for i in range(f_value.shape[0]):
                    for j in range(f_value.shape[1]):
                        new_f_value[i, j] += rs.normal(0.0, 0.1)

                feed_dict = {
                    l_ph: batch_l,
                    y_ph: batch_y,
                    W_ph: batch_W,
                    mu_ph: np.array([mu] * 2),
                    eps_ph: np.array([eps] * 2),
                    f_ph: new_f_value
                }

                new_e_value = session.run(e, feed_dict=feed_dict)

                for i in range(minimum_e_value.shape[0]):
                    assert minimum_e_value[i] <= new_e_value[i]
Ejemplo n.º 5
0
def main(argv):
    nb_rows, nb_cols = 40, 40
    nb_nodes = nb_rows * nb_cols

    edges = []

    edges += [[(i, j), (i, j + 1)] for i in range(nb_rows)
              for j in range(nb_cols) if i < nb_rows and j < nb_cols - 1]

    edges += [[(i, j), (i + 1, j)] for i in range(nb_rows)
              for j in range(nb_cols) if i < nb_rows - 1 and j < nb_cols]

    # edges += [[(i, j), (i + 1, j + 1)]
    #           for i in range(nb_rows) for j in range(nb_cols)
    #           if i < nb_rows - 1 and j < nb_cols - 1]

    W = np.zeros(shape=[nb_nodes, nb_nodes], dtype='float32')

    for [(i, j), (k, l)] in edges:
        row, col = i * nb_rows + j, k * nb_cols + l
        W[row, col] = W[col, row] = 1

    l = np.zeros(shape=[nb_rows, nb_cols], dtype='int8')
    y = np.zeros(shape=[nb_rows, nb_cols], dtype='float32')

    l[0, 0] = 1
    y[0, 0] = 1.1

    l[nb_rows - 1, nb_cols - 1] = 1
    y[nb_rows - 1, nb_cols - 1] = -1.0

    mu, eps = 1.0, 1e-8

    batch_l = np.zeros(shape=[2, nb_nodes], dtype='float32')
    batch_y = np.zeros(shape=[2, nb_nodes], dtype='float32')
    batch_W = np.zeros(shape=[2, nb_nodes, nb_nodes], dtype='float32')

    batch_l[0, :] = l.reshape(nb_nodes)
    batch_y[0, :] = y.reshape(nb_nodes)
    batch_W[0, :, :] = W

    batch_l[1, :] = l.reshape(nb_nodes)
    batch_y[1, :] = y.reshape(nb_nodes)
    batch_W[1, :, :] = -W

    l_ph = tf.placeholder('float32', shape=[None, None], name='l')
    y_ph = tf.placeholder('float32', shape=[None, None], name='y')

    mu_ph = tf.placeholder('float32', [None], name='mu')
    eps_ph = tf.placeholder('float32', [None], name='eps')

    W_ph = tf.placeholder('float32', shape=[None, None, None], name='W')

    solver = ExactSolver()
    # solver = JacobiSolver()
    model = GaussianFields(l=l_ph,
                           y=y_ph,
                           mu=mu_ph,
                           W=W_ph,
                           eps=eps_ph,
                           solver=solver)

    f_star = model.minimize()

    feed_dict = {
        l_ph: batch_l,
        y_ph: batch_y,
        W_ph: batch_W,
        mu_ph: np.array([mu] * 2),
        eps_ph: np.array([eps] * 2),
    }

    with tf.Session() as session:
        hd = HintonDiagram()

        f_value = session.run(f_star, feed_dict=feed_dict)

        f_value_0 = f_value[0, :]
        print(hd(f_value_0.reshape((nb_rows, nb_cols))))