Advertisement
toweber

nengo-second_test

Sep 19th, 2022
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # https://www.nengo.ai/nengo/examples/basic/two-neurons.html
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import nengo
  5. from nengo.dists import Uniform
  6. from nengo.utils.matplotlib import rasterplot
  7.  
  8.  
  9. model = nengo.Network(label="Two Neurons")
  10.  
  11. with model:
  12.     neuron = nengo.Ensemble(
  13.         2, #number of neurons
  14.         dimensions=1,  # Represent a scalar
  15.         # Set intercept to 0.5
  16.         intercepts=Uniform(-0.5, -0.5),
  17.         # Set the maximum firing rate of the neuron to 100hz
  18.         max_rates=Uniform(100, 100),
  19.         # Set the neuron's firing rate to increase for positive input
  20.         encoders=[[1], [-1]], # One 'on' and one 'off' neuron
  21.     )
  22.  
  23.     #****************        
  24.     # PROVIDE INPUT
  25.     #****************
  26.     input_signal = nengo.Node(lambda t: np.cos(8 * t))
  27.  
  28.     #****************
  29.     # CONNECT ELEMENTS
  30.     #****************
  31.     #Connect the input signal to the neuron
  32.     nengo.Connection(input_signal, neuron, synapse=0.01)
  33.  
  34.     #****************
  35.     # PROBES
  36.     #****************
  37.     # The original input
  38.     input_signal_probe = nengo.Probe(input_signal)
  39.     # The raw spikes from the neuron
  40.     spikes = nengo.Probe(neuron.neurons)
  41.     # Subthreshold soma voltage of the neuron
  42.     voltage = nengo.Probe(neuron.neurons, "voltage")
  43.     # Spikes filtered by a 10ms post-synaptic filter
  44.     filtered = nengo.Probe(neuron, synapse=0.01)
  45.  
  46.  
  47. with nengo.Simulator(model) as sim:  # Create the simulator
  48.     #****************
  49.     # Run
  50.     #****************
  51.     sim.run(1)  # Run it for 1 second
  52.  
  53. #****************
  54. # plot the results
  55. #****************
  56. t =  sim.trange()
  57. # Plot the decoded output of the ensemble
  58. plt.figure()
  59. plt.plot(sim.trange(), sim.data[filtered])
  60. plt.plot(sim.trange(), sim.data[input_signal_probe])
  61. plt.xlim(0, 1)
  62.  
  63. # Plot the spiking output of the ensemble
  64. plt.figure(figsize=(10, 8))
  65. plt.subplot(221)
  66. rasterplot(sim.trange(), sim.data[spikes])
  67. plt.yticks((1,2),("On neuron", "Off neuron"))
  68. plt.ylabel("Neuron")
  69. plt.xlim(0, 1)
  70. plt.ylim(2.5, 0.5)
  71.  
  72. # Plot the soma voltages of the neurons
  73. plt.subplot(222)
  74. #plt.plot(sim.trange(), sim.data[voltage][:, 0], "r")
  75. plt.plot(t, sim.data[voltage][:, 0] + 1, "r")
  76. plt.plot(t, sim.data[voltage][:, 1], "k")
  77.  
  78. plt.xlim(0, 1)
  79. plt.show()
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement