Ejemplo n.º 1
0
    def __init__(
            self, stim_provider, items, contexts, n_pos, rng=None,
            extra_pos=3):
        super(Vocabularies, self).__init__()

        vocabs = Config.default(spa.Network, 'vocabs')
        if vocabs is None:
            vocabs = VocabularyMap(rng=rng)
        self.vocabs = vocabs

        self.items = items
        if is_integer(contexts):
            contexts = spa.Vocabulary(contexts, rng=rng)
        self.contexts = contexts
        self.positions = spa.Vocabulary(self.items.dimensions, rng=rng)

        self.items.populate(';'.join(stim_provider.get_all_items()))
        if stim_provider.n_distractors_per_epoch > 0:
            self.items.populate(';'.join(stim_provider.get_all_distractors()))

        for i in range(self.items.dimensions):
            self.contexts.populate('CTX' + str(i))

        for i in range(n_pos + extra_pos):
            self.positions.populate('P' + str(i))
Ejemplo n.º 2
0
def test_reinterpret(rng):
    v1 = spa.Vocabulary(16, pointer_gen=rng)
    v1.populate("A; B")
    v2 = spa.Vocabulary(16, pointer_gen=rng)
    v2.populate("A; B")

    assert_equal(
        spa.reinterpret(PointerSymbol("A", TVocabulary(v1)), v2).evaluate().v,
        v1["A"].v)
Ejemplo n.º 3
0
def test_no_magic_vocab_transform():
    d = 16
    v1 = spa.Vocabulary(d)
    v2 = spa.Vocabulary(d)

    with spa.Network() as model:
        model.a = spa.State(vocab=v1)
        model.b = spa.State(vocab=v2)
        with pytest.raises(SpaTypeError):
            model.a >> model.b
def test_translate(rng):
    v1 = spa.Vocabulary(16, pointer_gen=rng)
    v1.populate('A; B')
    v2 = spa.Vocabulary(16, pointer_gen=rng)
    v2.populate('A; B')

    assert_allclose(spa.translate(PointerSymbol('A', TVocabulary(v1)),
                                  v2).evaluate().dot(v2['A']),
                    1.,
                    atol=0.2)
Ejemplo n.º 5
0
def test_translate(rng):
    v1 = spa.Vocabulary(16, pointer_gen=rng)
    v1.populate("A; B")
    v2 = spa.Vocabulary(16, pointer_gen=rng)
    v2.populate("A; B")

    assert_allclose(
        spa.translate(PointerSymbol("A", TVocabulary(v1)),
                      v2).evaluate().dot(v2["A"]),
        1.0,
        atol=0.2,
    )
Ejemplo n.º 6
0
def test_dynamic_translate(Simulator, rng):
    v1 = spa.Vocabulary(64, pointer_gen=rng)
    v1.populate("A; B")
    v2 = spa.Vocabulary(64, pointer_gen=rng)
    v2.populate("A; B")

    with spa.Network() as model:
        source = spa.Transcode("A", output_vocab=v1)
        x = spa.translate(source, v2)
        p = nengo.Probe(x.construct(), synapse=0.03)

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

    assert_sp_close(sim.trange(), sim.data[p], v2["A"], skip=0.3, atol=0.2)
Ejemplo n.º 7
0
def test_missing_pointer():
    vocab = spa.Vocabulary(16)
    with spa.Network():
        a = spa.State(vocab)
        assert a
        with pytest.raises(SpaParseError):
            spa.sym.A >> a
def create_random_vocab(dim,
                        num_vector_items,
                        seed,
                        b_unitary=False,
                        spatial_ids=['X', 'Y']):
    """create a random spa vocab with a certain number of vectors

    :dim: dimension of the vectors
    :num_vector_items: number of vectors in the Vocabulary
    :seed: random number seed
    :b_unitary: bool indication if the created vector shall be unitary
    :spatial_ids: list of ids for the spatial vectors, which shall be unitary
    :returns: an instance of a spa vocabulary

    """

    vocab = spa.Vocabulary(dimensions=dim,
                           pointer_gen=np.random.RandomState(seed=seed))

    for k in spatial_ids:
        vocab.populate(k + '.unitary()')

    for i in np.arange(num_vector_items):
        k = 'C%s' % str(i).zfill(len(str(num_vector_items)))

        if b_unitary:
            vocab.populate(k + '.unitary()')
        else:
            vocab.populate(k)

    return vocab
