def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For spiking networks
    config.output_nodes = 2

    pop = population.Population(config)
    pop.epoch(eval_fitness, 500)

    winner = pop.most_fit_genomes[-1]
    print 'Number of evaluations: %d' % winner.ID

    # Verify network output against training data.
    print '\nBest network output:'
    net = iznn.create_phenotype(winner)
    for inputData, outputData in zip(INPUTS, OUTPUTS):
        for j in range(MAX_TIME):
            output = net.advance([x * 10 for x in inputData])
            if output != [False, False]:
                break
        if output[0] and not output[1]:  # Network answered 1
            print "%r expected %d got 1" % (inputData, outputData)
        elif not output[0] and output[1]:  # Network answered 0
            print "%r expected %d got 0" % (inputData, outputData)
        else:
            print "%r expected %d got ?" % (inputData, outputData)

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores)
    visualize.plot_species(pop.species_log)
    visualize.draw_net(winner, view=True)
Beispiel #2
0
def check_add_connection(genome_type, feed_forward):
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))
    config.input_nodes = 3
    config.output_nodes = 4
    config.hidden_nodes = 5
    config.feedforward = feed_forward
    N = config.input_nodes + config.hidden_nodes + config.output_nodes

    connections = {}
    for a in range(100):
        g = genome_type.create_unconnected(a, config)
        g.add_hidden_nodes(config.hidden_nodes)
        for b in range(1000):
            g.mutate_add_connection()
        for c in g.conn_genes.values():
            connections[c.key] = connections.get(c.key, 0) + 1

    # TODO: The connections should be returned to the caller and checked
    # against the constraints/assumptions particular to the network type.
    for i in range(N):
        values = []
        for j in range(N):
            values.append(connections.get((i, j), 0))
        print("{0:2d}: {1}".format(i, " ".join("{0:3d}".format(x) for x in values)))
Beispiel #3
0
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 200)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Display the winning genome.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))

    # Show output of the most fit genome against training data.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    dt = iz_params[-1]
    for inputData, outputData in zip(xor_inputs, xor_outputs):
        neuron_data = {}
        for i, n in net.neurons.items():
            neuron_data[i] = []

        # Reset the network, apply the XOR inputs, and run for the maximum allowed time.
        net.reset()
        net.set_inputs(inputData)
        t0 = None
        t1 = None
        v0 = None
        v1 = None
        num_steps = int(max_time / dt)
        for j in range(num_steps):
            t = dt * j
            output = net.advance()

            # Capture the time and neuron membrane potential for later use if desired.
            for i, n in net.neurons.items():
                neuron_data[i].append((t, n.v))

            # Remember time and value of the first output spikes from each neuron.
            if t0 is None and output[0] > 0:
                t0, v0 = neuron_data[net.outputs[0]][-2]

            if t1 is None and output[1] > 0:
                t1, v1 = neuron_data[net.outputs[1]][-2]

        response = compute_output(t0, t1)
        print(inputData, response)
