Esempio n. 1
0
def multiple_prior_contexts():
    experiment_name = 'multiple_prior_contexts'
    directory = './' + experiment_name
    mkdir_p(directory)
    print()
    print('experiment=\'%s\'' % (experiment_name))

    e = SymbolsEncoder(
        max_symbols=26,  # maximum number of symbols
        num_s=208)  # number of statelets

    sl = SequenceLearner(
        num_spc=10,  # number of statelets per column
        num_dps=50,  # number of coincidence detectors per statelet
        num_rpd=12,  # number of receptors per coincidence detector
        d_thresh=6,  # coincidence detector threshold
        perm_thr=1,  # receptor permanence threshold
        perm_inc=1,  # receptor permanence increment
        perm_dec=0)  # receptor permanence decrement

    sl.input.add_child(e.output)

    values = [
        'a', 'z', 'a', 'z', 'a', 'z', 'b', 'z', 'b', 'z', 'b', 'z', 'c', 'z',
        'c', 'z', 'c', 'z', 'd', 'z', 'd', 'z', 'd', 'z', 'e', 'z', 'e', 'z',
        'e', 'z', 'f', 'z', 'f', 'z', 'f', 'z', 'g', 'z', 'g', 'z', 'g', 'z',
        'h', 'z', 'h', 'z', 'h', 'z', 'i', 'z', 'i', 'z', 'i', 'z', 'j', 'z',
        'j', 'z', 'j', 'z', 'k', 'z', 'k', 'z', 'k', 'z', 'l', 'z', 'l', 'z',
        'l', 'z', 'm', 'z', 'm', 'z', 'm', 'z', 'n', 'z', 'n', 'z', 'n', 'z',
        'o', 'z', 'o', 'z', 'o', 'z', 'p', 'z', 'p', 'z', 'p', 'z', 'q', 'z',
        'q', 'z', 'q', 'z', 'r', 'z', 'r', 'z', 'r', 'z', 's', 'z', 's', 'z',
        's', 'z', 't', 'z', 't', 'z', 't', 'z', 'u', 'z', 'u', 'z', 'u', 'z',
        'v', 'z', 'v', 'z', 'v', 'z', 'w', 'z', 'w', 'z', 'w', 'z', 'x', 'z',
        'x', 'z', 'x', 'z', 'y', 'z', 'y', 'z', 'y', 'z'
    ]

    le = preprocessing.LabelEncoder()
    le.fit(values)
    int_values = le.transform(values)

    scores = [0.0 for _ in range(len(values))]
    count_s_acts = [0 for _ in range(len(values))]
    count_s_hist = [0 for _ in range(len(values))]
    count_cs = [0 for _ in range(len(values))]
    hidden_s_usage = [0 for _ in range(2240)]
    output_s_usage = [0 for _ in range(2240)]

    print('val  scr  s_act  s_his    cs  active output statelets')

    for i in range(len(int_values)):
        e.compute(value=int_values[i])
        sl.compute(learn=True)

        # update information
        hidden_s_bits = sl.hidden.bits
        hidden_s_acts = sl.hidden.acts
        output_s_bits = sl.output.bits
        output_s_acts = sl.output.acts
        scores[i] = sl.get_score()
        count_s_acts[i] = len(output_s_acts)
        count_s_hist[i] = sl.get_historical_count()
        count_cs[i] = sl.get_coincidence_set_count()

        # update statelet usage
        for s in range(len(output_s_usage)):
            hidden_s_usage[s] += hidden_s_bits[s]
            output_s_usage[s] += output_s_bits[s]

        # plot statelets
        if (i + 1) % 6 == 0:
            title = values[i] + '_' + values[i - 1]
            plot_statelets(directory, 'hidden_' + title, hidden_s_bits)
            plot_statelets(directory, 'output_' + title, output_s_bits)

        # print information
        output_s_acts_str = '[' + ', '.join(
            str(act).rjust(4) for act in output_s_acts) + ']'
        print('{0:>3}  {1:0.1f}  {2:5d}  {3:5d}  {4:4d}  {5:>4}'.format(
            values[i], scores[i], count_s_acts[i], count_s_hist[i],
            count_cs[i], output_s_acts_str))

    # plot information
    plot_results(directory, 'results', values, scores, count_s_acts,
                 count_s_hist, count_cs, 600, 2200)
    plot_statelet_usage(directory, 'hidden', hidden_s_usage, 75)
    plot_statelet_usage(directory, 'output', output_s_usage, 75)
    '''
Esempio n. 2
0
def three_events(statelet_snapshots_on=False):
    experiment_name = 'three_events'
    directory = './' + experiment_name
    mkdir_p(directory)
    print()
    print('experiment=\'%s\'' % (experiment_name))

    NUM_S = 208
    NUM_SPC = 10
    TOTAL_NUM_S = NUM_S * NUM_SPC

    e = SymbolsEncoder(
        max_symbols=26,  # maximum number of symbols
        num_s=NUM_S)  # number of statelets

    sl = SequenceLearner(
        num_spc=NUM_SPC,  # number of statelets per column
        num_dps=50,  # number of coincidence detectors per statelet
        num_rpd=12,  # number of receptors per coincidence detector
        d_thresh=6,  # coincidence detector threshold
        perm_thr=1,  # receptor permanence threshold
        perm_inc=1,  # receptor permanence increment
        perm_dec=0)  # receptor permanence decrement

    sl.input.add_child(e.output)

    values = [
        'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'c', 'd', 'e',
        'f', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'f', 'e', 'd',
        'c', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'c',
        'd', 'c', 'b', 'a', 'a', 'a', 'a', 'a'
    ]

    le = preprocessing.LabelEncoder()
    le.fit(values)
    int_values = le.transform(values)

    scores = [0.0 for _ in range(len(values))]
    count_s_output_acts = [0 for _ in range(len(values))]
    count_s_hidden_acts = [0 for _ in range(len(values))]
    count_s_hist = [0 for _ in range(len(values))]
    count_cs = [0 for _ in range(len(values))]
    hidden_s_usage = [0 for _ in range(TOTAL_NUM_S)]
    output_s_usage = [0 for _ in range(TOTAL_NUM_S)]

    print('val  scr  s_act  s_his    cs  active output statelets')

    for i in range(len(int_values)):
        e.compute(value=int_values[i])
        sl.compute(learn=True)

        # update information
        hidden_s_bits = sl.hidden.bits
        hidden_s_acts = sl.hidden.acts
        output_s_bits = sl.output.bits
        output_s_acts = sl.output.acts
        scores[i] = sl.get_score()
        count_s_output_acts[i] = len(output_s_acts)
        count_s_hidden_acts[i] = len(hidden_s_acts)
        count_s_hist[i] = sl.get_historical_count()
        count_cs[i] = sl.get_coincidence_set_count()

        # update statelet usage
        for s in range(len(output_s_usage)):
            hidden_s_usage[s] += hidden_s_bits[s]
            output_s_usage[s] += output_s_bits[s]

        # plot statelets
        if statelet_snapshots_on and (i + 1) % 5 == 0 or i == 43:
            title = 'step_' + str(i) + '_' + values[i] + '_' + values[i - 1]
            plot_statelets(directory, 'hidden_' + title, hidden_s_bits)
            plot_statelets(directory, 'output_' + title, output_s_bits)

        # print information
        output_s_acts_str = '[' + ', '.join(
            str(act).rjust(4) for act in output_s_acts) + ']'
        print('{0:>3}  {1:0.1f}  {2:5d}  {3:5d}  {4:4d}  {5:>4}'.format(
            values[i], scores[i], count_s_output_acts[i], count_s_hist[i],
            count_cs[i], output_s_acts_str))

    # plot information
    plot_results(directory, 'results', values, scores, count_s_output_acts,
                 count_s_hidden_acts, count_s_hist, count_cs, 400, 400)
    plot_statelet_usage(directory, 'hidden', hidden_s_usage, 75)
    plot_statelet_usage(directory, 'output', output_s_usage, 75)