Example #1
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)
Example #2
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
Example #3
0
def test_transformed_and_pointer_symbol(Simulator, algebra, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate("A; B")

    with spa.Network() as model:
        a = spa.Transcode("A", output_vocab=vocab)
        x = (a * PointerSymbol("B")) * PointerSymbol("~B")
        p = nengo.Probe(x.construct(), synapse=0.3)

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

    assert_sp_close(sim.trange(),
                    sim.data[p],
                    vocab.parse("A * B * ~B"),
                    skip=0.2,
                    normalized=True)
Example #4
0
def test_transformed(Simulator, algebra, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate('A; B')

    with spa.Network() as model:
        a = spa.Transcode('A', output_vocab=vocab)
        x = PointerSymbol('B') * a
        p = nengo.Probe(x.construct(), synapse=0.3)

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

    assert_sp_close(sim.trange(),
                    sim.data[p],
                    vocab.parse('B*A'),
                    skip=0.2,
                    normalized=True)
Example #5
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)
Example #6
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)
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)
Example #8
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,
    )
Example #9
0
def test_complex_rule(Simulator, algebra, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate('A; B; C; D')

    with spa.Network() as model:
        a = spa.Transcode('A', output_vocab=vocab)
        b = spa.Transcode('B', output_vocab=vocab)

        x = (0.5 * PointerSymbol('C') * a +
             0.5 * PointerSymbol('D')) * (0.5 * b + a * 0.5)
        p = nengo.Probe(x.construct(), synapse=0.3)

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

    assert_sp_close(
        sim.trange(),
        sim.data[p],
        vocab.parse('(0.5 * C * A + 0.5 * D) * (0.5 * B + 0.5 * A)'),
        skip=0.2,
        normalized=True)
Example #10
0
def test_transformed_and_transformed(Simulator, algebra, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng, algebra=algebra)
    vocab.populate("A; B.unitary(); C")

    with spa.Network() as model:
        a = spa.Transcode("A", output_vocab=vocab)
        c = spa.Transcode("C", output_vocab=vocab)
        x = (PointerSymbol("B") * a) * (PointerSymbol("~B") * c)
        p = nengo.Probe(x.construct(), synapse=0.3)

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

    assert_sp_close(
        sim.trange(),
        sim.data[p],
        vocab.parse("(B * A) * (~B * C)"),
        skip=0.2,
        normalized=True,
        atol=0.3,
    )
Example #11
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)
Example #12
0
def test_action_selection(Simulator, rng):
    vocab = spa.Vocabulary(64)
    vocab.populate("A; B; C; D; E; F")

    with spa.Network() as model:
        state = spa.Transcode(lambda t: "ABCDEF"[min(5, int(t / 0.5))],
                              output_vocab=vocab)
        scalar = spa.Scalar()
        pointer = spa.State(vocab)
        with ActionSelection():
            spa.ifmax(spa.dot(state, PointerSymbol("A")), 0.5 >> scalar)
            spa.ifmax(spa.dot(state, PointerSymbol("B")),
                      PointerSymbol("B") >> pointer)
            spa.ifmax(spa.dot(state, PointerSymbol("C")), state >> pointer)
            d_utility = spa.ifmax(0, PointerSymbol("D") >> pointer)
            spa.ifmax(
                spa.dot(state, PointerSymbol("E")),
                0.25 >> scalar,
                PointerSymbol("E") >> pointer,
            )
        nengo.Connection(nengo.Node(lambda t: 1.5 < t <= 2.0), d_utility)
        p_scalar = nengo.Probe(scalar.output, synapse=0.03)
        p_pointer = nengo.Probe(pointer.output, synapse=0.03)

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

    t = sim.trange()
    assert_allclose(sim.data[p_scalar][(0.3 < t) & (t <= 0.5)], 0.5, atol=0.2)
    assert_sp_close(sim.trange(),
                    sim.data[p_pointer],
                    vocab["B"],
                    skip=0.8,
                    duration=0.2)
    assert_sp_close(sim.trange(),
                    sim.data[p_pointer],
                    vocab["C"],
                    skip=1.3,
                    duration=0.2)
    assert_sp_close(sim.trange(),
                    sim.data[p_pointer],
                    vocab["D"],
                    skip=1.8,
                    duration=0.2)
    assert_allclose(sim.data[p_scalar][(2.3 < t) & (t <= 2.5)], 0.25, atol=0.2)
    assert_sp_close(sim.trange(),
                    sim.data[p_pointer],
                    vocab["E"],
                    skip=2.3,
                    duration=0.2)
Example #13
0
def test_dot_with_fixed_matmul(Simulator, rng):
    vocab = spa.Vocabulary(16, pointer_gen=rng)
    vocab.populate("A; B")

    with spa.Network() as model:
        a = PointerSymbol("A")  # noqa: F841
        b = spa.Transcode(  # noqa: F841
            lambda t: "A" if t <= 0.5 else "B",
            output_vocab=vocab)
        x = eval("a @ b")
        p = nengo.Probe(x.construct(), synapse=0.03)

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

    t = sim.trange()
    assert_allclose(sim.data[p][(0.3 < t) & (t <= 0.5)], 1.0, atol=0.2)
    assert np.all(sim.data[p][0.8 < t] < 0.2)
Example #14
0
def test_binary_operation_on_fixed_pointer_with_pointer_symbol(
        Simulator, op, order, rng):
    vocab = spa.Vocabulary(64, pointer_gen=rng)
    vocab.populate('A; B')
    a = PointerSymbol('A', TVocabulary(vocab))  # noqa: F841
    b = SemanticPointer(vocab['B'].v)  # noqa: F841

    with spa.Network() as model:
        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.5)

    assert_sp_close(sim.trange(),
                    sim.data[p],
                    vocab.parse(order[0] + op + order[1]),
                    skip=0.3)
def test_binary_operation_on_fixed_pointer_with_pointer_symbol(
    Simulator, op, order, rng
):
    vocab = spa.Vocabulary(64, pointer_gen=rng)
    vocab.populate("A; B")
    a = PointerSymbol("A", TVocabulary(vocab))  # noqa: F841
    b = SemanticPointer(vocab["B"].v)  # noqa: F841

    with spa.Network() as model:
        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.5)

    assert_sp_close(
        sim.trange(), sim.data[p], vocab.parse(order[0] + op + order[1]), skip=0.3
    )
Example #16
0
def test_pointer_symbol_mul_with_array():
    with pytest.raises(TypeError):
        PointerSymbol("X") * np.array([1, 2])
Example #17
0
def test_pointer_symbol_with_dynamic_scalar(Simulator, rng):
    with spa.Network():
        scalar = spa.Scalar()
        with pytest.raises(SpaTypeError):
            PointerSymbol("A") * scalar