Creating a counter

Hi -

I’m trying to model the Wisconsin Card Sort Test. It seems as if it should be trivially easy, but I can’t work out a counter.

The model compares 2 vectors with a compare object, the output of which is sent to a node.

If the values are similar, let’s say output is > 0.8, the model is supposed to keep following that rule until it has done it correctly 10 times in a row. The problem with my code is that the node can’t capture the idea that separate steps have occurred.

My gut is that some variation on the delay_node example will work, but right now that is beyond me.

thanks

Howard C.

import nengo
import numpy as np
import nengo.spa as spa
import random

D = 16
model = spa.SPA()

with model:
   model.vocab = spa.Vocabulary(D)
   model.vocab.parse('CORRECT + INCORRECT + NUMBER + SHAPE + COLOR')
   model.pt_response = spa.State(D)
   model.tester_response = spa.State(D)
   
   
   def output_pt(t):
       if 0 < t < 1:
           return 'SHAPE'
       elif 1 < t < 2:
           return 'COLOR'
       elif 2 < t < 3:
           return 'NUMBER'
       elif 3 < t < 4:
           return 'NUMBER'
       elif 4 < t < 5:
           return 'NUMBER'
       elif 5 < t < 6:
           return 'COLOR'
       elif 6 < t < 7:
           return 'NUMBER'
       else:
            return '0'
       
       
   
   def correct_response(t):
       if 0 < t < 10:
           return 'NUMBER'
       elif 1 < t < 2:
           return 'COLOR'
       elif 2 < t < 3:
           return 'NUMBER'
       elif 3 < t < 4:
           return 'NUMBER'
       else:
           return '0'
           
   model.input_pt = spa.Input(pt_response = output_pt)
   model.input_tester = spa.Input(tester_response = correct_response)
   
   model.Compare = spa.Compare(vocab = model.vocab, dimensions = D)
   nengo.Connection(model.pt_response.output, model.Compare.inputA, synapse = 0)
   nengo.Connection(model.tester_response.output, model.Compare.inputB, synapse = 0)
   node_1 = nengo.Node(size_in = 1, size_out = 1)
   nengo.Connection(model.Compare.output, node_1)
   
   
   def counter(t,x,count = 0):
       if x < 0.8:
           count = 0
           return count
       elif x > 0.8 and count < 10:
           count = count + 1
           return count
       else:       
           
           count = 0
           return count
               
node_1.output = counter

If you want the function of the node to have a stateful value, you should use a class. Here’s a basic example of a counter that increments when above a threshold.

import nengo

import matplotlib.pyplot as plt

dt = 0.001

class Counter(object):

    def __init__(self):
        self.count = 0

    def state_func(self, t, x):
        if x > 0.8:
            self.count += 1
        
        return self.count


def in_func(t):
    if ((t - dt) % 1) > 0.5:
        return 1
    else:
        return 0

counter = Counter()

with nengo.Network() as model:
    in_nd = nengo.Node(in_func, size_out=1)
    state_nd = nengo.Node(counter.state_func, size_in=1, size_out=1)
    
    nengo.Connection(in_nd, state_nd, synapse=None)

    p_in = nengo.Probe(in_nd)
    p_state = nengo.Probe(state_nd)

with nengo.Simulator(model) as sim:
    sim.run(3.0)

plt.plot(sim.data[p_in])
plt.show()

plt.plot(sim.data[p_state])
plt.show()

To complete your use case, you’re going to need a bit more complex logic in the state_func.

Thank you

Howard C