Ejemplo n.º 9
0
def test_run(Simulator, algebra, seed):
    rng = np.random.RandomState(seed)
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate('A; B')

    with spa.Network(seed=seed) as model:
        model.bind = spa.Bind(vocab)

        def inputA(t):
            if 0 <= t < 0.1:
                return 'A'
            else:
                return 'B'

        model.input = spa.Transcode(inputA, output_vocab=vocab)
        model.input >> model.bind.input_left
        spa.sym.A >> model.bind.input_right

    with model:
        p = nengo.Probe(model.bind.output, synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.2)

    error = rmse(vocab.parse("(B*A).normalized()").v, sim.data[p][-1])
    assert error < 0.15

    error = rmse(vocab.parse("(A*A).normalized()").v, sim.data[p][100])
    assert error < 0.15
Ejemplo n.º 10
0
def create_vocab(D, keys, rng=None):
	vocab = spa.Vocabulary(int(D), pointer_gen=rng, max_similarity=0)
	if type(keys) is list:
		vocab.populate(';'.join(keys))
	else:
		vocab.populate(keys)
	return vocab
def test_run(Simulator, algebra, seed):
    rng = np.random.RandomState(seed)
    vocab = spa.Vocabulary(32, pointer_gen=rng, algebra=algebra)
    vocab.populate('A; B')

    with spa.Network(seed=seed, vocabs=VocabularyMap([vocab])) as model:
        model.superpos = spa.Superposition(2, vocab=32)

        def inputA(t):
            if 0 <= t < 0.1:
                return 'A'
            else:
                return 'B'

        model.input = spa.Transcode(inputA, output_vocab=vocab)
        model.input >> model.superpos.inputs[0]
        spa.sym.A >> model.superpos.inputs[1]

    with model:
        p = nengo.Probe(model.superpos.output, synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.2)

    error = rmse(vocab.parse("(B+A).normalized()").v, sim.data[p][-1])
    assert error < 0.1

    error = rmse(vocab.parse("(A+A).normalized()").v, sim.data[p][100])
    assert error < 0.2
Ejemplo n.º 12
0
def test_unbind(Simulator, side, seed):
    rng = np.random.RandomState(seed)
    vocab = spa.Vocabulary(36, pointer_gen=rng, algebra=VtbAlgebra())
    vocab.populate("A; B")

    with spa.Network(seed=seed) as model:
        vtb = VTB(100,
                  36,
                  unbind_left=(side == "left"),
                  unbind_right=(side == "right"))

        if side == "left":
            left = nengo.Node(vocab["B"].v)
            right = nengo.Node(vocab.parse("B*A").v)
        elif side == "right":
            left = nengo.Node(vocab.parse("A*B").v)
            right = nengo.Node(vocab["B"].v)
        else:
            raise ValueError("Invalid 'side' value.")

        nengo.Connection(left, vtb.input_left)
        nengo.Connection(right, vtb.input_right)

        p = nengo.Probe(vtb.output, synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.2)

    assert_sp_close(sim.trange(),
                    sim.data[p],
                    vocab.parse("A * B * ~B"),
                    skip=0.15,
                    atol=0.3)