Beispiel #4
0
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 200)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()
    node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'}
    visualize.draw_net(winner, view=True, node_names=node_names)
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    sum_square_error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12, 12))
    for r, (inputData, outputData, t0, t1, v0, v1, neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("{0!r} expected {1:.3f} got {2:.3f}".format(inputData, outputData, response))

        axes = plt.subplot(4, 1, r + 1)
        plt.title("Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData), fontsize=12)
        for i, s in neuron_data.items():
            if i in net.outputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron {0:d}".format(i))

        # Circle the first peak of each output.
        circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False)
        circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False)
        axes.add_artist(circle0)
        axes.add_artist(circle1)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 200)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Display the winning genome.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))

    # Show output of the most fit genome against training data.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nBest network output:')

    # Create a network of "fast spiking" Izhikevich neurons, simulation time step 0.25 millisecond.
    net = iznn.create_phenotype(winner, **neuron_params)
    for inputData, outputData in zip(xor_inputs, xor_outputs):
        neuron_data = {}
        for i, n in net.neurons.items():
            neuron_data[i] = []

        # Reset the network, apply the XOR inputs, and run for the maximum allowed time.
        net.reset()
        net.set_inputs(inputData)
        t0 = None
        t1 = None
        num_steps = int(max_time / dt)
        for j in range(num_steps):
            t = dt * j
            output = net.advance(dt)

            # Capture the time and neuron membrane potential for later use if desired.
            for i, n in net.neurons.items():
                neuron_data[i].append((t, n.v))

            # Remember time and value of the first output spikes from each neuron.
            if t0 is None and output[0] > 0:
                t0, v0 = neuron_data[net.outputs[0]][-2]

            if t1 is None and output[1] > 0:
                t1, v1 = neuron_data[net.outputs[1]][-2]

        response = compute_output(t0, t1)
        print(inputData, response)
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.epoch(eval_fitness, 200)

    winner = pop.most_fit_genomes[-1]
    print('Number of evaluations: %d' % winner.ID)

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores)
    visualize.plot_species(pop.species_log)
    visualize.draw_net(winner, view=True)

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12,12))
    for r, (inputData, outputData, t0, t1, neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("%r expected %.3f got %.3f" % (inputData, outputData, response))

        plt.subplot(4, 1, r + 1)
        plt.title("Traces for XOR input {%.1f, %.1f}" % inputData, fontsize=12)
        for i, s in neuron_data.items():
            if i not in net.inputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron %d" % i)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(axis='both', labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()
Beispiel #7
0
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 200)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()
    node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'}
    visualize.draw_net(winner, view=True, node_names=node_names)
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    sum_square_error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12, 12))
    for r, (inputData, outputData, t0, t1, v0, v1,
            neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("{0!r} expected {1:.3f} got {2:.3f}".format(
            inputData, outputData, response))

        axes = plt.subplot(4, 1, r + 1)
        plt.title(
            "Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData),
            fontsize=12)
        for i, s in neuron_data.items():
            if i in net.outputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron {0:d}".format(i))

        # Circle the first peak of each output.
        circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False)
        circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False)
        axes.add_artist(circle0)
        axes.add_artist(circle1)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()
def test_iznn_evolve():
    """This is a stripped-down copy of the XOR2 spiking example."""

    # Network inputs and expected outputs.
    xor_inputs = ((0, 0), (0, 1), (1, 0), (1, 1))
    xor_outputs = (0, 1, 1, 0)

    # Maximum amount of simulated time (in milliseconds) to wait for the network to produce an output.
    max_time = 50.0

    def compute_output(t0, t1):
        '''Compute the network's output based on the "time to first spike" of the two output neurons.'''
        if t0 is None or t1 is None:
            # If one of the output neurons failed to fire within the allotted time,
            # give a response which produces a large error.
            return -1.0
        else:
            # If the output neurons fire within 1.0 milliseconds of each other,
            # the output is 1, and if they fire more than 11 milliseconds apart,
            # the output is 0, with linear interpolation between 1 and 11 milliseconds.
            response = 1.1 - 0.1 * abs(t0 - t1)
            return max(0.0, min(1.0, response))

    def simulate(genome):
        # Create a network of Izhikevich neurons based on the given genome.
        net = iznn.create_phenotype(genome, **iznn.THALAMO_CORTICAL_PARAMS)
        dt = 0.25
        sum_square_error = 0.0
        simulated = []
        for inputData, outputData in zip(xor_inputs, xor_outputs):
            neuron_data = {}
            for i, n in net.neurons.items():
                neuron_data[i] = []

            # Reset the network, apply the XOR inputs, and run for the maximum allowed time.
            net.reset()
            net.set_inputs(inputData)
            t0 = None
            t1 = None
            v0 = None
            v1 = None
            num_steps = int(max_time / dt)
            for j in range(num_steps):
                t = dt * j
                output = net.advance(dt)

                # Capture the time and neuron membrane potential for later use if desired.
                for i, n in net.neurons.items():
                    neuron_data[i].append((t, n.v))

                # Remember time and value of the first output spikes from each neuron.
                if t0 is None and output[0] > 0:
                    t0, v0 = neuron_data[net.outputs[0]][-2]

                if t1 is None and output[1] > 0:
                    t1, v1 = neuron_data[net.outputs[1]][-2]

            response = compute_output(t0, t1)
            sum_square_error += (response - outputData) ** 2

            simulated.append(
                (inputData, outputData, t0, t1, v0, v1, neuron_data))

        return sum_square_error, simulated

    def eval_fitness(genomes):
        for genome in genomes:
            sum_square_error, simulated = simulate(genome)
            genome.fitness = 1 - sum_square_error

    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    # TODO: This is a little hackish, but will a user ever want to do it?
    # If so, provide a convenience method on Config for it.
    for i, tc in enumerate(config.type_config['DefaultStagnation']):
        if tc[0] == 'species_fitness_func':
            config.type_config['DefaultStagnation'][i] = (tc[0], 'median')

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 10)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, **iznn.RESONATOR_PARAMS)
    sum_square_error, simulated = simulate(winner)

    repr(winner)
    str(winner)
    for g in winner.node_genes:
        repr(g)
        str(g)
    for g in winner.conn_genes:
        repr(g)
        str(g)
