def test_mnist_pipeline(self):
        network = DiehlAndCook2015(n_inpt=784, n_neurons=400, exc=22.5, inh=17.5, dt=1.0, norm=78.4)
        environment = DatasetEnvironment(dataset=MNIST(path='../data/MNIST', download=True), train=True, intensity=0.25)
        pipeline = Pipeline(network=network, environment=environment, encoding=poisson, time=350)

        assert pipeline.network == network
        assert pipeline.env == environment
        assert pipeline.encoding == poisson
        assert pipeline.time == 350
        assert pipeline.history_length is None
network = DiehlAndCook2015(n_input=32 * 32 * 3,
                           n_neurons=100,
                           dt=1.0,
                           exc=22.5,
                           inh=17.5,
                           nu=[0, 1e-2],
                           norm=78.4)

# Specify dataset wrapper environment.
environment = DatasetEnvironment(dataset=CIFAR10(path='../../data/CIFAR10'),
                                 train=True)

# Build pipeline from components.
pipeline = Pipeline(network=network,
                    environment=environment,
                    encoding=poisson,
                    time=50,
                    plot_interval=1)

# Train the network.
labels = environment.labels
for i in range(60000):
    # Choose an output neuron to clamp to spiking behavior.
    c = choice(10, size=1, replace=False)
    c = 10 * labels[i].long() + Tensor(c).long()
    clamp = torch.zeros(pipeline.time, network.n_neurons, dtype=torch.uint8)
    clamp[:, c] = 1
    clamp_v = torch.zeros(pipeline.time, network.n_neurons, dtype=torch.float)
    clamp_v[:, c] = network.layers['Ae'].thresh + network.layers['Ae'].theta[
        c] + 10
network.add_connection(input_exc_conn, source='X', target='E')
network.add_connection(exc_readout_conn, source='E', target='R')

# Add all monitors to the network.
for layer in layers:
    network.add_monitor(spikes[layer], name='%s_spikes' % layer)
    
    if layer in voltages:
        network.add_monitor(voltages[layer], name='%s_voltages' % layer)

# Load SpaceInvaders environment.
environment = GymEnvironment('Asteroids-v0')
environment.reset()

pipeline = Pipeline(network, environment, encoding=bernoulli, time=1, history=5, delta=10, plot_interval=plot_interval,
                    print_interval=print_interval, render_interval=render_interval, action_function=select_multinomial,
                    output='R')

total = 0
rewards = []
avg_rewards = []
lengths = []
avg_lengths = []

i = 0
try:
    while i < n:
        pipeline.step()
        
        if pipeline.done:
            pipeline.reset_()
Exemple #4
0
                        norm=0.15 * middle.n)

# Add all layers and connections to the network.
network.add_layer(inpt, name='X')
network.add_layer(middle, name='Y')
network.add_layer(out, name='Z')
network.add_connection(inpt_middle, source='X', target='Y')
network.add_connection(middle_out, source='Y', target='Z')

# Load SpaceInvaders environment.
environment = GymEnvironment('SpaceInvaders-v0')
environment.reset()

# Build pipeline from specified components.
pipeline = Pipeline(network,
                    environment,
                    encoding=bernoulli,
                    feedback=select_multinomial,
                    output='Z',
                    time=1,
                    history_length=2,
                    delta=4,
                    plot_interval=100,
                    render_interval=5)

# Run environment simulation and network training.
while True:
    pipeline.step()
    if pipeline.done == True:
        pipeline._reset()
Exemple #5
0
spikes = {}
for layer in set(network.layers):
    spikes[layer] = Monitor(network.layers[layer], state_vars=['s'], time=time)
    network.add_monitor(spikes[layer], name='%s_spikes' % layer)

voltages = {}
for layer in set(network.layers) - {'X'}:
    voltages[layer] = Monitor(network.layers[layer],
                              state_vars=['v'],
                              time=time)
    network.add_monitor(voltages[layer], name='%s_voltages' % layer)

# Build pipeline from above-specified components.
pipeline = Pipeline(network=network,
                    environment=environment,
                    encoding=encoding,
                    time=time,
                    plot_interval=1)

# Neuron assignments and spike proportions.
assignments = -torch.ones_like(torch.Tensor(n_neurons))
proportions = torch.zeros_like(torch.Tensor(n_neurons, 10))
rates = torch.zeros_like(torch.Tensor(n_neurons, 10))