Ejemplo n.º 13
0
def test_unbind(Simulator, algebra, side, seed):
    rng = np.random.RandomState(seed)
    vocab = spa.Vocabulary(64, pointer_gen=rng, algebra=algebra)
    vocab.populate('A; B')

    with spa.Network(seed=seed) as model:
        model.bind = spa.Bind(vocab,
                              unbind_left=(side == 'left'),
                              unbind_right=(side == 'right'))

        if side == 'left':
            spa.sym.B >> model.bind.input_left
            spa.sym.B * spa.sym.A >> model.bind.input_right
        elif side == 'right':
            spa.sym.A * spa.sym.B >> model.bind.input_left
            spa.sym.B >> model.bind.input_right
        else:
            raise ValueError("Invalid 'side' value.")

    with model:
        p = nengo.Probe(model.bind.output, synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.2)

    assert_sp_close(sim.trange(),
                    sim.data[p],
                    vocab.parse('A * B * ~B'),
                    skip=0.15,
                    atol=0.3)
Ejemplo n.º 14
0
def test_fixed_dot_matmul(rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A; B")

    v = TVocabulary(vocab)  # noqa: F841
    assert_allclose(
        eval("PointerSymbol('A', v) @ PointerSymbol('A', v)").evaluate(), 1.0)
Ejemplo n.º 15
0
def test_additive_op_fixed_scalar_and_pointer_symbol(op, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A")

    with spa.Network():
        with pytest.raises(TypeError):
            eval("2" + op + "PointerSymbol('A')")
Ejemplo n.º 16
0
def test_binary_operation_on_modules_with_fixed_pointer(
        Simulator, algebra, op, order, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate("A; B")
    b = SemanticPointer(vocab["B"].v)  # noqa: F841

    with spa.Network() as model:
        a = spa.Transcode("A", output_vocab=vocab)  # noqa: F841
        if order == "AB":
            x = eval("a" + op + "b")
        elif order == "BA":
            x = eval("b" + op + "a")
        else:
            raise ValueError("Invalid order argument.")
        p = nengo.Probe(x.construct(), synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.3)

    assert_sp_close(
        sim.trange(),
        sim.data[p],
        vocab.parse(order[0] + op + order[1]),
        skip=0.2,
        atol=0.3,
    )
Ejemplo n.º 17
0
def build_actions_map(paths, base_vocab):
    '''Built key, value vocabs for matching obj, goal combos to actions'''

    action_key_vocab = spa.Vocabulary(dim)
    actions = []

    for idx, path in enumerate(paths):

        goal_sum = 'GOAL_' + str(idx) + '_SUM'

        for x in range(len(path) - 1):
            immediate_goal = path[x]
            action = 'ADD_' + immediate_goal

            sp = base_vocab['OBJECT'] * base_vocab[goal_sum] + \
                 base_vocab['GOAL'] * base_vocab[immediate_goal]

            actions.append(action)
            action_key_vocab.add(action + '_KEY', sp.v)

        # add (main goal to last action) mapping
        action = 'ADD_' + path[-1]
        sp = base_vocab['OBJECT'] * base_vocab[goal_sum] + \
             base_vocab['GOAL'] * base_vocab['GOAL_' + str(idx)]

        actions.append(action)
        action_key_vocab.add(action + '_KEY', sp.v)

    action_val_vocab = base_vocab.create_subset(actions)

    return action_key_vocab, action_val_vocab
Ejemplo n.º 18
0
def make_good_unitary_old(D, eps=np.pi * 1e-3, n_trials=10000):
    for _ in range(n_trials):
        d = spa.Vocabulary(D)
        sp = d.create_pointer().unitary()
        a = np.angle(np.fft.fft(sp.v))
        if np.all(np.abs(a) > eps):
            return spa.SemanticPointer(sp.v)
    raise RuntimeError("bleh")
Ejemplo n.º 19
0
def test_unary_operation_on_pointer_symbol(op, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A")

    with spa.Network():
        x = eval(op + "PointerSymbol('A', TVocabulary(vocab))")
        node = x.construct()
    assert_equal(node.output, vocab.parse(op + "A").v)
Ejemplo n.º 20
0
def test_multiply_fixed_scalar_and_pointer_symbol(rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A")

    with spa.Network():
        x = 2 * PointerSymbol("A", TVocabulary(vocab))
        node = x.construct()
    assert_equal(node.output, vocab.parse("2 * A").v)
Ejemplo n.º 21
0
def test_pointer_symbol_network_creation(rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A")

    with spa.Network():
        A = PointerSymbol("A", TVocabulary(vocab))
        node = A.construct()
    assert_equal(node.output, vocab["A"].v)
Ejemplo n.º 22
0
def test_fixed_dot(rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A; B")

    v = TVocabulary(vocab)
    assert_allclose(
        spa.dot(PointerSymbol("A", v), PointerSymbol("A", v)).evaluate(), 1.0)
    assert spa.dot(PointerSymbol("A", v), PointerSymbol("B",
                                                        v)).evaluate() <= 0.1
Ejemplo n.º 23
0
def test_binary_operation_on_pointer_symbols(op, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A; B")

    with spa.Network():
        v = TVocabulary(vocab)  # noqa: F841
        x = eval("PointerSymbol('A', v)" + op + "PointerSymbol('B', v)")
        node = x.construct()
    assert_equal(node.output, vocab.parse("A" + op + "B").v)
Ejemplo n.º 24
0
    def __init__( self, seed=1, label="KerNengo", cats=None ):
        """
        build the nengo network
        there will be as many input and Keras node objects as the categories to be searched
        in the image. The categories can be given in a list as cats argoments, otherwise the
        global categories is used
        """
        self.probes = {}
        self.nodes  = {}
        self.states = {}
        self.prob   = {}
        self.v_dim  = list(lg.n_coords.values())[ 0 ][ 0 ]
        self.voc    = spa.Vocabulary( self.v_dim )
        self.dl     = nengo.Network( seed=seed, label='DL'+label )
        self.spa    = spa.Network( seed=seed+1, label='SPA'+label )

        if cats is None:
            self.categories = lg.categories
        else:
            self.categories = cats

        # initialize probabilities
        for c in self.categories:
            self.prob[ c ]  = numpy.zeros( ( self.v_dim, ) )

        """
        this is the part intended for using a vocabolary, but I have found no ways to use it
        (see NOTE in state())
        n_cat       = len( self.categories )
        parse_str   = (n_cat - 1 ) * "WHERE{}; " + "WHERE{}" 
        spointers   = parse_str.format( *self.categories )
        self.voc.populate( spointers )
        """

        with self.dl:
            net = 'dl'
            for c in self.categories:
                inp     = "input_" + c
                self.inode( c, net=net )
                self.knode( c )
                nengo.Connection( self.get_node( inp, net=net ), self.get_node( c, net=net ), synapse=None )
                self.probe( self.get_node( c, net=net ), c, net=net )

        with self.spa:
            net = 'spa'
            cfg = nengo.Config( nengo.Ensemble )
            cfg[ nengo.Ensemble ].neuron_type   = nengo.Direct()
            with cfg:
                for c in self.categories:
                    inp     = "input_" + c
                    self.inode( c, net=net )
                    where   = c + "_where"
                    self.state( c )
                    nengo.Connection( self.get_node( inp, net=net ), self.states[ c ].input )
                    self.probe( self.states[ c ].output, where, net=net )
Ejemplo n.º 25
0
def test_spa_vocab():
    # create a model without a vocab and check that it is empty
    model = spa.Network()
    assert len(model.vocabs) == 0

    # create a model with a vocab and check that it's filled
    va = spa.Vocabulary(16)
    va.populate("PANTS")
    vb = spa.Vocabulary(32)
    vb.populate("SHOES")
    model = spa.Network(vocabs=VocabularyMap([va, vb]))
    assert list(model.vocabs[16].keys()) == ["PANTS"]
    assert list(model.vocabs[32].keys()) == ["SHOES"]

    # warning on vocabs with duplicate dimensions
    vc = spa.Vocabulary(16)
    vc.populate("SOCKS")
    with pytest.warns(UserWarning):
        model = spa.Network(vocabs=VocabularyMap([va, vb, vc]))
    assert list(model.vocabs[16].keys()) == ["SOCKS"]
    assert list(model.vocabs[32].keys()) == ["SHOES"]
Ejemplo n.º 26
0
 def v_words(self):
     """
     create the main vocabulary with the names of the objects selected among
     CIFAR categories, and extract the subsets of objects to be combined in
     sentences
     """
     self.words = spa.Vocabulary(dimensions=n_class)
     vect = numpy.eye(n_class)
     for w, v in zip(classes, vect):
         self.words.add(w.upper(), v)
     self.word1 = self.words.create_subset(self.obj1)
     self.word2 = self.words.create_subset(self.obj2)
Ejemplo n.º 27
0
def test_unary_operation_on_module(Simulator, algebra, op, suffix, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate("A")

    with spa.Network() as model:
        stimulus = spa.Transcode("A", output_vocab=vocab)  # noqa: F841
        x = eval(op + "stimulus" + suffix)
        p = nengo.Probe(x.construct(), synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.3)

    assert_sp_close(sim.trange(), sim.data[p], vocab.parse(op + "A"), skip=0.2)
Ejemplo n.º 28
0
def create_vectors(objs, D, axis={'X', 'Y'}):
    init_dic = spa.Vocabulary(dimensions=D, max_similarity=0.01)
    vec_dic = {}

    for a in axis:
        ax = make_good_unitary(D)

        init_dic.add(a, ax)
        vec_dic[a] = ax

    # for item in vecs:
    #     init_dic.add(item, init_dic.create_pointer(unitary =True, attempts=5000))

    #using one dictionary for both to reduce similarity
    for item in objs:
        init_dic.add(item, init_dic.create_pointer(attempts=5000))

    obj_dic = spa.Vocabulary(dimensions=D, max_similarity=0.01)
    for item in objs:
        obj_dic.add(item, init_dic[item].v)

    return obj_dic, vec_dic
Ejemplo n.º 29
0
def test_assignment_of_pointer_symbol(Simulator, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A")

    with spa.Network() as model:
        sink = spa.State(vocab)
        PointerSymbol("A") >> sink
        p = nengo.Probe(sink.output, synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.5)

    assert_sp_close(sim.trange(), sim.data[p], vocab["A"], skip=0.3)
Ejemplo n.º 30
0
def run_trial(dim, n_objects, n_goals, n_steps):
    '''Collect accuracy measure for a trial using the method'''
    goal_keys = ['GOAL_' + str(x) for x in range(n_goals)]
    object_keys = ['OBJ_' + str(x) for x in range(n_objects)]
    action_keys = ['ADD_' + x for x in object_keys]
    tags = ['GOAL', 'OBJECT']

    paths = build_goal_paths(object_keys, n_goals, n_steps)

    # build all the vocabs for mapping between objects, actions, goals, precons
    base_vocab = spa.Vocabulary(dim)
    base_vocab.populate(';'.join(goal_keys + object_keys + action_keys + tags))

    action_vocab, precon_vocab = build_precons_map(paths, base_vocab,
                                                   goal_keys)
    goal_vocab, sums_vocab = build_objects_map(paths, base_vocab, goal_keys)
    action_key_vocab, action_val_vocab = build_actions_map(paths, base_vocab)

    total = 0
    correct = 0
    results = []

    for idx, goal in enumerate(goal_keys):
        print('Current Goal: %s' % goal)
        print('Target Sequence: ', paths[idx])
        plan = []
        current_goal = goal

        for step in range(n_steps):
            goal_tag = 'GOAL_' + str(idx) + '_SUM'
            action_key = base_vocab['OBJECT'] * base_vocab[goal_tag] + \
                         base_vocab['GOAL'] * base_vocab[current_goal]

            action, _ = associate(action_key, action_key_vocab,
                                  action_val_vocab)

            print('Planned action: % s' % action)
            plan.append(action)

            precon, _ = associate(base_vocab[action], action_vocab,
                                  precon_vocab)

            current_goal = precon

        plan = [x.strip('ADD_') for x in plan]
        if list(reversed(plan)) == paths[idx]:
            correct += 1
        total += 1
        print('')

    return correct / total