示例#1
0
def test_kwta_presentation():
    """Tests one kwta presentation to half of the units, followed by
    another presentation to the second half."""
    s = InputSample(8, 8, [[1] * 8] * 4 + [[0] * 8] * 4)
    kwta_presentation(Tns.p2, Tns.p1, s, 2)
    assert get_current_time() == 2
    rates = get_rate_encoder(Tns.p1).get_rates()
    assert_array_less(rates[4:8], rates[0:4])
    s = InputSample(8, 8, [[0] * 8] * 4 + [[1] * 8] * 4)
    kwta_presentation(Tns.p2, Tns.p1, s, 2)
    assert get_current_time() == 4
    rates = get_rate_encoder(Tns.p1).get_rates()
    assert_array_less(rates[0:4], rates[4:8])
示例#2
0
def test_kwta_presentation():
    """Tests one kwta presentation to half of the units, followed by
    another presentation to the second half."""
    s = InputSample(8, 8, [[1] * 8] * 4 + [[0] * 8] * 4)
    kwta_presentation(Tns.p2, Tns.p1, s, 2)
    assert get_current_time() == 2
    rates = get_rate_encoder(Tns.p1).get_rates()
    assert_array_less(rates[4:8], rates[0:4])
    s = InputSample(8, 8, [[0] * 8] * 4 + [[1] * 8] * 4)
    kwta_presentation(Tns.p2, Tns.p1, s, 2)
    assert get_current_time() == 4
    rates = get_rate_encoder(Tns.p1).get_rates()
    assert_array_less(rates[0:4], rates[4:8])
示例#3
0
def test_kwta_epoch_1_winner():
    Tns.p2.max_unit_rate = 10
    Tns.p1.max_unit_rate = 3.3
    sum_weights_before_1st_epoch = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    kwta_epoch(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=itertools.repeat(Tns.sample1, 2),
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=25,
               learning_rule=conditional_pca_learning,
               learning_rate=0.1,
               max_weight_value=Tns.max_weight)
    sum_weights_after_1st_epoch = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    weights_diff_1 = sum_weights_after_1st_epoch - sum_weights_before_1st_epoch
    argwinner_1 = numpy.argmax(weights_diff_1)
    # assert diff is all 0 except winner
    winner_diff_1 = weights_diff_1[argwinner_1]
    assert winner_diff_1 > 0
    weights_diff_1_nowin = numpy.array(weights_diff_1)
    weights_diff_1_nowin[argwinner_1] -= winner_diff_1
    assert_allclose(weights_diff_1_nowin, numpy.zeros(len(weights_diff_1)))
    # run some time without input to let the activity come back to 0
    from scheduling.pynn_scheduling import RATE_ENC_RESPAWN_DICT
    run_simulation(get_current_time() + 30)
    # second epoch
    kwta_epoch(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=itertools.repeat(Tns.sample2, 2),
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=25,
               learning_rule=conditional_pca_learning,
               learning_rate=0.1,
               max_weight_value=Tns.max_weight)
    sum_weights_after_2nd_epoch = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    weights_diff_2 = sum_weights_after_2nd_epoch - sum_weights_after_1st_epoch
    argwinner_2 = numpy.argmax(weights_diff_2)
    assert argwinner_1 != argwinner_2
    # assert still only one non zero diff
    winner_diff_2 = weights_diff_2[argwinner_2]
    assert winner_diff_2 > 0
    weights_diff_2_nowin = numpy.array(weights_diff_2)
    weights_diff_2_nowin[argwinner_2] -= winner_diff_2
    assert_allclose(weights_diff_2_nowin, numpy.zeros(len(weights_diff_2)))
示例#4
0
def test_kwta_epoch_1_winner():
    Tns.p2.max_unit_rate=10
    Tns.p1.max_unit_rate=3.3
    sum_weights_before_1st_epoch = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    kwta_epoch(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=itertools.repeat(Tns.sample1, 2),
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=25,
               learning_rule=conditional_pca_learning,
               learning_rate=0.1,
               max_weight_value=Tns.max_weight)
    sum_weights_after_1st_epoch = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    weights_diff_1 = sum_weights_after_1st_epoch - sum_weights_before_1st_epoch
    argwinner_1 = numpy.argmax(weights_diff_1)
    # assert diff is all 0 except winner
    winner_diff_1 = weights_diff_1[argwinner_1]
    assert winner_diff_1 > 0
    weights_diff_1_nowin = numpy.array(weights_diff_1)
    weights_diff_1_nowin[argwinner_1] -= winner_diff_1
    assert_allclose(weights_diff_1_nowin, numpy.zeros(len(weights_diff_1)))
    # run some time without input to let the activity come back to 0
    from scheduling.pynn_scheduling import RATE_ENC_RESPAWN_DICT
    run_simulation(get_current_time()+30)
    # second epoch
    kwta_epoch(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=itertools.repeat(Tns.sample2, 2),
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=25,
               learning_rule=conditional_pca_learning,
               learning_rate=0.1,
               max_weight_value=Tns.max_weight)
    sum_weights_after_2nd_epoch = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    weights_diff_2 = sum_weights_after_2nd_epoch - sum_weights_after_1st_epoch
    argwinner_2 = numpy.argmax(weights_diff_2)
    assert argwinner_1 != argwinner_2
    # assert still only one non zero diff
    winner_diff_2 = weights_diff_2[argwinner_2]
    assert winner_diff_2 > 0
    weights_diff_2_nowin = numpy.array(weights_diff_2)
    weights_diff_2_nowin[argwinner_2] -= winner_diff_2
    assert_allclose(weights_diff_2_nowin, numpy.zeros(len(weights_diff_2)))
示例#5
0
def test_train_kwta():
    Tns.p2.max_unit_rate = 10
    Tns.p1.max_unit_rate = 3.4
    sum_weights_before_1st_training = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    train_kwta(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=list(itertools.repeat(Tns.sample1, 1)),
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=22,
               learning_rule=conditional_pca_learning,
               learning_rate=.01,
               max_weight_value=Tns.max_weight,
               trained_pop_max_rate=None,
               input_pop_max_rate=None,
               min_delta_w=None,
               max_epoch=2)
    sum_weights_after_1st_training = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    weights_diff = sum_weights_after_1st_training - sum_weights_before_1st_training
    run_simulation(get_current_time() + 30)
    train_kwta(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=[Tns.sample2],
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=22,
               learning_rule=conditional_pca_learning,
               learning_rate=0.5,
               max_weight_value=Tns.max_weight,
               trained_pop_max_rate=None,
               input_pop_max_rate=None,
               min_delta_w=0.05,
               max_epoch=None)
    sum_weights_after_2nd_training = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
示例#6
0
def test_train_kwta():
    Tns.p2.max_unit_rate=10
    Tns.p1.max_unit_rate=3.4
    sum_weights_before_1st_training = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    train_kwta(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=list(itertools.repeat(Tns.sample1, 1)),
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=22,
               learning_rule=conditional_pca_learning,
               learning_rate=.01,
               max_weight_value=Tns.max_weight,
               trained_pop_max_rate=None,
               input_pop_max_rate=None,
               min_delta_w=None,
               max_epoch=2)
    sum_weights_after_1st_training = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)
    weights_diff = sum_weights_after_1st_training - sum_weights_before_1st_training
    run_simulation(get_current_time()+30)
    train_kwta(trained_population=Tns.p2,
               input_population=Tns.p1,
               projection=Tns.prj1_2,
               input_samples=[Tns.sample2],
               num_winners=1,
               neighbourhood_fn=None,
               presentation_duration=22,
               learning_rule=conditional_pca_learning,
               learning_rate=0.5,
               max_weight_value=Tns.max_weight,
               trained_pop_max_rate=None,
               input_pop_max_rate=None,
               min_delta_w=0.05,
               max_epoch=None)
    sum_weights_after_2nd_training = \
        numpy.add.reduce(get_weights(Tns.prj1_2, Tns.max_weight)._weights, axis=0)