Example #1
0
    def reinforcements(stimulus, actions):
        """
        Compute ABTask reinforcements.

        Returns a reward of 1 if the stimulus prompt and action match, -1 if 
        there is a mismatch, and 0 if the standby action is taken.
        """

        actions = nd.keep(actions,
                          keys={
                              feature("respond", "A"),
                              feature("respond", "B"),
                              feature("respond", "standby")
                          })

        stimulus = nd.keep(stimulus, keys={feature("A"), feature("B")})
        correct = nd.MutableNumDict({feature("respond", "standby"): 0})
        _correct = nd.transform_keys(
            stimulus,
            func=lambda s: feature("respond", s.tag),
        )
        correct.update(2 * _correct - 1)

        r = nd.sum_by(correct * actions,
                      keyfunc=lambda a: feature(("r", "respond")))

        return r
Example #2
0
# For this example, we will create a linear-regression-like setup, with an
# input x, an output y, and parameters w.

# We will be learning the relationship:
# y = m * x + b

m, b = 3.0, -1.0

batch_size = 20
epochs = 10
lr = 0.5

batches = [[gauss(0, 1) for _ in range(batch_size)] for _ in range(epochs)]

x = nd.MutableNumDict({1: 0.0, 2: 1.0})
y = nd.MutableNumDict({3: 0.0})

w = nd.MutableNumDict({1: gauss(0, 1), 2: gauss(0, 1)})

# To get a persistent gradient tape, we simply pass in True as the value for
# the `persistent` parameter to the tape constructor.

with nd.GradientTape(persistent=True) as tape:
    y_hat = nd.sum_by(x * w, keyfunc=lambda x: 3)
    cost = (y - y_hat)**2 / 2

mse: List[float] = []
for i in range(epochs):

    grad_w = nd.MutableNumDict(default=0.0)
Example #3
0
    feature("word", "/banana/"),
    feature("word", "/apple/"),
    feature("word", "/orange/"),
    feature("word", "/plum/"),
))

# The working memory interface serves several purposes. It sets the number of
# available memory slots, defines commands for writing to and reading from each
# individual slot, and defines commands for globally resetting the working
# memory state.

wm_interface = RegisterArray.Interface(name="wm", slots=3, vops=("retrieve", ))

# We set up default action activations, as in `flow_control.py`.

default_strengths = nd.MutableNumDict(default=0)
default_strengths.extend(wm_interface.defaults,
                         speech_interface.defaults,
                         value=0.5)

# As in `chunk_extraction.py`, we construct a chunk database to store chunks.

nacs_cdb = Chunks()

# We then manually populate the database with chunks representing the fruits
# that alice discovered in `chunk_extraction.py`.

nacs_cdb.define(chunk("APPLE"), feature("word", "/apple/"),
                feature("color", "red"), feature("shape", "round"),
                feature("size", "medium"), feature("texture", "smooth"))