예제 #1
0
파일: ecasim.py 프로젝트: RafaelMri/reca
def sim(raw_args):
    """Internal test for the ECA"""

    size = 5
    rule = 90
    simple = False
    iterations = 0

    if len(raw_args) > 1:
        # length, simple, rule
        opts, args = getopt.getopt(raw_args[1:], "s:r:I:", ['simple'])
        for o, a in opts:
            if o == '-s':
                size = int(a)
            elif o == '--simple':
                simple = True
            elif o == '-r':
                rule = int(a)
            elif o == '-I':
                iterations = int(a)

    if iterations == 0:
        iterations = int(math.ceil((size + 1) / 2))
    config = cutil.config_simple(size) if simple else cutil.config_rand(size)
    automation = ECA(rule)

    state_vector = []
    state_vector.extend(config)
    for t in xrange(iterations):
        # util.print_config_1dim(config)
        config = automation.step(config)
        state_vector.extend(config)

    plot_temporal([state_vector], 1, size, 1, 1 + iterations, sample_nr=0)
예제 #2
0
def main(raw_args):
    input_size, rule, iterations, random_mappings, input_area, automaton_area = digest_args(
        raw_args)

    if iterations == 0:
        iterations = int(math.ceil((input_size + 1) / 2))

    encoder = ClassicEncoder(random_mappings, input_size, input_area,
                             automaton_area)
    automaton = ECA(rule)
    reservoir = Reservoir(automaton, iterations, verbose=False)

    # Training
    raw_train_inputs, train_labels = problems.density(5000,
                                                      input_size,
                                                      on_probability=0.5)
    train_inputs = encoder.translate(raw_train_inputs)
    train_outputs = reservoir.transform(train_inputs)

    sys.stdout.write("Fitting... ")
    sys.stdout.flush()
    # regr = linear_model.LinearRegression()
    # regr.fit(train_outputs, train_labels)
    regr = svm.SVC(kernel='linear')
    regr.fit(train_outputs, train_labels)
    sys.stdout.write("Done\n")
    sys.stdout.flush()

    # Testing
    raw_test_inputs, y_true = problems.density(500,
                                               input_size,
                                               on_probability=0.5)
    test_inputs = encoder.translate(raw_test_inputs)
    test_outputs = reservoir.transform(test_inputs)
    y_pred = regr.predict(test_outputs)
    y_pred_class = rutil.classify_output(y_pred)
    error = mean_squared_error(y_true, y_pred)

    print "TEST RESULTS (samples, every 50th value)"
    print "Predicted, raw:    ", y_pred[::50]
    print "Predicted:         ", y_pred_class[::50]
    print "Actual:            ", y_true[::50]
    print "Mean squared error:", error
    corrects = 0
    for i in xrange(len(y_pred_class)):
        if y_pred_class[i] == y_true[i]:
            corrects += 1
    print "Correctness (%):   ", (100 * corrects / len(y_pred_class))
예제 #3
0
def main(raw_args):
    input_size, rule, iterations, random_mappings, input_area, automaton_area = digest_args(
        raw_args)

    if iterations == 0:
        iterations = int(math.ceil((input_size + 1) / 2))

    concat_before = True
    verbose = 0

    encoder = ClassicEncoder(random_mappings,
                             input_size,
                             input_area,
                             automaton_area,
                             verbose=verbose)
    automaton = ECA(rule)
    reservoir = Reservoir(automaton, iterations, verbose=verbose)
    estimator = svm.SVC()
    # estimator = svm.SVC(kernel='linear')
    # estimator = linear_model.LinearRegression()
    computer = Computer(encoder,
                        reservoir,
                        estimator,
                        concat_before=concat_before,
                        verbose=verbose)

    # Generating problem sets
    n_training_sets = 1000
    n_testing_sets = 200
    raw_inputs, labels = problems.parity(n_training_sets + n_testing_sets,
                                         input_size)

    if verbose > 1:
        print "Initial configurations:"
        for i, ri in enumerate(raw_inputs):
            cutil.print_config_1dim(ri, postfix="(%d)" % i)

    time_checkpoint = time.time()

    computer.train([raw_inputs[:n_training_sets]], [labels[:n_training_sets]])

    print "Training time: ", (time.time() - time_checkpoint)
    time_checkpoint = time.time()

    # Testing
    y_pred, _ = computer.test(raw_inputs[n_training_sets:])

    step = 50
    print "Testing time: ", (time.time() - time_checkpoint)
    print "TEST RESULTS (samples, every 50th value)"
    print "Predicted, raw:    ", y_pred[::step]
    error = mean_squared_error(labels[n_training_sets:], y_pred)

    y_pred = rutil.classify_output(y_pred)

    print "Predicted:         ", y_pred[::step]
    print "Actual:            ", labels[n_training_sets::step]
    print "Mean squared error:", error
    corrects = 0
    for i in xrange(len(y_pred)):
        if y_pred[i] == labels[n_training_sets + i]:
            corrects += 1
    print "Correctness (%):   ", (100 * corrects / len(y_pred))

    for i in xrange(10):
        print raw_inputs[n_training_sets + i * step], "->", y_pred[i * step]
예제 #4
0
def main(raw_args):
    _, size, rule, n_iterations, n_random_mappings, input_area, automaton_area = digest_args(
        raw_args)

    n_training_sets = 30
    n_testing_sets = 20
    time_steps = 20
    delay = 0
    concat_before = True
    verbose = 1

    encoder = ClassicEncoder(n_random_mappings,
                             size,
                             input_area,
                             automaton_area,
                             verbose=verbose)
    automation = ECA(rule)
    reservoir = Reservoir(automation, n_iterations, verbose=verbose)
    # estimator = svm.SVC()
    # estimator = svm.SVC(kernel='linear')
    estimator = linear_model.LinearRegression()
    computer = Computer(encoder,
                        reservoir,
                        estimator,
                        concat_before=concat_before,
                        verbose=verbose)

    training_inputs, training_labels = problems.temporal_parity(
        n_training_sets, time_steps, window_size=size, delay=delay)
    testing_inputs, testing_labels = problems.temporal_parity(n_testing_sets,
                                                              time_steps,
                                                              window_size=size,
                                                              delay=delay)

    sample_i = n_testing_sets / 2
    sample_quantity = 8
    print "Sample input, output:", zip(
        testing_inputs[sample_i][:sample_quantity],
        testing_labels[sample_i][:sample_quantity])

    time_checkpoint = time.time()
    computer.train(training_inputs, training_labels)
    x, y_pred_pre = computer.test(testing_inputs)
    y_pred = [classify_output(set_prediction) for set_prediction in y_pred_pre]
    print "Training and testing time:", (time.time() - time_checkpoint)

    n_correct = 0
    n_semi_correct = 0
    n_total_semi = 0
    print "Predicted (raw): [%s]" % ', '.join(
        ["%.2f" % value for value in y_pred_pre[sample_i][:sample_quantity]])
    print "Predicted:      ", y_pred[sample_i][:sample_quantity]
    print "Factual:        ", testing_labels[sample_i][:sample_quantity]
    for predicted, actual in zip(y_pred, testing_labels):
        correct = True
        for y1, y2 in zip(predicted, actual):
            if y1 != y2:
                correct = False
            else:
                n_semi_correct += 1
            n_total_semi += 1
        n_correct += 1 if correct else 0
    print "Correct:       %d/%d" % (n_correct, n_testing_sets)
    print "Semi correct:  %d/%d" % (n_semi_correct, n_total_semi)

    plot_temporal(x,
                  n_random_mappings,
                  encoder.automaton_area,
                  time_steps + delay,
                  n_iterations,
                  sample_nr=sample_i)
예제 #5
0
파일: cifar.py 프로젝트: RafaelMri/reca
    "data_batch_1", "data_batch_2", "data_batch_3", "data_batch_4",
    "data_batch_5"
])
testing_sets, testing_labels = read_files(["test_batch"])

training_sets = training_sets.reshape((50000, 1, 3072))
training_labels = np.array(training_labels).reshape((50000, 1))
testing_sets = testing_sets.reshape((10000, 1, 3072))
testing_labels = np.array(testing_labels).reshape((10000, 1))

# # q = (25, 50, 75)
# # print np.percentile(training_set.ravel(), q)

# classifier = svm.SVC()
classifier = linear_model.SGDClassifier()
ca = ECA(rule)
reservoir = Reservoir(ca, n_iterations, verbose=0)
encoder = RealEncoder(n_random_mappings, 3072, 0, 3072, verbose=0)
computer = Computer(encoder, reservoir, classifier, True, False, verbose=0)

up_to = 100
training_sets = training_sets[0:up_to]
training_labels = training_labels[0:up_to]
testing_sets = testing_sets[0:100]
testing_labels = testing_labels[0:100]

time_checkpoint = time.time()
computer.train(training_sets, training_labels)
predictions, _ = computer.test(testing_sets)
print "Train + test time:", (time.time() - time_checkpoint)
예제 #6
0
파일: 20bittask.py 프로젝트: RafaelMri/reca
def main(size, rules, n_iterations, n_random_mappings, diffuse, pad):
    n_iterations = [int(value) for value in n_iterations.split(',')]
    n_random_mappings = [int(value) for value in n_random_mappings.split(',')]
    rules = [int(value) for value in rules.split(',')]

    if not (len(n_iterations) == len(n_random_mappings)):
        raise ValueError(
            "The number of iterations and random mappings do not match.")

    size = len(inputs[0][0])  # The size of the input, or
    diffuse = size
    pad = size
    concat_before = True  # Concat the automata before or after iterating
    verbose = 0  # How much information to print to consol
    n_layers = len(
        n_random_mappings)  # The number of layers including the first

    encoders = []
    computers = []

    for layer_i in xrange(n_layers):
        automaton = ECA(rules[layer_i])
        encoder = ClassicEncoder(
            n_random_mappings[layer_i],
            size if layer_i == 0 else
            labels.shape[2],  # Input size if it's the first layer
            diffuse,
            pad,
            verbose=verbose)

        estimator = linear_model.LinearRegression(n_jobs=12)

        reservoir = Reservoir(automaton,
                              n_iterations[layer_i],
                              verbose=verbose)

        computer = Computer(encoder,
                            reservoir,
                            estimator,
                            concat_before=concat_before,
                            verbose=verbose)

        encoders.append(encoder)
        computers.append(computer)

    time_checkpoint = time.time()

    o = None  # Output of one estimator
    correct = []
    incorrect_time_steps = []
    tot_fit_time = 0

    for layer_i in xrange(n_layers):
        # Training/fitting
        try:
            # Preserving the values of the output nodes
            # For the first layer, we transform/train with the very input,
            # and for the subsequent layers, the output from the previous layer is used
            x, fit_time = computers[layer_i].train(
                inputs[:n_train] if o is None else o, labels[:n_train])
            tot_fit_time += fit_time
        except LinAlgError:
            # logging.error(linalgerrmessage)
            print "LinAlgError occured.", time.time()
            return 1

        # Predicting with test data to get the very results
        o = computers[layer_i].test(inputs[n_train:])[0]
        o = classify_output(o)
        o = unflatten(o, [n_time_steps] * n_test)

        n_correct = 0
        n_mispredicted_time_steps = 0
        for predicted_seq, desired_seq in zip(o, labels[n_train:]):
            success = True
            for prediction_element, label_element in zip(
                    predicted_seq, desired_seq):
                if not np.array_equal(prediction_element, label_element):
                    success = False
                    n_mispredicted_time_steps += 1
            if success:
                n_correct += 1
        correct.append(n_correct)
        incorrect_time_steps.append(n_mispredicted_time_steps)
        if not logit:
            print "Layer %d:\t%d correct seq.s\t%d mispred. time steps\t%.2fs" % (
                layer_i, n_correct, n_mispredicted_time_steps, tot_fit_time)

        if layer_i < n_layers - 1:
            # Predicting with train data set in order to get input to the next layer
            o = computers[layer_i].test(inputs[:n_train])[0]
            o = classify_output(o)
            o = unflatten(o, [n_time_steps] * n_train)

            # if n_whole_runs == 1:
            #     from stats.plotter import plot_temporal
            #     plot_temporal(x,
            #                   encoders[layer_i].n_random_mappings,
            #                   encoders[layer_i].automaton_area,
            #                   n_time_steps,
            #                   n_iterations[layer_i],
            #                   sample_nr=2)

    # print "Time:                   %.1f (training, testing, binarizing)" % (time.time() - time_checkpoint)

    if logit:
        result = [j for i in zip(correct, incorrect_time_steps) for j in i]
        logging.info(
            "\"%s\",\"%s\",\"%s\",%d,%d,%d,%s,%s,%.2f,%d,%d,%d,%d" +
            ",%d" * n_layers * 2, ','.join(str(e) for e in n_iterations),
            ','.join(str(e) for e in n_random_mappings),
            ','.join(str(e) for e in rules), size, diffuse, pad, concat_before,
            estimator.__class__.__name__, tot_fit_time, n_train, n_test,
            distractor_period, 1 if correct[-1] == n_test else 0, *result)

    return 0  # The run went good
예제 #7
0
파일: japvow.py 프로젝트: RafaelMri/reca
def main(initial_input_size, rules, n_iterations, n_random_mappings, diffuse, pad):
    initial_input_size = 12
    diffuse = initial_input_size
    pad = initial_input_size
    n_iterations = [int(value) for value in n_iterations.split(',')]
    n_random_mappings = [int(value) for value in n_random_mappings.split(',')]
    rules = [int(value) for value in rules.split(',')]
    encoders = []
    computers = []

    if not (len(n_iterations) == len(n_random_mappings)):
        raise ValueError("The number of iterations and random mappings do not match.")
    n_layers = len(n_random_mappings)  # The number of layers including the first

    for layer_i in xrange(n_layers):  # Setup
        automaton = ECA(rules[layer_i])

        if layer_i == 0:
            encoder = RealEncoder(n_random_mappings[layer_i],
                                  initial_input_size,
                                  diffuse,
                                  pad,
                                  quantize=quantize_japvow)
        else:
            group_len = len(quantize_activation([1]))
            encoder = ClassicEncoder(n_random_mappings[layer_i],
                                     9 * group_len + tmpencoder.automaton_area,
                                     9 * group_len + tmpencoder.automaton_area,
                                     9 * group_len + tmpencoder.automaton_area,
                                     group_len=group_len)

        # estimator = svm.SVC(kernel='linear')
        # estimator = linear_model.SGDClassifier()
        # estimator = linear_model.Perceptron()
        estimator = linear_model.LinearRegression(n_jobs=4)
        # estimator = linear_model.SGDRegressor(loss='squared_loss')

        reservoir = Reservoir(automaton,
                              n_iterations[layer_i],
                              verbose=0)

        if layer_i == n_layers - 1:  # Last layer
            computer = JaegerComputer(encoder, reservoir, estimator, True, verbose=0, d=d, method=4)
        elif layer_i == 0:  # First layer
            computer = JaegerComputer(encoder, reservoir, estimator, True, verbose=0, d=d, method=3)
        else:  # Inter-layers
            computer = Computer(encoder, reservoir, estimator, True, verbose=0)

        encoders.append(encoder)
        computers.append(computer)

    activation_levels = []
    o = None  # Output of one estimator
    tot_fit_time = 0

    for layer_i in xrange(n_layers):  # Training

        layer_inputs = training_sets if o is None else o
        layer_labels = training_labels if layer_i == 0 else subsequent_training_labels
        raw_training_input = None  # training_sets if layer_i == 0 else jaeger_training_sets

        x, fit_time = computers[layer_i].train(layer_inputs, layer_labels, extensions=raw_training_input)
        tot_fit_time += fit_time
        print fit_time, " fit time"

        if layer_i < n_layers - 1:  # No need to test the last layer before the very real test
            o, _ = computers[layer_i].test(layer_inputs, extensions=raw_training_input)
            percentiles = np.percentile(o, q)
            activation_levels.append(percentiles)
            print q, "percentiles (tr):", percentiles
            o = inter_process(o, len(training_sets), 9, d, percentiles)

            o = np.append(encoded_jaeger_training_sets, o, axis=2)
            # o = unflatten(o, sequence_len_training)

    o = None

    out_of = []
    misclassif = []

    for layer_i in xrange(n_layers):  # Testing

        raw_training_input = None  # testing_sets if layer_i == 0 else jaeger_testing_sets
        o, x = computers[layer_i].test(testing_sets if o is None else o, extensions=raw_training_input)
        print q, "percent., new:   ", np.percentile(o, q)

        n_correct, n_incorrect = correctness(o, flatten(
            subsequent_testing_labels if layer_i < n_layers - 1 else final_layer_testing_labels))
        out_of.append(n_correct + n_incorrect)
        misclassif.append(n_incorrect)

        # print "Misprecictions, percent: %d, %.1f" % (n_incorrect, (100.0 * n_correct / (n_correct + n_incorrect)))

        if layer_i < n_layers - 1:
            percentiles = activation_levels[layer_i]
            o = inter_process(o, len(testing_sets), 9, d, percentiles)

            # o = np.append(x, o, axis=2)

            o = np.append(encoded_jaeger_testing_sets, o, axis=2)
            # o = unflatten(o, sequence_len_testing)

            # sample_nr = 0
            # if layer_i < n_layers - 1:
            #     time_steps = sequence_len_testing[sample_nr]
            # else:
            #     time_steps = 1
            # plot_temporal(x,
            #               encoders[layer_i].n_random_mappings,
            #               encoders[layer_i].automaton_area,
            #               time_steps,
            #               n_iterations[layer_i] * (1 if layer_i < n_layers - 1 else d),
            #               sample_nr=sample_nr)
    result = [j for i in zip(out_of, misclassif) for j in i]
    print ["%.1f" % (100 * float(result[i]) / result[i - 1]) for i in xrange(1, len(result), 2)], "% error"
    if logit:
        rep_string = "\"%s\",\"%s\",\"%s\",%d,%d,%d,%s,%s,%d,%.2f,%d" + ",%d" * n_layers * 2
        logging.info(rep_string,
                     ','.join(str(e) for e in n_iterations),
                     ','.join(str(e) for e in n_random_mappings),
                     ','.join(str(e) for e in rules),
                     initial_input_size,
                     diffuse,
                     pad,
                     'True',
                     estimator.__class__.__name__,
                     d,
                     tot_fit_time,
                     misclassif[-1],
                     *result)
    return 0