# Record spikes during the simulation.
spike_record = torch.zeros(update_interval, time, n_neurons)

# Get data labels.
labels = pipeline.env.labels

# Sequence of accuracy estimates.
Exemple #6
0
from bindsnet.encoding    import poisson
from bindsnet.pipeline    import Pipeline
from bindsnet.models      import DiehlAndCook2015
from bindsnet.environment import DatasetEnvironment

# Build Diehl & Cook 2015 network.
network = DiehlAndCook2015(n_inpt=784,
						   n_neurons=400,
						   exc=22.5,
						   inh=17.5,
						   dt=1.0,
						   norm=78.4)

# Specify dataset wrapper environment.
environment = DatasetEnvironment(dataset=MNIST(path='../../data/MNIST'),
								 train=True,
								 download=True,
								 intensity=0.25)

# Build pipeline from components.
pipeline = Pipeline(network=network,
					environment=environment,
					encoding=poisson,
					time=350,
				    plot_interval=1)

# Train the network.
for i in range(60000):    
	pipeline.step()
	network._reset()
Exemple #7
0
                       target="Hidden Layer")
network.add_connection(middle_out,
                       source="Hidden Layer",
                       target="Output Layer")

# Load SpaceInvaders environment.
environment = GymEnvironment("BreakoutDeterministic-v4")
environment.reset()

# Build pipeline from specified components.
pipeline = Pipeline(
    network,
    environment,
    encoding=bernoulli,
    action_function=select_softmax,
    output="Output Layer",
    time=100,
    history_length=1,
    delta=1,
    plot_interval=1,
    render_interval=1,
)

# Train agent for 100 episodes.
print("Training: ")
for i in range(100):
    pipeline.reset_()
    # initialize episode reward
    reward = 0
    while True:
        pipeline.step()
        reward += pipeline.reward
def main(seed=0, n_neurons=100, n_train=60000, n_test=10000, inhib=100, lr=0.01, lr_decay=1, time=350, dt=1,
         theta_plus=0.05, theta_decay=1e-7, progress_interval=10, update_interval=250, plot=False,
         train=True, gpu=False):

    assert n_train % update_interval == 0 and n_test % update_interval == 0, \
                            'No. examples must be divisible by update_interval'

    params = [
        seed, n_neurons, n_train, inhib, lr_decay, time, dt,
        theta_plus, theta_decay, progress_interval, update_interval
    ]

    model_name = '_'.join([str(x) for x in params])

    np.random.seed(seed)

    if gpu:
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
        torch.cuda.manual_seed_all(seed)
    else:
        torch.manual_seed(seed)

    n_examples = n_train if train else n_test
    n_classes = 10

    # Build network.
    if train:
        network = Network(dt=dt)

        input_layer = Input(n=784, traces=True, trace_tc=5e-2)
        network.add_layer(input_layer, name='X')

        output_layer = DiehlAndCookNodes(
            n=n_classes, rest=0, reset=1, thresh=1, decay=1e-2,
            theta_plus=theta_plus, theta_decay=theta_decay, traces=True, trace_tc=5e-2
        )
        network.add_layer(output_layer, name='Y')

        w = torch.rand(784, n_classes)
        input_connection = Connection(
            source=input_layer, target=output_layer, w=w,
            update_rule=MSTDPET, nu=lr, wmin=0, wmax=1,
            norm=78.4, tc_e_trace=0.1
        )
        network.add_connection(input_connection, source='X', target='Y')

    else:
        network = load(os.path.join(params_path, model_name + '.pt'))
        network.connections['X', 'Y'].update_rule = NoOp(
            connection=network.connections['X', 'Y'], nu=network.connections['X', 'Y'].nu
        )
        network.layers['Y'].theta_decay = torch.IntTensor([0])
        network.layers['Y'].theta_plus = torch.IntTensor([0])

    # Load MNIST data.
    environment = MNISTEnvironment(
        dataset=MNIST(root=data_path, download=True), train=train, time=time
    )

    # Create pipeline.
    pipeline = Pipeline(
        network=network, environment=environment, encoding=repeat,
        action_function=select_spiked, output='Y', reward_delay=None
    )

    spikes = {}
    for layer in set(network.layers):
        spikes[layer] = Monitor(network.layers[layer], state_vars=('s',), time=time)
        network.add_monitor(spikes[layer], name='%s_spikes' % layer)

    if train:
        network.add_monitor(Monitor(
                network.connections['X', 'Y'].update_rule, state_vars=('tc_e_trace',), time=time
            ), 'X_Y_e_trace')

    # Train the network.
    if train:
        print('\nBegin training.\n')
    else:
        print('\nBegin test.\n')

    spike_ims = None
    spike_axes = None
    weights_im = None
    elig_axes = None
    elig_ims = None

    start = t()
    for i in range(n_examples):
        if i % progress_interval == 0:
            print(f'Progress: {i} / {n_examples} ({t() - start:.4f} seconds)')
            start = t()

            if i > 0 and train:
                network.connections['X', 'Y'].update_rule.nu[1] *= lr_decay

        # Run the network on the input.
        # print("Example",i,"Results:")
        # for j in range(time):
        #     result = pipeline.env_step()
        #     pipeline.step(result,a_plus=1, a_minus=0)
        # print(result)
        for j in range(time):
            pipeline.train()

        if not train:
            _spikes = {layer: spikes[layer].get('s') for layer in spikes}

        if plot:
            _spikes = {layer: spikes[layer].get('s') for layer in spikes}
            w = network.connections['X', 'Y'].w
            square_weights = get_square_weights(w.view(784, n_classes), 4, 28)

            spike_ims, spike_axes = plot_spikes(_spikes, ims=spike_ims, axes=spike_axes)
            weights_im = plot_weights(square_weights, im=weights_im)
            elig_ims, elig_axes = plot_voltages(
                {'Y': network.monitors['X_Y_e_trace'].get('e_trace').view(-1, time)[1500:2000]},
                plot_type='line', ims=elig_ims, axes=elig_axes
            )

            plt.pause(1e-8)

        pipeline.reset_state_variables()  # Reset state variables.
        network.connections['X', 'Y'].update_rule.tc_e_trace = torch.zeros(784, n_classes)

    print(f'Progress: {n_examples} / {n_examples} ({t() - start:.4f} seconds)')

    if train:
        network.save(os.path.join(params_path, model_name + '.pt'))
        print('\nTraining complete.\n')
    else:
        print('\nTest complete.\n')
Exemple #9
0
    def test_gym_pipeline(self):
        # Build network.
        network = Network(dt=1.0)

        # Layers of neurons.
        inpt = Input(n=6552, traces=True)
        middle = LIFNodes(n=225, traces=True, thresh=-52.0 + torch.randn(225))
        out = LIFNodes(n=60, refrac=0, traces=True, thresh=-40.0)

        # Connections between layers.
        inpt_middle = Connection(source=inpt, target=middle, wmax=1e-2)
        middle_out = Connection(source=middle,
                                target=out,
                                wmax=0.5,
                                update_rule=m_stdp_et,
                                nu=2e-2,
                                norm=0.15 * middle.n)

        # Add all layers and connections to the network.
        network.add_layer(inpt, name='X')
        network.add_layer(middle, name='Y')
        network.add_layer(out, name='Z')
        network.add_connection(inpt_middle, source='X', target='Y')
        network.add_connection(middle_out, source='Y', target='Z')

        # Load SpaceInvaders environment.
        environment = GymEnvironment('SpaceInvaders-v0')
        environment.reset()

        # Build pipeline from specified components.
        for history_length in [3, 4, 5, 6]:
            for delta in [2, 3, 4]:
                p = Pipeline(network,
                             environment,
                             encoding=bernoulli,
                             action_function=select_multinomial,
                             output='Z',
                             time=1,
                             history_length=history_length,
                             delta=delta)

                assert p.action_function == select_multinomial
                assert p.history_length == history_length
                assert p.delta == delta

        # Checking assertion errors
        for time in [0, -1]:
            try:
                p = Pipeline(network,
                             environment,
                             encoding=bernoulli,
                             action_function=select_multinomial,
                             output='Z',
                             time=time,
                             history_length=2,
                             delta=4)
            except ValueError:
                pass

        for delta in [0, -1]:
            try:
                p = Pipeline(network,
                             environment,
                             encoding=bernoulli,
                             action_function=select_multinomial,
                             output='Z',
                             time=time,
                             history_length=2,
                             delta=delta)
            except ValueError:
                pass

        for output in ['K']:
            try:
                p = Pipeline(network,
                             environment,
                             encoding=bernoulli,
                             action_function=select_multinomial,
                             output=output,
                             time=time,
                             history_length=2,
                             delta=4)
            except ValueError:
                pass

        p = Pipeline(network,
                     environment,
                     encoding=bernoulli,
                     action_function=select_random,
                     output='Z',
                     time=1,
                     history_length=2,
                     delta=4,
                     save_interval=50,
                     render_interval=5)

        assert p.action_function == select_random
        assert p.encoding == bernoulli
        assert p.save_interval == 50
        assert p.render_interval == 5
        assert p.time == 1
