How to use nengo - 10 common examples

To help you get started, we’ve selected a few nengo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github studywolf / blog / Nengo scripting / arm_control / nengo_arm.py View on Github external
y3 = y2 - len1 * np.cos(angle_offset-angles[0] - angles[1])

        arm_func._nengo_html_ = '''
        <svg viewBox="0 0 100 100" height="100%" width="100%">
            <line style="stroke:black" y2="{y2}" x2="{x2}" y1="{y1}" x1="{x1}"></line>
            <line style="stroke:black" y2="{y3}" x2="{x3}" y1="{y2}" x1="{x2}"></line>
            <circle fill="black" stroke-width="1" stroke="black" r="1.5" cy="{y3}" cx="{x3}"></circle>
        </svg>
        '''.format(**locals())
        # end of visualization code ---------------------------------------------

        return data
    arm_node = nengo.Node(output=arm_func, size_in=2)

    # specify torque input to arm
    input_node = nengo.Node(output=[1, .1])

    # to send a target to an ensemble which then connections to the arm
    ens = nengo.Ensemble(n_neurons=500, dimensions=2, radius=np.sqrt(20))
    nengo.Connection(input_node, ens[:2]) # to send target info to ensemble

    # connect ens to arm
    nengo.Connection(ens, arm_node)#, function=some_function) 
    # --------------------------------------------------------

if run_in_GUI:
    # to run in GUI, comment out next 4 lines for running without GUI
    import nengo_gui
    nengo_gui.GUI(model=model, filename=__file__, locals=locals(), 
                  interactive=False, allow_file_change=False).start()
    import sys
    sys.exit()
