示例#1
0
def test_concept_active_learner():
    n_features = 3
    hyp_space_type = "boundary"

    active_learner = ConceptActiveLearner(n_features, hyp_space_type)
    active_learner.update_posterior()
    active_learner_prob = active_learner.expected_information_gain()

    first_feature_prob = np.array([np.log2(4) - 3/4 * np.log2(3),
                                   np.log2(4) - np.log2(2),
                                   np.log2(4) - 3/4 * np.log2(3)])

    assert np.allclose(first_feature_prob,
                       active_learner_prob)
示例#2
0
def run_three_feature_line_simulations():
    hyp_space_type = "line"
    n_hyp = 6
    n_features = 3
    n_labels = 2
    sampling = "max"

    # feature, label pairs
    xs = [0, 0, 1, 1, 2, 2]
    ys = [0, 1, 0, 1, 0, 1]

    figure, ax = plt.subplots()

    al = ConceptActiveLearner(n_features, hyp_space_type, sampling)
    active_learning_prob_one = np.array(
        [al.expected_information_gain(x) for x in range(n_features)])

    # normalize
    active_learning_prob_one = active_learning_prob_one / \
        np.sum(active_learning_prob_one)

    # get predictions from self-teaching model
    st = ConceptSelfTeacher(n_features, hyp_space_type, sampling)
    st.update_learner_posterior()
    st.update_self_teaching_posterior()
    self_teacher_prob_one = st.self_teaching_posterior[0, :, 0]

    plt.figure()
    plt.plot(
        np.arange(1, n_features + 1),
        active_learning_prob_one,
        color='red',
        # linestyle=':',
        label="Information Gain model")
    plt.plot(np.arange(1, n_features + 1),
             self_teacher_prob_one,
             color='blue',
             label="Self-teaching model")
    axes = plt.gca()
    # axes.set_ylim([0.1, 0.15])
    plt.xticks(np.arange(1, n_features + 1), fontsize=16)
    plt.yticks(fontsize=16)
    axes.set_ylim([0, 0.4])
    handles, labels = ax.get_legend_handles_labels()
    plt.legend(loc='lower center', fontsize=16)
    plt.xlabel("Feature", fontsize=16)
    plt.ylabel("Probability of selecting feature", fontsize=16)
    plt.tight_layout()
    plt.savefig('figures/concept_learning_line_three_features.png')
示例#3
0
def run_eight_feature_line_simulations():
    hyp_space_type = "line"
    n_hyp = 36
    n_features = 8

    figure, ax = plt.subplots()

    al = ConceptActiveLearner(n_features, hyp_space_type)
    al.update_posterior()
    active_learning_prob_one = al.expected_information_gain()

    # normalize
    active_learning_prob_one = active_learning_prob_one / \
        np.sum(active_learning_prob_one)

    # get predictions from self-teaching model
    st = ConceptSelfTeacher(n_features, hyp_space_type)
    st.update_learner_posterior()
    st.update_self_teaching_posterior()
    self_teacher_prob_one = st.self_teaching_posterior

    plt.figure()
    plt.plot(
        np.arange(1, n_features + 1),
        active_learning_prob_one,
        color='red',
        # linestyle=':',
        label="Information Gain model")
    plt.plot(np.arange(1, n_features + 1),
             self_teacher_prob_one,
             color='blue',
             label="Self-teaching model")
    axes = plt.gca()
    axes.set_ylim([0, 0.2])
    plt.xticks(np.arange(1, n_features + 1), fontsize=16)
    plt.yticks(fontsize=16)
    handles, labels = ax.get_legend_handles_labels()
    plt.legend(loc='lower center', fontsize=16)
    plt.xlabel("Feature", fontsize=16)
    plt.ylabel("Probability of selecting feature", fontsize=16)
    plt.tight_layout()
    plt.savefig('figures/concept_learning_line_eight_features.png')
示例#4
0
def run_second_feature_boundary_simulations():
    hyp_space_type = "boundary"
    n_hyp = 4
    n_features = 3
    n_labels = 2
    sampling = "max"

    # feature, label pairs
    xs = [0, 1, 1, 2]
    ys = [1, 0, 1, 0]

    figure, ax = plt.subplots()
    figure.set_size_inches(10, 10)

    for i, (x, y) in enumerate(zip(xs, ys)):
        # get predictions from active learning model
        al = ConceptActiveLearner(n_features, hyp_space_type, sampling)
        active_learning_prob_one = np.array(
            [al.expected_information_gain(x) for x in range(n_features)])

        # normalize
        active_learning_prob_one = active_learning_prob_one / \
            np.sum(active_learning_prob_one)

        # perform update
        al.update(x=x, y=y)
        active_learning_prob_two = np.array(
            [al.expected_information_gain(x) for x in range(n_features)])

        # normalize
        denom = np.sum(active_learning_prob_two)
        active_learning_prob_two = np.divide(active_learning_prob_two,
                                             denom,
                                             where=denom != 0)
        active_learning_prob_two[np.isclose(denom, 0)] = 0

        # get predictions from self-teaching model
        st = ConceptSelfTeacher(n_features, hyp_space_type, sampling)
        st.update_learner_posterior()
        st.update_self_teaching_posterior()
        self_teacher_prob_one = st.self_teaching_posterior[0, :, 0]

        # update learner posterior after a single observation
        updated_learner_posterior = st.learner_posterior[:, x, y]
        st.learner_posterior = np.repeat(updated_learner_posterior,
                                         n_labels * n_features).reshape(
                                             n_hyp, n_features, n_labels)

        # update learner and self-teacher after a single observation
        st.update_learner_posterior()
        st.update_self_teaching_posterior()
        self_teacher_prob_two = st.self_teaching_posterior[0, :, 0]

        # plot second feature prob
        plt.subplot(2, 2, i + 1)
        plt.plot(
            np.arange(1, n_features + 1),
            active_learning_prob_two,
            color='red',
            # linestyle=':',
            label="Information Gain model")
        plt.plot(np.arange(1, n_features + 1),
                 self_teacher_prob_two,
                 color='blue',
                 label="Self-teaching model")
        axes = plt.gca()
        axes.set_ylim([0, 1])
        plt.xlabel("Feature", fontsize=16)
        plt.ylabel("Probability of selecting feature", fontsize=16)
        plt.title('x = {}, y = {}'.format(x + 1, y), fontsize=18)
        plt.xticks([1, 2, 3], fontsize=16)
        plt.yticks(fontsize=16)
        handles, labels = ax.get_legend_handles_labels()
        plt.legend(fontsize=12)

    plt.tight_layout()
    plt.savefig('figures/concept_learning_boundary_second_feature_prob.png')