Nengo Example: 2-Dimensional Representation

Ensembles of neurons represent information. In Nengo, we represent that information with real-valued vectors -- lists of numbers. In this example, we will represent a two-dimensional vector with a single ensemble of leaky integrate-and-fire neurons.


In [11]:
### do some setup before we start
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

Step 1: Create the model

Our model consists of a single ensemble, which we will call Neurons. It will represent a two-dimensional signal.


In [12]:
import nengo
model = nengo.Model('2D Representation')

# Our ensemble consists of 100 leaky integrate-and-fire neurons,
# and represents a 2-dimensional signal
model.make_ensemble('Neurons', nengo.LIF(100), dimensions=2)


Out[12]:
<nengo.objects.Ensemble at 0x10b62fc50>

Step 2: Provide input to the model

The signal that an ensemble represents varies over time. We will use a simple sine and cosine wave as examples of continuously changing signals.


In [13]:
# Create input nodes representing the sine and cosine
model.make_node('sin', output=np.sin)
model.make_node('cos', output=np.cos)

# Connect the input nodes to the ensemble

# The transform defines which dimension the input will project to
model.connect('sin', 'Neurons', transform=[[1], [0]])
model.connect('cos', 'Neurons', transform=[[0], [1]])


Out[13]:
<nengo.connections.SimpleConnection at 0x10b633510>

Step 3: Run the model


In [16]:
model.probe('sin')
model.probe('cos')
model.probe('Neurons', filter=0.02)  # Unfiltered neuronal output has high variance!

# Run the model for 5 seconds
model.run(5)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-7e46cf97135d> in <module>()
      1 model.probe('sin')
      2 model.probe('cos')
----> 3 model.probe('Neurons', filter=0.02)  # Unfiltered neuronal output has high variance!
      4 
      5 # Run the model for 5 seconds

/Users/Trevor/Code/nengo/nengo/model.pyc in probe(self, target, sample_every, filter)
    636         if type(obj) == Ensemble:
    637             obj_s = self.get_string(target)
--> 638             p = obj.probe(probe_type, sample_every, filter, self)
    639             self.probed[key] = p.probe
    640             return p

/Users/Trevor/Code/nengo/nengo/objects.py in probe(self, to_probe, dt_sample, filter, model)
    232         self.probes.append(p)
    233         if model is not None:
--> 234             model.add(p)
    235         return p
    236 

/Users/Trevor/Code/nengo/nengo/model.pyc in add(self, obj)
    302             raise ValueError("Something called " + obj.name + " already exists."
    303                              " Please choose a different name.")
--> 304         obj.add_to_model(self)
    305         if hasattr(obj, 'name'):
    306             self.objs[obj.name] = obj

/Users/Trevor/Code/nengo/nengo/probes.py in add_to_model(self, model)
     22 
     23     def add_to_model(self, model):
---> 24         model.add(self.signal)
     25         model.add(self.filter)
     26         model.add(self.transform)

/Users/Trevor/Code/nengo/nengo/model.pyc in add(self, obj)
    300         """
    301         if hasattr(obj, 'name') and self.objs.has_key(obj.name):
--> 302             raise ValueError("Something called " + obj.name + " already exists."
    303                              " Please choose a different name.")
    304         obj.add_to_model(self)

ValueError: Something called probe(Neurons.decoded_output) already exists. Please choose a different name.

In [17]:
# Plot the input signals
t = model.data[model.simtime]
plt.plot(t, model.data['sin'], label="Sine")
plt.plot(t, model.data['cos'], label="Cosine")
plt.legend()
plt.ylim(-1.5, 1.5)


Out[17]:
(-1.5, 1.5)

In [20]:
# Plot the decoded output of the ensemble
t = model.data[model.simtime]
plt.plot(t, model.data['sin'], label="Sine")
plt.plot(t, model.data['cos'], label="Cosine")
plt.plot(t, model.data['Neurons'], label="Decoded output")
plt.legend()


Out[20]:
<matplotlib.legend.Legend at 0x10baa5050>