Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://www.nengo.ai/nengo/examples/basic/two-neurons.html
- import matplotlib.pyplot as plt
- import numpy as np
- import nengo
- from nengo.dists import Uniform
- from nengo.utils.matplotlib import rasterplot
- model = nengo.Network(label="Two Neurons")
- with model:
- neuron = nengo.Ensemble(
- 2, #number of neurons
- dimensions=1, # Represent a scalar
- # Set intercept to 0.5
- intercepts=Uniform(-0.5, -0.5),
- # Set the maximum firing rate of the neuron to 100hz
- max_rates=Uniform(100, 100),
- # Set the neuron's firing rate to increase for positive input
- encoders=[[1], [-1]], # One 'on' and one 'off' neuron
- )
- #****************
- # PROVIDE INPUT
- #****************
- input_signal = nengo.Node(lambda t: np.cos(8 * t))
- #****************
- # CONNECT ELEMENTS
- #****************
- #Connect the input signal to the neuron
- nengo.Connection(input_signal, neuron, synapse=0.01)
- #****************
- # PROBES
- #****************
- # The original input
- input_signal_probe = nengo.Probe(input_signal)
- # The raw spikes from the neuron
- spikes = nengo.Probe(neuron.neurons)
- # Subthreshold soma voltage of the neuron
- voltage = nengo.Probe(neuron.neurons, "voltage")
- # Spikes filtered by a 10ms post-synaptic filter
- filtered = nengo.Probe(neuron, synapse=0.01)
- with nengo.Simulator(model) as sim: # Create the simulator
- #****************
- # Run
- #****************
- sim.run(1) # Run it for 1 second
- #****************
- # plot the results
- #****************
- t = sim.trange()
- # Plot the decoded output of the ensemble
- plt.figure()
- plt.plot(sim.trange(), sim.data[filtered])
- plt.plot(sim.trange(), sim.data[input_signal_probe])
- plt.xlim(0, 1)
- # Plot the spiking output of the ensemble
- plt.figure(figsize=(10, 8))
- plt.subplot(221)
- rasterplot(sim.trange(), sim.data[spikes])
- plt.yticks((1,2),("On neuron", "Off neuron"))
- plt.ylabel("Neuron")
- plt.xlim(0, 1)
- plt.ylim(2.5, 0.5)
- # Plot the soma voltages of the neurons
- plt.subplot(222)
- #plt.plot(sim.trange(), sim.data[voltage][:, 0], "r")
- plt.plot(t, sim.data[voltage][:, 0] + 1, "r")
- plt.plot(t, sim.data[voltage][:, 1], "k")
- plt.xlim(0, 1)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement