Ejemplo n.º 1
0
def generate(data_folder, net=None, alpha=1000.0):
    beta = alpha / 4.0

    # generate the Function Space
    forces, _, _ = forcing_functions.load_folder(data_folder,
                                                 rhythmic=True,
                                                 alpha=alpha,
                                                 beta=beta)
    # make an array out of all the possible functions we want to represent
    force_space = np.vstack(forces)
    # use this array as our space to perform svd over
    fs = nengo.FunctionSpace(space=force_space, n_basis=10)

    # store the weights for each trajectory
    weights_x = []
    weights_y = []
    for ii in range(len(forces)):
        forces = force_space[ii * 2:ii * 2 + 2]
        # load up the forces to be output by the forcing function
        # calculate the corresponding weights over the basis functions
        weights_x.append(np.dot(fs.basis.T, forces[0]))
        weights_y.append(np.dot(fs.basis.T, forces[1]))

    if net is None:
        net = nengo.Network()
    with net:

        # --------------------- Inputs --------------------------
        net.input = nengo.Node(size_in=2, label='input')

        number = nengo.Node(output=[0], label='number')

        # ------------------- Point Attractors --------------------
        net.x = point_attractor.generate(n_neurons=1000,
                                         alpha=alpha,
                                         beta=beta)
        nengo.Connection(net.input[0], net.x.input[0], synapse=None)
        net.y = point_attractor.generate(n_neurons=1000,
                                         alpha=alpha,
                                         beta=beta)
        nengo.Connection(net.input[1], net.y.input[0], synapse=None)

        # -------------------- Oscillators ----------------------
        kick = nengo.Node(nengo.utils.functions.piecewise({
            0: 1,
            .05: 0
        }),
                          label='kick')
        osc = oscillator.generate(net, n_neurons=2000, speed=.025)
        osc.label = 'oscillator'
        nengo.Connection(kick, osc[0])

        # ------------------- Forcing Functions --------------------

        def dmp_weights_func(t, x):
            x = int(min(max(x, 0), len(forces)))
            # load weights for generating this number's x and y forces
            return np.hstack([weights_x[x], weights_y[x]])

        # create input switch for generating weights for different numbers
        # NOTE: this should be switched to an associative memory
        dmp_weights_gen = nengo.Node(output=dmp_weights_func,
                                     size_in=1,
                                     size_out=fs.n_basis * 2,
                                     label='dmp weights gen')
        nengo.Connection(number, dmp_weights_gen)

        # -------------------- Product for decoding -----------------------

        product_x = nengo.Network('Product X')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=product_x,
                               input_magnitude=1.0)
        product_y = nengo.Network('Product Y')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=product_y,
                               input_magnitude=1.0)

        # get the largest basis function value for normalization
        max_basis = np.max(fs.basis * fs.scale)
        domain = np.linspace(-np.pi, np.pi, fs.basis.shape[0])
        domain_cossin = np.array([np.cos(domain), np.sin(domain)]).T
        for ff, product in zip(
            [dmp_weights_gen[:fs.n_basis], dmp_weights_gen[fs.n_basis:]],
            [product_x, product_y]):
            for ii in range(fs.n_basis):
                # find the value of a basis function at a value of (x, y)
                target_function = nengo.utils.connection.target_function(
                    domain_cossin, fs.basis[:, ii] * fs.scale / max_basis)
                nengo.Connection(osc, product.B[ii], **target_function)
                # multiply the value of each basis function at x by its weight
            nengo.Connection(ff, product.A)

        nengo.Connection(product_x.output,
                         net.x.input[1],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)
        nengo.Connection(product_y.output,
                         net.y.input[1],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)

        # -------------------- Output ------------------------------

        net.output = nengo.Node(size_in=2, label='output')
        nengo.Connection(net.x.output, net.output[0])
        nengo.Connection(net.y.output, net.output[1])

        # create a node to give a plot of the represented function
        ff_plot = fs.make_plot_node(domain=domain, lines=2, ylim=[-50, 50])
        nengo.Connection(dmp_weights_gen[:fs.n_basis],
                         ff_plot[:fs.n_basis],
                         synapse=0.1)
        nengo.Connection(dmp_weights_gen[fs.n_basis:],
                         ff_plot[fs.n_basis:],
                         synapse=0.1)

    return net
Ejemplo n.º 2
0
domain = np.linspace(-1, 1, n_samples)


# define kinds of functions you'd like to represent
def gaussian(mag, mean, sd):
    return mag * np.exp(-(domain - mean)**2 / (2 * sd**2))


# create a distribution for generating encoders and eval points
gaussian_space = nengo.dists.Function(gaussian,
                                      mean=nengo.dists.Uniform(-1, 1),
                                      sd=nengo.dists.Uniform(0.01, 0.1),
                                      mag=nengo.dists.Uniform(-1, 1))

# build the function space, generate the basis functions
fs = nengo.FunctionSpace(gaussian_space, n_basis=30)

model = nengo.Network()
with model:

    # ensemble that store weights over basis functions
    # for the compressed function representation
    weights = nengo.Ensemble(n_neurons=1500, dimensions=fs.n_basis, radius=5)
    # set encoders to be sampled from weights for common functions
    weights.encoders = fs.project(gaussian_space)
    # set eval points to be sampled from weights for common functions
    weights.eval_points = fs.project(gaussian_space)

    # input to the stimulus population, whose output projects the
    # compressed function representation weights into weights
    # change stim_control for learning different functions in
    if mean < domain_min:
        mean += domain_range
    try:
        # Adding gaussians offset by the domain range to simulate cycling
        return mag * ( np.exp(-(domain - mean)**2 / (2 * std**2)) +\
                       np.exp(-(domain - mean - domain_range)**2 / (2 * std**2)) +\
                       np.exp(-(domain - mean + domain_range)**2 / (2 * std**2))
                     )

    except FloatingPointError:
        return domain * 0


fs = nengo.FunctionSpace(nengo.dists.Function(gaussian,
                                              mean=nengo.dists.Uniform(
                                                  domain_min, domain_max),
                                              std=nengo.dists.Uniform(.1, .7),
                                              mag=1),
                         n_basis=n_basis_1d)
fs2d = nengo.FunctionSpace(nengo.dists.Function(
    gaussian2d,
    mean_x=nengo.dists.Uniform(domain_min, domain_max),
    mean_y=nengo.dists.Uniform(domain_min, domain_max),
    std=nengo.dists.Uniform(.1, .7),
    mag=1),
                           n_basis=n_basis_2d,
                           n_samples=n_samples)

model = nengo.Network(seed=13)
model.config[nengo.Ensemble].neuron_type = nengo.Direct(
)  #TODO: temp, just use direct for debugging
ps_neuron_type = nengo.LIF(
def generate(input_signal, alpha=1000.0):
    beta = alpha / 4.0

    # Read in the class mean for numbers from vision network
    weights_data = np.load('models/mnist_vision_data/params.npz')
    weights = weights_data['Wc']
    means_data = np.load('models/mnist_vision_data/class_means.npz')
    means = np.matrix(1.0 / means_data['means'])
    sps = np.multiply(weights.T, means.T)[:10]
    sps_labels = [
        'ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT',
        'NINE'
    ]
    dimensions = weights.shape[0]

    # generate the Function Space
    forces, _, goals = forcing_functions.load_folder(
        'models/handwriting_trajectories',
        rhythmic=False,
        alpha=alpha,
        beta=beta)
    # make an array out of all the possible functions we want to represent
    force_space = np.vstack(forces)
    # use this array as our space to perform svd over
    fs = nengo.FunctionSpace(space=force_space, n_basis=10)

    # store the weights for each number
    weights_x = []
    weights_y = []
    for ii in range(len(goals)):
        forces = force_space[ii * 2:ii * 2 + 2]
        # load up the forces to be output by the forcing function
        # calculate the corresponding weights over the basis functions
        weights_x.append(np.dot(fs.basis.T, forces[0]))
        weights_y.append(np.dot(fs.basis.T, forces[1]))

    # Create our vocabularies
    rng = np.random.RandomState(0)
    vocab_vision = Vocabulary(dimensions=dimensions, rng=rng)
    vocab_dmp_weights_x = Vocabulary(dimensions=fs.n_basis + 2, rng=rng)
    vocab_dmp_weights_y = Vocabulary(dimensions=fs.n_basis + 2, rng=rng)
    for label, sp, wx, wy, goal in zip(sps_labels, sps, weights_x, weights_y,
                                       goals):
        vocab_vision.add(label,
                         np.array(sp)[0] / np.linalg.norm(np.array(sp)[0]))
        vocab_dmp_weights_x.add(label, np.hstack([wx, goal[0][0], goal[1][0]]))
        vocab_dmp_weights_y.add(label, np.hstack([wy, goal[0][1], goal[1][1]]))

    net = spa.SPA()
    # net.config[nengo.Ensemble].neuron_type = nengo.Direct()
    with net:

        # def input_func(t):
        #     return vocab_vision.parse(input_signal).v
        # net.input = nengo.Node(input_func, label='input')
        net.input = spa.State(dimensions, subdimensions=10, vocab=vocab_vision)

        time_func = lambda t: min(max((t * 2) % 4 - 2.5, -1), 1)
        timer_node = nengo.Node(output=time_func, label='timer')

        # ------------------- Point Attractors --------------------

        def goals_func(t, x):
            if (x[0] + 1) < 1e-5:
                return x[1], x[2]
            return x[3], x[4]

        goal_node = nengo.Node(goals_func,
                               size_in=5,
                               size_out=2,
                               label='goals')
        nengo.Connection(timer_node, goal_node[0])

        net.x = point_attractor.generate(n_neurons=1000,
                                         alpha=alpha,
                                         beta=beta)
        nengo.Connection(goal_node[0], net.x.input[0], synapse=None)
        net.y = point_attractor.generate(n_neurons=1000,
                                         alpha=alpha,
                                         beta=beta)
        nengo.Connection(goal_node[1], net.y.input[0], synapse=None)

        # -------------------- Ramp ------------------------------
        ramp_node = nengo.Node(output=time_func, label='ramp node')
        ramp = nengo.Ensemble(n_neurons=1000, dimensions=1, label='ramp')
        nengo.Connection(ramp_node, ramp)

        # ------------------- Forcing Functions --------------------

        net.assoc_mem_x = spa.AssociativeMemory(
            input_vocab=vocab_vision,
            output_vocab=vocab_dmp_weights_x,
            wta_output=False)
        nengo.Connection(net.input.output, net.assoc_mem_x.input)
        nengo.Connection(net.assoc_mem_x.output[[-2, -1]], goal_node[[1, 3]])

        net.assoc_mem_y = spa.AssociativeMemory(
            input_vocab=vocab_vision,
            output_vocab=vocab_dmp_weights_y,
            wta_output=False)
        nengo.Connection(net.input.output, net.assoc_mem_y.input)
        nengo.Connection(net.assoc_mem_y.output[[-2, -1]], goal_node[[2, 4]])

        # -------------------- Product for decoding -----------------------

        net.product_x = nengo.Network('Product X')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=net.product_x,
                               input_magnitude=1.0)
        net.product_y = nengo.Network('Product Y')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=net.product_y,
                               input_magnitude=1.0)

        # get the largest basis function value for normalization
        max_basis = np.max(fs.basis * fs.scale)
        domain = np.linspace(-1, 1, fs.basis.shape[0])

        for ff, product in zip(
            [net.assoc_mem_x.output, net.assoc_mem_y.output],
            [net.product_x, net.product_y]):
            for ii in range(fs.n_basis):
                # find the value of a basis function at a value of x
                def basis_fn(x, jj=ii):
                    index = int(x[0] * len(domain) / 2.0 + len(domain) / 2.0)
                    index = max(min(index, len(domain) - 1), 0)
                    return fs.basis[index][jj] * fs.scale / max_basis

                # multiply the value of each basis function at x by its weight
                nengo.Connection(ramp, product.B[ii], function=basis_fn)
                nengo.Connection(ff[ii], product.A[ii])

        def relay_func(t, x):
            t = time_func(t)
            if t <= -1:
                return [0, 0]
            return x

        relay = nengo.Node(output=relay_func,
                           size_in=2,
                           size_out=2,
                           label='relay')

        nengo.Connection(net.product_x.output,
                         relay[0],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)
        nengo.Connection(net.product_y.output,
                         relay[1],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)

        nengo.Connection(relay[0], net.x.input[1], synapse=None)
        nengo.Connection(relay[1], net.y.input[1], synapse=None)

        # -------------------- Output ------------------------------

        net.output = nengo.Node(size_in=2)
        nengo.Connection(net.x.output, net.output[0], synapse=0.01)
        nengo.Connection(net.y.output, net.output[1], synapse=0.01)

        # create a node to give a plot of the represented function
        ff_plot = fs.make_plot_node(domain=domain, lines=2, ylim=[-75, 75])
        nengo.Connection(net.assoc_mem_x.output[:fs.n_basis],
                         ff_plot[:fs.n_basis],
                         synapse=0.1)
        nengo.Connection(net.assoc_mem_y.output[:fs.n_basis],
                         ff_plot[fs.n_basis:],
                         synapse=0.1)

    return net
Ejemplo n.º 5
0
    if mean < domain_min:
        mean += domain_range
    try:
        # Adding gaussians offset by the domain range to simulate cycling
        return mag * ( np.exp(-(domain - mean)**2 / (2 * std**2)) +\
                       np.exp(-(domain - mean - domain_range)**2 / (2 * std**2)) +\
                       np.exp(-(domain - mean + domain_range)**2 / (2 * std**2))
                     )

    except FloatingPointError:
        return domain * 0


fs = nengo.FunctionSpace(nengo.dists.Function(gaussian,
                                              mean=nengo.dists.Uniform(
                                                  domain_min, domain_max),
                                              std=nengo.dists.Uniform(.1, .7),
                                              mag=1),
                         n_basis=10)

model = nengo.Network(seed=13)
model.config[nengo.Ensemble].neuron_type = nengo.Direct(
)  #TODO: temp, just use direct for debugging
ps_neuron_type = nengo.LIF()  # neuron type for posecells
with model:

    # Node that handles ROS communication
    # in:  best_x, best_y, best_th
    # out: vtrans, vrot, stim_x, stim_y, stim_th, energy
    if __name__ == '__main__':
        posecell_node = nengo.Node(NengoPosecellNetwork(),
                                   size_in=3,
def generate(input_signal, alpha=1000.0):
    beta = alpha / 4.0

    # Read in the class mean for numbers from vision network
    weights_data = np.load('models/mnist_vision_data/params.npz')
    weights = weights_data['Wc']
    means_data = np.load('models/mnist_vision_data/class_means.npz')
    means = np.matrix(1.0 / means_data['means'])
    sps = np.multiply(weights.T, means.T)[:10]
    sps_labels = [
        'ZERO', 'ONE', 'TWO', 'THREE', 'FOUR',
        'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE']
    dimensions = weights.shape[0]

    # generate the Function Space
    forces, _, _ = forcing_functions.load_folder(
        'models/handwriting_trajectories', rhythmic=True,
        alpha=alpha, beta=beta)
    # make an array out of all the possible functions we want to represent
    force_space = np.vstack(forces)
    # use this array as our space to perform svd over
    fs = nengo.FunctionSpace(space=force_space, n_basis=10)

    # store the weights for each trajectory
    weights_x = []
    weights_y = []
    for ii in range(len(forces)):
        forces = force_space[ii*2:ii*2+2]
        # load up the forces to be output by the forcing function
        # calculate the corresponding weights over the basis functions
        weights_x.append(np.dot(fs.basis.T, forces[0]))
        weights_y.append(np.dot(fs.basis.T, forces[1]))

    # Create our vocabularies
    rng = np.random.RandomState(0)
    vocab_vision = Vocabulary(dimensions=dimensions, rng=rng)
    vocab_dmp_weights_x = Vocabulary(dimensions=fs.n_basis, rng=rng)
    vocab_dmp_weights_y = Vocabulary(dimensions=fs.n_basis, rng=rng)
    for label, sp, wx, wy in zip(
            sps_labels, sps, weights_x, weights_y):
        vocab_vision.add(
            label, np.array(sp)[0] / np.linalg.norm(np.array(sp)[0]))
        vocab_dmp_weights_x.add(label, wx)
        vocab_dmp_weights_y.add(label, wy)


    net = spa.SPA()
    # net.config[nengo.Ensemble].neuron_type = nengo.Direct()
    with net:

        # --------------------- Inputs --------------------------
        # def input_func(t):
        #     return vocab_vision.parse(input_signal).v
        # net.input = nengo.Node(input_func, label='input')
        net.input = spa.State(dimensions, subdimensions=10,
                              vocab=vocab_vision)

        number = nengo.Node(output=[0], label='number')

        # ------------------- Point Attractors --------------------
        net.x = point_attractor.generate(
            n_neurons=1000, alpha=alpha, beta=beta)
        net.y = point_attractor.generate(
            n_neurons=1000, alpha=alpha, beta=beta)

        # -------------------- Oscillators ----------------------
        kick = nengo.Node(nengo.utils.functions.piecewise({0: 1, .05: 0}),
                          label='kick')
        osc = oscillator.generate(net, n_neurons=2000, speed=.05)
        osc.label = 'oscillator'
        nengo.Connection(kick, osc[0])

        # ------------------- Forcing Functions --------------------

        net.assoc_mem_x = spa.AssociativeMemory(
            input_vocab=vocab_vision,
            output_vocab=vocab_dmp_weights_x,
            wta_output=False)
        nengo.Connection(net.input.output, net.assoc_mem_x.input)

        net.assoc_mem_y = spa.AssociativeMemory(
            input_vocab=vocab_vision,
            output_vocab=vocab_dmp_weights_y,
            wta_output=False)
        nengo.Connection(net.input.output, net.assoc_mem_y.input)

        # -------------------- Product for decoding -----------------------

        net.product_x = nengo.Network('Product X')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=net.product_x,
                               input_magnitude=1.0)
        net.product_y = nengo.Network('Product Y')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=net.product_y,
                               input_magnitude=1.0)

        # get the largest basis function value for normalization
        max_basis = np.max(fs.basis*fs.scale)
        domain = np.linspace(-np.pi, np.pi, fs.basis.shape[0])
        domain_cossin = np.array([np.cos(domain), np.sin(domain)]).T
        for ff, product in zip([net.assoc_mem_x.output,
                                net.assoc_mem_y.output],
                               [net.product_x, net.product_y]):
            for ii in range(fs.n_basis):
                # find the value of a basis function at a value of (x, y)
                target_function = nengo.utils.connection.target_function(
                    domain_cossin, fs.basis[:, ii]*fs.scale/max_basis)
                nengo.Connection(osc, product.B[ii], **target_function)
                # multiply the value of each basis function at x by its weight
            nengo.Connection(ff, product.A)

        nengo.Connection(net.product_x.output, net.x.input[1],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)
        nengo.Connection(net.product_y.output, net.y.input[1],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)

        # -------------------- Output ------------------------------

        net.output = nengo.Node(size_in=2, label='output')
        nengo.Connection(net.x.output, net.output[0])
        nengo.Connection(net.y.output, net.output[1])

        # create a node to give a plot of the represented function
        ff_plot = fs.make_plot_node(domain=domain, lines=2,
                                    ylim=[-1000000, 1000000])
        nengo.Connection(net.assoc_mem_x.output,
                         ff_plot[:fs.n_basis], synapse=0.1)
        nengo.Connection(net.assoc_mem_y.output,
                         ff_plot[fs.n_basis:], synapse=0.1)

    return net
Ejemplo n.º 7
0
def generate(input_signal, alpha=1000.0):
    beta = alpha / 4.0

    # generate the Function Space
    forces, _, goals = forcing_functions.load_folder(
        'models/locomotion_trajectories',
        rhythmic=True,
        alpha=alpha,
        beta=beta)
    # make an array out of all the possible functions we want to represent
    force_space = np.vstack(forces)
    # use this array as our space to perform svd over
    fs = nengo.FunctionSpace(space=force_space, n_basis=10)

    # store the weights for each movement
    weights_a = []  # ankle
    weights_k = []  # knee
    weights_h = []  # hip
    # NOTE: things are added to weights based on the order files are read
    for ii in range(int(len(goals) / 6)):
        forces = force_space[ii * 6:ii * 6 + 6]
        # load up the forces to be output by the forcing function
        # calculate the corresponding weights over the basis functions
        weights_a.append(
            np.hstack([
                np.dot(fs.basis.T, forces[0]),  # ankle 1
                np.dot(fs.basis.T, forces[1])
            ]))  # ankle 2
        weights_h.append(
            np.hstack([
                np.dot(fs.basis.T, forces[2]),  # hip 1
                np.dot(fs.basis.T, forces[3])
            ]))  # hip 2
        weights_k.append(
            np.hstack([
                np.dot(fs.basis.T, forces[4]),  # knee 1
                np.dot(fs.basis.T, forces[5])
            ]))  # knee 2

    # Create our vocabularies
    sps_labels = ['GALLOP', 'RUNNING', 'WALKING']
    rng = np.random.RandomState(0)
    dimensions = 50  # some arbitrary number
    vocab_input = Vocabulary(dimensions=dimensions, rng=rng)
    vocab_dmp_weights_a = Vocabulary(dimensions=fs.n_basis * 2, rng=rng)
    vocab_dmp_weights_k = Vocabulary(dimensions=fs.n_basis * 2, rng=rng)
    vocab_dmp_weights_h = Vocabulary(dimensions=fs.n_basis * 2, rng=rng)

    for ii, (label, wa, wk,
             wh) in enumerate(zip(sps_labels, weights_a, weights_k,
                                  weights_h)):
        vocab_input.parse(label)  # randomly generate input vector
        vocab_dmp_weights_a.add(label, wa)
        vocab_dmp_weights_k.add(label, wk)
        vocab_dmp_weights_h.add(label, wh)

    net = spa.SPA()
    net.config[nengo.Ensemble].neuron_type = nengo.LIFRate()
    with net:

        config = nengo.Config(nengo.Ensemble)
        config[nengo.Ensemble].neuron_type = nengo.Direct()
        with config:
            # --------------------- Inputs --------------------------

            # def input_func(t):
            #     return vocab_input.parse(input_signal).v
            # net.input = nengo.Node(input_func)
            net.input = spa.State(dimensions,
                                  subdimensions=10,
                                  vocab=vocab_input)

            # ------------------- Point Attractors --------------------

            zero = nengo.Node([0])
            net.a1 = point_attractor.generate(n_neurons=1000,
                                              alpha=alpha,
                                              beta=beta)
            nengo.Connection(zero, net.a1.input[0], synapse=None)
            net.a2 = point_attractor.generate(n_neurons=1000,
                                              alpha=alpha,
                                              beta=beta)
            nengo.Connection(zero, net.a1.input[0], synapse=None)

            net.k1 = point_attractor.generate(n_neurons=1000,
                                              alpha=alpha,
                                              beta=beta)
            nengo.Connection(zero, net.k1.input[0], synapse=None)
            net.k2 = point_attractor.generate(n_neurons=1000,
                                              alpha=alpha,
                                              beta=beta)
            nengo.Connection(zero, net.k2.input[0], synapse=None)

            net.h1 = point_attractor.generate(n_neurons=1000,
                                              alpha=alpha,
                                              beta=beta)
            nengo.Connection(zero, net.h1.input[0], synapse=None)
            net.h2 = point_attractor.generate(n_neurons=1000,
                                              alpha=alpha,
                                              beta=beta)
            nengo.Connection(zero, net.h2.input[0], synapse=None)

        # -------------------- Oscillators ----------------------

        kick = nengo.Node(nengo.utils.functions.piecewise({
            0: 1,
            .05: 0
        }),
                          label='kick')

        osc = oscillator.generate(net, n_neurons=3000, speed=.01)
        osc.label = 'oscillator'
        nengo.Connection(kick, osc[0])

        # ------------------- Forcing Functions --------------------

        with config:
            net.assoc_mem_a = spa.AssociativeMemory(
                input_vocab=vocab_input,
                output_vocab=vocab_dmp_weights_a,
                wta_output=False)
            nengo.Connection(net.input.output, net.assoc_mem_a.input)

            net.assoc_mem_k = spa.AssociativeMemory(
                input_vocab=vocab_input,
                output_vocab=vocab_dmp_weights_k,
                wta_output=False)
            nengo.Connection(net.input.output, net.assoc_mem_k.input)

            net.assoc_mem_h = spa.AssociativeMemory(
                input_vocab=vocab_input,
                output_vocab=vocab_dmp_weights_h,
                wta_output=False)
            nengo.Connection(net.input.output, net.assoc_mem_h.input)

            # -------------------- Product for decoding -----------------------

            product_a1 = nengo.Network('Product A1')
            nengo.networks.Product(n_neurons=1000,
                                   dimensions=fs.n_basis,
                                   net=product_a1)
            product_a2 = nengo.Network('Product A2')
            nengo.networks.Product(n_neurons=1000,
                                   dimensions=fs.n_basis,
                                   net=product_a2)

            product_h1 = nengo.Network('Product H1')
            nengo.networks.Product(n_neurons=1000,
                                   dimensions=fs.n_basis,
                                   net=product_h1)
            product_h2 = nengo.Network('Product H2')
            nengo.networks.Product(n_neurons=1000,
                                   dimensions=fs.n_basis,
                                   net=product_h2)

            product_k1 = nengo.Network('Product K1')
            nengo.networks.Product(n_neurons=1000,
                                   dimensions=fs.n_basis,
                                   net=product_k1)
            product_k2 = nengo.Network('Product K2')
            nengo.networks.Product(n_neurons=1000,
                                   dimensions=fs.n_basis,
                                   net=product_k2)

            # get the largest basis function value for normalization
            max_basis = np.max(fs.basis * fs.scale)
            domain = np.linspace(-np.pi, np.pi, fs.basis.shape[0])
            domain_cossin = np.array([np.cos(domain), np.sin(domain)]).T
            for ff, product in zip([
                    net.assoc_mem_a.output[:fs.n_basis],
                    net.assoc_mem_a.output[fs.n_basis:],
                    net.assoc_mem_k.output[:fs.n_basis],
                    net.assoc_mem_k.output[fs.n_basis:],
                    net.assoc_mem_h.output[:fs.n_basis],
                    net.assoc_mem_h.output[fs.n_basis:]
            ], [
                    product_a1, product_a2, product_k1, product_k2, product_h1,
                    product_h2
            ]):
                for ii in range(fs.n_basis):
                    # find the value of a basis function at a value of (x, y)
                    target_function = nengo.utils.connection.target_function(
                        domain_cossin, fs.basis[:, ii] * fs.scale / max_basis)
                    nengo.Connection(osc, product.B[ii], **target_function)
                    # multiply the value of each basis function at x by its weight
                nengo.Connection(ff, product.A)

            nengo.Connection(product_a1.output,
                             net.a1.input[1],
                             transform=np.ones((1, fs.n_basis)) * max_basis)
            nengo.Connection(product_a2.output,
                             net.a2.input[1],
                             transform=np.ones((1, fs.n_basis)) * max_basis)

            nengo.Connection(product_k1.output,
                             net.k1.input[1],
                             transform=np.ones((1, fs.n_basis)) * max_basis)
            nengo.Connection(product_k2.output,
                             net.k2.input[1],
                             transform=np.ones((1, fs.n_basis)) * max_basis)

            nengo.Connection(product_h1.output,
                             net.h1.input[1],
                             transform=np.ones((1, fs.n_basis)) * max_basis)
            nengo.Connection(product_h2.output,
                             net.h2.input[1],
                             transform=np.ones((1, fs.n_basis)) * max_basis)

            # -------------------- Output ------------------------------

            net.output = nengo.Node(size_in=6, label='output')
            nengo.Connection(net.a1.output, net.output[0], synapse=0.01)
            nengo.Connection(net.a2.output, net.output[1], synapse=0.01)
            nengo.Connection(net.k1.output, net.output[2], synapse=0.01)
            nengo.Connection(net.k2.output, net.output[3], synapse=0.01)
            nengo.Connection(net.h1.output, net.output[4], synapse=0.01)
            nengo.Connection(net.h2.output, net.output[5], synapse=0.01)

            # add in the goal offsets
            nengo.Connection(net.assoc_mem_a.output[[-2, -1]],
                             net.output[[0, 1]],
                             synapse=None)
            nengo.Connection(net.assoc_mem_k.output[[-2, -1]],
                             net.output[[2, 3]],
                             synapse=None)
            nengo.Connection(net.assoc_mem_h.output[[-2, -1]],
                             net.output[[4, 5]],
                             synapse=None)

            # create a node to give a plot of the represented function
            ff_plot_a = fs.make_plot_node(domain=domain,
                                          lines=2,
                                          ylim=[-1000000, 1000000])
            nengo.Connection(net.assoc_mem_a.output, ff_plot_a, synapse=0.1)

            ff_plot_k = fs.make_plot_node(domain=domain,
                                          lines=2,
                                          ylim=[-1000000, 1000000])
            nengo.Connection(net.assoc_mem_k.output, ff_plot_k, synapse=0.1)

            ff_plot_h = fs.make_plot_node(domain=domain,
                                          lines=2,
                                          ylim=[-1000000, 1000000])
            nengo.Connection(net.assoc_mem_h.output, ff_plot_h, synapse=0.1)

    return net
Ejemplo n.º 8
0
pZA = prior
logpZA = np.log(pZA)
M = len(prior)

# --------------------------------------------------------------------------------- #

## MODEL
for index in range(num_iterations):

    def prior_space():
        idx = np.random.randint(0, len(pZA))
        return pZA[idx]

    n_basis = 20

    space = nengo.FunctionSpace(nengo.dists.Function(prior_space),
                                n_basis=n_basis)

    from copy import deepcopy
    space_raw = deepcopy(space.space)

    ### ----------------------------------------------------------------------------------------------- #

    # initial input
    global p, i, k, spy
    i = 0
    k = 0
    srch_space = M

    beta = 0.65
    dt = 0.001
Ejemplo n.º 9
0
    return post


ages = np.linspace(start=1, stop=100, num=100, dtype=np.int32)
data = {}
n_basis = 20
for x in ages:
    if x < 5:
        pad = 5 - x + 1
    else:
        pad = 0

    # define sub-spaces
    space = nengo.FunctionSpace(nengo.dists.Function(
        skew_gauss,
        skew=nengo.dists.Uniform(skew - 1, skew + 2),
        loc=nengo.dists.Uniform(loc - 1, loc + 2),
        scale=nengo.dists.Uniform(scale - 1, scale + 2)),
                                n_basis=n_basis)

    from copy import deepcopy
    space_raw = deepcopy(space.space)

    lik_space = nengo.FunctionSpace(nengo.dists.Function(likelihood,
                                                         x=nengo.dists.Uniform(
                                                             x - 5 + pad,
                                                             x + 5 + pad)),
                                    n_basis=n_basis)

    lik_space_raw = deepcopy(lik_space.space)

    post_space = nengo.FunctionSpace(nengo.dists.Function(
Ejemplo n.º 10
0
def generate(data_folder, net=None, alpha=1000.0):
    beta = alpha / 4.0

    # generate the Function Space
    forces, _, goals = forcing_functions.load_folder(data_folder,
                                                     rhythmic=False,
                                                     alpha=alpha,
                                                     beta=beta)
    # make an array out of all the possible functions we want to represent
    force_space = np.vstack(forces)
    # use this array as our space to perform svd over
    fs = nengo.FunctionSpace(space=force_space, n_basis=10)
    range_goals = np.array(range(len(goals)))

    # store the weights for each number
    weights_x = []
    weights_y = []
    for ii in range(len(goals)):
        forces = force_space[ii * 2:ii * 2 + 2]
        # load up the forces to be output by the forcing function
        # calculate the corresponding weights over the basis functions
        weights_x.append(np.dot(fs.basis.T, forces[0]))
        weights_y.append(np.dot(fs.basis.T, forces[1]))

    if net is None:
        net = nengo.Network()
    # net.config[nengo.Ensemble].neuron_type = nengo.Direct()
    with net:

        time_func = lambda t: min(max((t * 2) % 4 - 2.5, -1), 1)
        timer_node = nengo.Node(output=time_func)
        net.number = nengo.Node(output=lambda t, x: x / (len(goals) / 2.0) - 1,
                                size_in=1,
                                size_out=1)
        # ------------------- Point Attractors --------------------

        goal_net = goal_network.generate(goals)
        nengo.Connection(net.number, goal_net.input)
        nengo.Connection(timer_node, goal_net.inhibit_node)

        net.x = point_attractor.generate(n_neurons=1000,
                                         alpha=alpha,
                                         beta=beta)
        nengo.Connection(goal_net.output[0], net.x.input[0], synapse=None)
        net.y = point_attractor.generate(n_neurons=1000,
                                         alpha=alpha,
                                         beta=beta)
        nengo.Connection(goal_net.output[1], net.y.input[0], synapse=None)

        # -------------------- Ramp ------------------------------
        ramp_node = nengo.Node(output=time_func)
        ramp = nengo.Ensemble(n_neurons=1000, dimensions=1, label='ramp')
        nengo.Connection(ramp_node, ramp)

        # ------------------- Forcing Functions --------------------

        # n_basis_functions dimensions to represent the weights, + 1 to
        # represent the x position to decode from
        ff_x = nengo.Ensemble(n_neurons=1000,
                              dimensions=fs.n_basis,
                              radius=np.sqrt(fs.n_basis))
        ff_y = nengo.Ensemble(n_neurons=1000,
                              dimensions=fs.n_basis,
                              radius=np.sqrt(fs.n_basis))

        def dmp_weights_func(x, x_or_y):
            # find the nearest value
            num = range_goals[min(
                max(
                    np.abs(range_goals -
                           ((x + 1) * len(goals) / 2.0)).argmin(), 0),
                len(goals))]
            # load weights for generating this number's x and y forces
            if x_or_y == 'x':
                return weights_x[num]
            elif x_or_y == 'y':
                return weights_y[num]

        # generate weights for different numbers
        dmp_weights_gen = nengo.Ensemble(n_neurons=2000,
                                         dimensions=1,
                                         label='dmp_weights_gen')
        nengo.Connection(net.number, dmp_weights_gen)
        nengo.Connection(dmp_weights_gen,
                         ff_x,
                         function=lambda x: dmp_weights_func(x, x_or_y='x'),
                         synapse=.01)
        nengo.Connection(dmp_weights_gen,
                         ff_y,
                         function=lambda x: dmp_weights_func(x, x_or_y='y'),
                         synapse=.01)

        # -------------------- Product for decoding -----------------------

        product_x = nengo.Network('Product X')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=product_x)
        product_y = nengo.Network('Product Y')
        nengo.networks.Product(n_neurons=1000,
                               dimensions=fs.n_basis,
                               net=product_y)

        # get the largest basis function value for normalization
        max_basis = np.max(fs.basis * fs.scale)
        domain = np.linspace(-1, 1, fs.basis.shape[0])

        for ff, product in zip([ff_x, ff_y], [product_x, product_y]):
            for ii in range(fs.n_basis):
                # find the value of a basis function at a value of x
                def basis_fn(x, jj=ii):
                    index = int(x[0] * len(domain) / 2.0 + len(domain) / 2.0)
                    index = max(min(index, len(domain) - 1), 0)
                    return fs.basis[index][jj] * fs.scale / max_basis

                # multiply the value of each basis function at x by its weight
                nengo.Connection(ramp, product.B[ii], function=basis_fn)
                nengo.Connection(ff[ii], product.A[ii])

        def relay_func(t, x):
            t = time_func(t)
            if t <= -1:
                return [0, 0]
            return x

        relay = nengo.Node(output=relay_func, size_in=2, size_out=2)

        nengo.Connection(product_x.output,
                         relay[0],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)
        nengo.Connection(product_y.output,
                         relay[1],
                         transform=np.ones((1, fs.n_basis)) * max_basis,
                         synapse=None)

        nengo.Connection(relay[0], net.x.input[1], synapse=None)
        nengo.Connection(relay[1], net.y.input[1], synapse=None)

        # -------------------- Output ------------------------------

        net.output = nengo.Node(size_in=2)
        nengo.Connection(net.x.output, net.output[0], synapse=0.01)
        nengo.Connection(net.y.output, net.output[1], synapse=0.01)

        # create a node to give a plot of the represented function
        ff_plot = fs.make_plot_node(domain=domain, lines=2, ylim=[-50, 50])
        nengo.Connection(ff_x, ff_plot[:fs.n_basis], synapse=0.1)
        nengo.Connection(ff_y, ff_plot[fs.n_basis:], synapse=0.1)

    return net