github studywolf / blog / Nengo scripting / Nengo 2 / arm_control / nengo_arm.py View on Github external
<svg viewBox="0 0 100 100" height="100%" width="100%">
            <line style="stroke:black" y2="{y2}" x2="{x2}" y1="{y1}" x1="{x1}"></line>
            <line style="stroke:black" y2="{y3}" x2="{x3}" y1="{y2}" x1="{x2}"></line>
            <circle fill="black" stroke-width="1" stroke="black" r="1.5" cy="{y3}" cx="{x3}"></circle>
        </svg>
        '''.format(**locals())
        # end of visualization code ---------------------------------------------

        return data
    arm_node = nengo.Node(output=arm_func, size_in=2)

    # specify torque input to arm
    input_node = nengo.Node(output=[1, .1])

    # to send a target to an ensemble which then connections to the arm
    ens = nengo.Ensemble(n_neurons=500, dimensions=2, radius=20)
    nengo.Connection(input_node, ens[:2]) # to send target info to ensemble

    # connect ens to arm
    nengo.Connection(ens, arm_node)#, function=some_function) 
    # --------------------------------------------------------

if run_in_GUI:
    # to run in GUI, comment out next 4 lines for running without GUI
    import nengo_gui
    nengo_gui.GUI(model=model, filename=__file__, locals=locals(), 
                  interactive=False, allow_file_change=False).start()
    import sys
    sys.exit()
else:  
    # to run in command line
    with model:
github studywolf / blog / Nengo scripting / Nengo 2 / arm_control / nengo_arm.py View on Github external
if run_in_GUI:
    # to run in GUI, comment out next 4 lines for running without GUI
    import nengo_gui
    nengo_gui.GUI(model=model, filename=__file__, locals=locals(), 
                  interactive=False, allow_file_change=False).start()
    import sys
    sys.exit()
else:  
    # to run in command line
    with model:    
        probe_input = nengo.Probe(input_node)
        probe_arm = nengo.Probe(arm_node[arm.DOF*2])
        
    print 'building model...'
    sim = nengo.Simulator(model, dt=.001)
    print 'build complete.'

    sim.run(10)

    t = sim.trange()
    x = sim.data[probe_arm]
    y = sim.data[probe_arm]

    # plot collected data
    import matplotlib.pyplot as plt

    plt.subplot(311)
    plt.plot(t, x)
    plt.xlabel('time')
    plt.ylabel('probe_arm0')
github studywolf / blog / Nengo scripting / arm_control / nengo_arm.py View on Github external
if run_in_GUI:
    # to run in GUI, comment out next 4 lines for running without GUI
    import nengo_gui
    nengo_gui.GUI(model=model, filename=__file__, locals=locals(), 
                  interactive=False, allow_file_change=False).start()
    import sys
    sys.exit()
else:  
    # to run in command line
    with model:    
        probe_input = nengo.Probe(input_node)
        probe_arm = nengo.Probe(arm_node[arm.DOF*2])
        
    print 'building model...'
    sim = nengo.Simulator(model, dt=.001)
    print 'build complete.'

    sim.run(10)

    t = sim.trange()
    x = sim.data[probe_arm]
    y = sim.data[probe_arm]

    # plot collected data
    import matplotlib.pyplot as plt

    plt.subplot(311)
    plt.plot(t, x)
    plt.xlabel('time')
    plt.ylabel('probe_arm0')
github hunse / nef-rbm / nlif-deep.py View on Github external
if 1:
    # deep.backprop(train, test, n_epochs=100)
    deep.sgd(train, test, n_epochs=50)
    print "mean error", deep.test(test).mean()

# --- try to get autoencoder back
if 0:
    deep.auto_sgd_down(train_images, test_images, rate=0.6, n_epochs=30)
    print "recons error", rms(test_images - recons, axis=1).mean()

if 0:
    # Try to learn linear reconstructor (doesn't work too well)
    import nengo

    codes = deep.encode(train_images)
    decoders, info = nengo.decoders.LstsqL2()(codes, train_images)
    print info['rmses'].mean()

    recons = np.dot(codes, decoders)
    print rms(train_images - recons, axis=1).mean()

    plt.figure(99)
    plt.clf()
    show_recons(test_images, recons)

if 0:
    # save parameters
    d = {}
    d['weights'] = [auto.W.get_value() for auto in deep.autos]
    d['biases'] = [auto.c.get_value() for auto in deep.autos]
    if all(hasattr(auto, 'V') for auto in deep.autos):
        d['rec_weights'] = [auto.V.get_value() for auto in deep.autos]
github hunse / nef-rbm / auto / train_lif.py View on Github external
# deep.backprop(train, test, n_epochs=50, noise=0.5, shift=True)

    deep.sgd(train, test, n_epochs=50, tradeoff=1, noise=0.3, shift=True)
    print "mean error", deep.test(test).mean()

# --- try to get autoencoder back
if 0:
    deep.auto_sgd_down(train_images, test_images, rate=0.6, n_epochs=30)
    print "recons error", rms(test_images - recons, axis=1).mean()

if 0:
    # Try to learn linear reconstructor (doesn't work too well)
    import nengo

    codes = deep.encode(train_images)
    decoders, info = nengo.decoders.LstsqL2()(codes, train_images)
    print info['rmses'].mean()

    recons = np.dot(codes, decoders)
    print rms(train_images - recons, axis=1).mean()

    plt.figure(99)
    plt.clf()
    show_recons(test_images, recons)

if 0:
    # save parameters
    d = {}
    d['weights'] = [auto.W.get_value() for auto in deep.autos]
    d['biases'] = [auto.c.get_value() for auto in deep.autos]
    if all(hasattr(auto, 'V') for auto in deep.autos):
        d['rec_weights'] = [auto.V.get_value() for auto in deep.autos]
github adityagilra / FOLLOW / input_ff_rec_robot_nengo_directu_ocl.py View on Github external
#plt.show()
#sys.exit()
del robtrange,rateEvolveProbe                                   # free some memory

if __name__ == "__main__":
    #########################
    ### Create Nengo network
    #########################
    print('building model')
    mainModel = nengo.Network(label="Single layer network", seed=seedR0)
    with mainModel:
        nodeIn = nengo.Node( size_in=N//2, output = lambda timeval,currval: inpfn(timeval)*varFactors[Nobs:] )
                                                                # scale input to network by torque factors
        # input layer from which feedforward weights to ratorOut are computed
        ratorIn = nengo.Ensemble( Nexc, dimensions=N//2, radius=reprRadiusIn,
                            neuron_type=nengo.neurons.LIF(), seed=seedR1, label='ratorIn' )
        nengo.Connection(nodeIn, ratorIn, synapse=None)
                                                                # No filtering here as no filtering/delay in the plant/arm
        # layer with learning incorporated
        #intercepts = np.append(np.random.uniform(-0.2,0.2,size=Nexc//2),np.random.uniform(-1.,1.,size=Nexc//2))
        ratorOut = nengo.Ensemble( Nexc, dimensions=Nobs, radius=reprRadius,\
                                    neuron_type=nengo.neurons.LIF(), seed=seedR2, label='ratorOut')
        # don't use the same seeds across the connections,
        #  else they seem to be all evaluated at the same values of low-dim variables
        #  causing seed-dependent convergence issues possibly due to similar frozen noise across connections
        
        if trialClamp:
            # clamp ratorOut at the end of each trial (Tperiod) for 100ms.
            #  Error clamped below during end of the trial for 100ms.
            clampValsZeros = np.zeros(Nexc)
            clampValsNegs = -100.*np.ones(Nexc)
            endTrialClamp = nengo.Node(lambda t: clampValsZeros if (t%Tperiod)&lt;(Tperiod-Tclamp) else clampValsNegs)
github adityagilra / FOLLOW / input_ff_rec_robot_nengo_directu_ocl.py View on Github external
###
            ### Add the exc learning rules to the connection, and the error ensemble to the learning rule ###
            ###
            EtoERulesDict = { 'PES' : nengo.PES(learning_rate=PES_learning_rate_rec,
                                            pre_tau=tau) }#,
                                            #clipType=excClipType,
                                            #decay_rate_x_dt=excPES_weightsDecayRate*dt,
                                            #integral_tau=excPES_integralTau) }
            plasticConnEE.learning_rule_type = EtoERulesDict
            #plasticConnEE.learning_rule['PES'].learning_rate=0
                                                            # learning_rate has no effect
                                                            # set to zero, yet works fine!
                                                            # It works only if you set it
                                                            # in the constructor PES() above
            # feedforward learning rule
            InEtoERulesDict = { 'PES' : nengo.PES(learning_rate=PES_learning_rate_FF,
                                            pre_tau=tau) }#,
                                            #clipType=excClipType,
                                            #decay_rate_x_dt=excPES_weightsDecayRate*dt,
                                            #integral_tau=excPES_integralTau) }
            InEtoE.learning_rule_type = InEtoERulesDict

            if learnIfNoInput:  # obsolete, no support for trialClamp
                print("Obsolete flag learnIfNoInput")
                sys.exit(1)
                errorWt = nengo.Node( size_in=Nobs, output = lambda timeval,errWt: \
                                            zeros2N if (timeval%Tperiod) &lt; rampT else errWt*(np.abs(errWt)&gt;weightErrorCutoff) )
                                                            # only learn when there is no input,
                                                            #  using the local (input+err) current
                                                            #  thus, only the error is used &amp; input doesn't interfere
                nengo.Connection(errorOff,errorWt,synapse=weightErrorTau)
                                                            # error to errorWt ensemble, filter for weight learning
github adityagilra / FOLLOW / input_ff_rec_transform_nengo_ocl.py View on Github external
# clamp ratorIn and ratorOut at the end of each trial (Tperiod) for 100ms.
            #  Error clamped below during end of the trial for 100ms.
            clampValsZeros = np.zeros(Nexc)
            clampValsNegs = -100.*np.ones(Nexc)
            endTrialClamp = nengo.Node(lambda t: clampValsZeros if (t%Tperiod)&lt;(Tperiod-Tclamp) else clampValsNegs)
            nengo.Connection(endTrialClamp,ratorIn.neurons,synapse=1e-3)
            nengo.Connection(endTrialClamp,ratorOut.neurons,synapse=1e-3)
                                                                # fast synapse for fast-reacting clamp
        
        if plastDecoders:
            # don't use the same seeds across the connections,
            #  else they seem to be all evaluated at the same values of low-dim variables
            #  causing seed-dependent convergence issues possibly due to similar frozen noise across connections
            if initLearned:
                # default transform is unity
                InEtoE = nengo.Connection(ratorIn, ratorOut, synapse=tau)
            else:
                InEtoE = nengo.Connection(ratorIn, ratorOut, transform=Wdyn2, synapse=tau)
            #np.random.seed(1)
            #InEtoE = nengo.Connection(nodeIn, ratorOut, synapse=None,
            #            transform=np.random.uniform(-20,20,size=(N,N))+np.eye(N))
            EtoE = nengo.Connection(ratorOut, ratorOut,
                                transform=Wdyn2, synapse=tau)   # synapse is tau_syn for filtering
        else:
            # If initLearned==True, these weights will be reset below using InEtoEfake and EtoEfake
            if copycatLayer and not copycatPreLearned:          # if copycatLayer from Wdesired, we don't learn the FF transform,
                                                                #  else we cannot compare to copycatweights, since a constant is arbitrary between ff and rec.
                InEtoE = nengo.Connection(ratorIn.neurons, ratorOut.neurons, synapse=tau)
                                                                # the system didn't learn in this case
                                                                #  possibly the problem is neurons to neurons here and ensemble to ensemble for InEtoEexpect
            else:
                InEtoE = nengo.Connection(ratorIn.neurons, ratorOut.neurons,
github adityagilra / FOLLOW / input_ff_rec_transform_nengo_ocl.py View on Github external
if copycatLayer and not copycatPreLearned:          # if copycatLayer from Wdesired, we don't learn the FF transform,
                                                                #  else we cannot compare to copycatweights, since a constant is arbitrary between ff and rec.
                InEtoE = nengo.Connection(ratorIn.neurons, ratorOut.neurons, synapse=tau)
                                                                # the system didn't learn in this case
                                                                #  possibly the problem is neurons to neurons here and ensemble to ensemble for InEtoEexpect
            else:
                InEtoE = nengo.Connection(ratorIn.neurons, ratorOut.neurons,
                                                transform=Wdyn2/20., synapse=tau)
                                                                # Wdyn2 same as for EtoE, but mean(InEtoE) = mean(EtoE)/20
            EtoE = nengo.Connection(ratorOut.neurons, ratorOut.neurons,
                                transform=Wdyn2, synapse=tau)   # synapse is tau_syn for filtering

        # initLearned
        if initLearned and not inhibition:                      # initLearned=True will set bidirectional weights
                                                                #  thus only useful if inhibition=False
            InEtoEfake = nengo.Connection(ratorIn, ratorOut, synapse=tau)
            EtoEfake = nengo.Connection(ratorOut, ratorOut,
                            function=Wdesired, synapse=tau) # synapse is tau_syn for filtering

        # probes
        nodeIn_probe = nengo.Probe(nodeIn, synapse=None)
        ratorIn_probe = nengo.Probe(ratorIn, synapse=tau)
        # don't probe what is encoded in ratorIn, rather what is sent to ratorOut
        # 'output' reads out the output of the connection InEtoE in nengo 2.2.1.dev0
        #  but in older nengo ~2.0, the full variable encoded in ratorOut (the post-ensemble of InEtoE)
        # NOTE: InEtoE is from neurons to neurons, so 'output' is Nexc-dim not N-dim!
        #ratorIn_probe = nengo.Probe(InEtoE, 'output')
        #ratorIn_probe = nengo.Probe(InEtoE, 'input', synapse=tau)
        # don't probe ratorOut here as this calls build_decoders() separately for this;
        #  just call build_decoders() once for ratorOut2error, and probe 'output' of that connection below
        #ratorOut_probe = nengo.Probe(ratorOut, synapse=tau)
                                                                # synapse is tau for filtering