Exemple #10
0
    def test_MNIST_pipeline(self):
        network = DiehlAndCook2015(n_inpt=784,
                                   n_neurons=400,
                                   exc=22.5,
                                   inh=17.5,
                                   dt=1.0,
                                   norm=78.4)

        environment = DatasetEnvironment(dataset=MNIST(path='../../data/MNIST',
                                                       download=True),
                                         train=True,
                                         intensity=0.25)

        p = Pipeline(network=network,
                     environment=environment,
                     encoding=poisson,
                     time=350)

        assert p.network == network
        assert p.env == environment
        assert p.encoding == poisson
        assert p.time == 350
        assert p.history_length is None

        def test_Gym_pipeline(self):
            # Build network.
            network = Network(dt=1.0)

            # Layers of neurons.
            inpt = Input(n=6552, traces=True)
            middle = LIFNodes(n=225,
                              traces=True,
                              thresh=-52.0 + torch.randn(225))
            out = LIFNodes(n=60, refrac=0, traces=True, thresh=-40.0)

            # Connections between layers.
            inpt_middle = Connection(source=inpt, target=middle, wmax=1e-2)
            middle_out = Connection(source=middle,
                                    target=out,
                                    wmax=0.5,
                                    update_rule=m_stdp_et,
                                    nu=2e-2,
                                    norm=0.15 * middle.n)

            # Add all layers and connections to the network.
            network.add_layer(inpt, name='X')
            network.add_layer(middle, name='Y')
            network.add_layer(out, name='Z')
            network.add_connection(inpt_middle, source='X', target='Y')
            network.add_connection(middle_out, source='Y', target='Z')

            # Load SpaceInvaders environment.
            environment = GymEnvironment('SpaceInvaders-v0')
            environment.reset()

            # Build pipeline from specified components.
            for history_length in [3, 4, 5, 6]:
                for delta in [2, 3, 4]:
                    p = Pipeline(network,
                                 environment,
                                 encoding=bernoulli,
                                 feedback=select_multinomial,
                                 output='Z',
                                 time=1,
                                 history_length=2,
                                 delta=4)

                    assert p.feedback == select_multinomial
                    assert p.history_length == history_length
                    assert p.delta == delta

            # Checking assertion errors
            for time in [0, -1]:
                try:
                    p = Pipeline(network,
                                 environment,
                                 encoding=bernoulli,
                                 feedback=select_multinomial,
                                 output='Z',
                                 time=time,
                                 history_length=2,
                                 delta=4)
                except Exception as es:
                    assert es == AssertionError

            for delta in [0, -1]:
                try:
                    p = Pipeline(network,
                                 environment,
                                 encoding=bernoulli,
                                 feedback=select_multinomial,
                                 output='Z',
                                 time=time,
                                 history_length=2,
                                 delta=delta)
                except Exception as es:
                    assert es == AssertionError

            for output in ['K']:
                try:
                    p = Pipeline(network,
                                 environment,
                                 encoding=bernoulli,
                                 feedback=select_multinomial,
                                 output=output,
                                 time=time,
                                 history_length=2,
                                 delta=4)
                except Exception as es:
                    assert es == AssertionError

            p = Pipeline(network,
                         environment,
                         encoding=bernoulli,
                         feedback=select_random,
                         output='Z',
                         time=1,
                         history_length=2,
                         delta=4,
                         save_interval=50,
                         render_interval=5)

            assert p.feedback == select_random
            assert p.encoding == bernoulli
            assert p.save_interval == 50
            assert p.render_interval == 5
            assert p.time == 1