Example #1
0
def test_stop3():
    assert not stop3.is_stop()
    stop3.update_information(example_saver)
    example_saver_local = copy.deepcopy(example_saver)
    assert stop3._accum_cost == 0
    example_saver_local.add_state(State(select_index=[2], performance=0.89, cost=[3]))
    stop3.update_information(example_saver_local)
    assert stop3._accum_cost == 3
    assert not stop3.is_stop()
    example_saver_local.add_state(State(select_index=[3], performance=0.89, cost=[7]))
    stop3.update_information(example_saver_local)
    assert stop3._accum_cost == 10
    assert stop3.is_stop()
Example #2
0
def test_stop4():
    assert not stop4.is_stop()
    stop4.update_information(example_saver)
    example_saver_local = copy.deepcopy(example_saver)
    assert stop4._percent == 0
    example_saver_local.add_state(State(select_index=[2], performance=0.89, cost=[3]))
    stop4.update_information(example_saver_local)
    assert stop4._percent == 1/6
    assert stop4.is_stop()
Example #3
0
def test_stop2():
    assert not stop2.is_stop()
    stop2.update_information(example_saver)
    example_saver_local = copy.deepcopy(example_saver)
    assert stop2._current_iter == 0
    example_saver_local.add_state(State(select_index=[2], performance=0.89))
    stop2.update_information(example_saver_local)
    assert stop2._current_iter == 1
    assert not stop2.is_stop()
    stop2._current_iter = 10
    assert stop2.is_stop()
Example #4
0
def target_func(round, train_id, test_id, Lcollection, Ucollection, saver, examples, labels, global_parameters):
    # your query strategy
    qs = QueryInstanceQBC(examples, labels, disagreement='vote_entropy')
    # your model
    reg = linear_model.LogisticRegression(solver='liblinear')
    reg.fit(X=examples[Lcollection.index, :], y=labels[Lcollection.index])
    # stopping criterion
    while len(Ucollection) > 30:
        select_index = qs.select(Lcollection, Ucollection, reg, n_jobs=1)
        Ucollection.difference_update(select_index)
        Lcollection.update(select_index)

        # update model
        reg.fit(X=examples[Lcollection.index, :], y=labels[Lcollection.index])
        pred = reg.predict(examples[test_id, :])
        accuracy = sum(pred == labels[test_id]) / len(test_id)

        # save intermediate results
        st = State(select_index=select_index, performance=accuracy)
        saver.add_state(st)
        saver.save()      
Example #5
0
def run_thread(round, train_id, test_id, Lcollection, Ucollection, saver, examples, labels, global_parameters):
    # initialize object
    reg.fit(X=examples[Lcollection.index, :], y=labels[Lcollection.index])
    pred = reg.predict(examples[test_id, :])
    accuracy = sum(pred == labels[test_id]) / len(test_id)
    # initialize StateIO module
    saver.set_initial_point(accuracy)
    while len(Ucollection) > 30:
        select_index = qs.select(Lcollection, Ucollection, model=reg)
        Ucollection.difference_update(select_index)
        Lcollection.update(select_index)

        # update model
        reg.fit(X=examples[Lcollection.index, :], y=labels[Lcollection.index])
        pred = reg.predict(examples[test_id, :])
        accuracy = sum(pred == labels[test_id]) / len(test_id)

        # save intermediate results
        st = State(select_index=select_index, performance=accuracy)
        # add user defined information
        # st.add_element(key='sub_ind', value=sub_ind)
        saver.add_state(st)
        saver.save()
Example #6
0
train_ind, test_ind, L_ind, U_ind = toolbox.get_split(round=0)
# -------Initialize StateIO----------
saver = StateIO(round=0,
                train_idx=train_ind,
                test_idx=test_ind,
                init_L=L_ind,
                init_U=U_ind,
                saving_path='.')
# or by using toolbox
# saver = toolbox.get_stateio(round=0)

saver.init_L.difference_update([0, 1, 2])
saver.init_U.update([0, 1, 2])

# -------Basic operations------------
st1_batch1 = State(select_index=[1], performance=0.89)
my_value = 'my_entry_info'
st1_batch1.add_element(key='my_entry', value=my_value)
st1_batch2 = State(select_index=[0, 1], performance=0.89)
st2_batch1 = State(select_index=[0], performance=0.89)
st3_batch1 = State(select_index=[2], performance=0.89)

saver.add_state(st1_batch1)
saver.add_state(st1_batch2)
saver.add_state(st2_batch1)

saver.save()

prev_st = saver.get_state(index=1)  # get 2nd query
# or use the index operation directly
prev_st = saver[1]
Example #7
0
        selected_feature = strategy.select(observed_entries=label_ind,
                                           unkonwn_entries=unlab_ind)

        # update index
        label_ind.update(selected_feature)
        unlab_ind.difference_update(selected_feature)

        # train/test
        lab_in_train = map_whole_index_to_train(train_idx, label_ind)
        X_mc = AFASMC_mc(X=X[train_idx], y=y[train_idx], omega=lab_in_train)
        model.fit(X_mc, y[train_idx])
        pred = model.predict(X[test_idx])
        perf = accuracy_score(y_true=y[test_idx], y_pred=pred)

        # save
        st = State(select_index=selected_feature, performance=perf)
        saver.add_state(st)
        # saver.save()

        stopping_criterion.update_information(saver)

    stopping_criterion.reset()
    AFASMC_result.append(copy.deepcopy(saver))

SVD_mc = IterativeSVD_mc(rank=4)
# Stablility
for i in range(5):
    train_idx = tr[i]
    test_idx = te[i]
    label_ind = MultiLabelIndexCollection(lab[i], label_size=X.shape[1])
    unlab_ind = MultiLabelIndexCollection(unlab[i], label_size=X.shape[1])
Example #8
0
from alipy.toolbox import ToolBox as acebox

X, y = load_iris(return_X_y=True)
split_count = 5
cur_path = os.path.abspath('.')
toolbox = acebox(X=X, y=y, query_type='AllLabels', saving_path=cur_path)

# split data
toolbox.split_AL(test_ratio=0.3, initial_label_rate=0.1, split_count=split_count)
saver = toolbox.get_stateio(round=0)
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    saver.init_L.difference_update([0, 1, 2])
    saver.init_U.update([0, 1, 2])

st1_batch2 = State(select_index=[0, 1], performance=0.89)
st1_batch1 = State(select_index=[1], performance=0.89)
st2_batch1 = State(select_index=[0], performance=0.89)
st3_batch1 = State(select_index=[2], performance=0.89)


def test_stateio_validity_checking():
    saver.add_state(st1_batch1)
    saver.add_state(st1_batch2)
    saver.add_state(st2_batch1)
    assert not saver.check_batch_size()
    assert saver.cost_inall == 0
    nq, cost = saver.refresh_info()
    assert nq == 4
    assert cost == 0