Beispiel #9
0
def test_iznn_evolve():
    """This is a stripped-down copy of the XOR2 spiking example."""

    # Network inputs and expected outputs.
    xor_inputs = ((0, 0), (0, 1), (1, 0), (1, 1))
    xor_outputs = (0, 1, 1, 0)

    # Maximum amount of simulated time (in milliseconds) to wait for the network to produce an output.
    max_time = 50.0

    def compute_output(t0, t1):
        '''Compute the network's output based on the "time to first spike" of the two output neurons.'''
        if t0 is None or t1 is None:
            # If one of the output neurons failed to fire within the allotted time,
            # give a response which produces a large error.
            return -1.0
        else:
            # If the output neurons fire within 1.0 milliseconds of each other,
            # the output is 1, and if they fire more than 11 milliseconds apart,
            # the output is 0, with linear interpolation between 1 and 11 milliseconds.
            response = 1.1 - 0.1 * abs(t0 - t1)
            return max(0.0, min(1.0, response))

    def simulate(genome):
        # Create a network of Izhikevich neurons based on the given genome.
        net = iznn.create_phenotype(genome, **iznn.THALAMO_CORTICAL_PARAMS)
        dt = 0.25
        sum_square_error = 0.0
        simulated = []
        for inputData, outputData in zip(xor_inputs, xor_outputs):
            neuron_data = {}
            for i, n in net.neurons.items():
                neuron_data[i] = []

            # Reset the network, apply the XOR inputs, and run for the maximum allowed time.
            net.reset()
            net.set_inputs(inputData)
            t0 = None
            t1 = None
            v0 = None
            v1 = None
            num_steps = int(max_time / dt)
            for j in range(num_steps):
                t = dt * j
                output = net.advance(dt)

                # Capture the time and neuron membrane potential for later use if desired.
                for i, n in net.neurons.items():
                    neuron_data[i].append((t, n.v))

                # Remember time and value of the first output spikes from each neuron.
                if t0 is None and output[0] > 0:
                    t0, v0 = neuron_data[net.outputs[0]][-2]

                if t1 is None and output[1] > 0:
                    t1, v1 = neuron_data[net.outputs[1]][-2]

            response = compute_output(t0, t1)
            sum_square_error += (response - outputData)**2

            simulated.append(
                (inputData, outputData, t0, t1, v0, v1, neuron_data))

        return sum_square_error, simulated

    def eval_fitness(genomes):
        for genome in genomes:
            sum_square_error, simulated = simulate(genome)
            genome.fitness = 1 - sum_square_error

    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    # TODO: This is a little hackish, but will a user ever want to do it?
    # If so, provide a convenience method on Config for it.
    for i, tc in enumerate(config.type_config['DefaultStagnation']):
        if tc[0] == 'species_fitness_func':
            config.type_config['DefaultStagnation'][i] = (tc[0], 'median')

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 10)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, **iznn.RESONATOR_PARAMS)
    sum_square_error, simulated = simulate(winner)

    repr(winner)
    str(winner)
    for g in winner.node_genes:
        repr(g)
        str(g)
    for g in winner.conn_genes:
        repr(g)
        str(g)