Question Memory exampleΒΆ


Nengo example: Simple question answering with memory

This demo implements a simple form of question answering. Two features (color and shape) will be bound by circular convolution and stored in a memory population. A cue will be used to determine either one of the features by deconvolution.

When you run the network, it will start by binding RED and CIRCLE for 0.25 seconds and then binding BLUE and SQUARE for 0.25 seconds. Both bound semantic pointers are stored in a memory population. Then the network is asked with the cue. For example, when the cue is CIRCLE the network will respond with RED.

In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline

import nengo
%load_ext nengo.ipynb
from nengo import spa
<IPython.core.display.Javascript at 0x7fe8e86c1110>

Create the model

In [2]:
# Number of dimensions for the Semantic Pointers
dimensions = 32

model = spa.SPA(label="Simple question answering")

with model:
    model.color_in = spa.Buffer(dimensions=dimensions)
    model.shape_in = spa.Buffer(dimensions=dimensions)
    model.conv = spa.Memory(
        dimensions=dimensions, subdimensions=4, synapse=0.4)
    model.cue = spa.Buffer(dimensions=dimensions)
    model.out = spa.Buffer(dimensions=dimensions)

    # Connect the buffers
    cortical_actions = spa.Actions('conv = color_in * shape_in',
                                   'out = conv * ~cue')
    model.cortical = spa.Cortical(cortical_actions)

Provide the input

The color input will RED and then BLUE for 0.25 seconds each before being turned off. In the same way the shape input will be CIRCLE and then SQUARE for 0.25 seconds each. Thus, the network will bind alternatingly RED * CIRCLE and BLUE * SQUARE for 0.5 seconds each.

The cue for deconvolving bound semantic pointers will be turned off for 0.5 seconds and then cycles through CIRCLE, RED, SQUARE, and BLUE within one second.

In [3]:
def color_input(t):
    if t < 0.25:
        return 'RED'
    elif t < 0.5:
        return 'BLUE'
        return '0'

def shape_input(t):
    if t < 0.25:
        return 'CIRCLE'
    elif t < 0.5:
        return 'SQUARE'
        return '0'

def cue_input(t):
    if t < 0.5:
        return '0'
    sequence = ['0', 'CIRCLE', 'RED', '0', 'SQUARE', 'BLUE']
    idx = int(((t - 0.5) // (1. / len(sequence))) % len(sequence))
    return sequence[idx]

with model:
    model.inp = spa.Input(
        color_in=color_input, shape_in=shape_input, cue=cue_input)

Probe the output

In [4]:
with model:
    model.config[nengo.Probe].synapse = nengo.Lowpass(0.03)
    color_in = nengo.Probe(model.color_in.state.output)
    shape_in = nengo.Probe(model.shape_in.state.output)
    cue = nengo.Probe(model.cue.state.output)
    conv = nengo.Probe(model.conv.state.output)
    out = nengo.Probe(model.out.state.output)

Run the model

In [5]:
with nengo.Simulator(model) as sim:

Plot the results

In [6]:
plt.figure(figsize=(10, 10))
vocab = model.get_default_vocab(dimensions)

plt.subplot(5, 1, 1)
plt.plot(sim.trange(), model.similarity(, color_in))
plt.legend(model.get_output_vocab('color_in').keys, fontsize='x-small')

plt.subplot(5, 1, 2)
plt.plot(sim.trange(), model.similarity(, shape_in))
plt.legend(model.get_output_vocab('shape_in').keys, fontsize='x-small')

plt.subplot(5, 1, 3)
plt.plot(sim.trange(), model.similarity(, cue))
plt.legend(model.get_output_vocab('cue').keys, fontsize='x-small')

plt.subplot(5, 1, 4)
for pointer in ['RED * CIRCLE', 'BLUE * SQUARE']:

plt.subplot(5, 1, 5)
plt.plot(sim.trange(), spa.similarity([out], vocab))
plt.legend(model.get_output_vocab('out').keys, fontsize='x-small')
plt.xlabel("time [s]")
<matplotlib.text.Text at 0x7fe8abbf0690>

The plots of shape, color, and convolved show that first RED * CIRCLE and then BLUE * SQUARE will be loaded into the convolved buffer so after 0.5 seconds it represents the superposition RED * CIRCLE + BLUE * SQUARE.

The last plot shows that the output is most similar to the semantic pointer bound to the current cue. For example, when RED and CIRCLE are being convolved and the cue is CIRCLE, the output is most similar to RED. Thus, it is possible to unbind semantic pointers from the superposition stored in convolved.

Download question_memory as an IPython notebook or Python script.