[SOVLED] How to save synaptic weights every step?


#1

Dear All,

Now, I can get the synaptic weights by:

conn_l = nest.GetConnections(target=[output_layer[0]])
weights_l = nest.GetStatus(conn_l, keys="weight")

My question is how to save the weights locally by each step? Either in csv or h5py file should work.
When the brain model is initialized, I can use logger to print them once in the console once.

Regards,
Zhenshan


#2

You can define a new TF that saves csv files. You’ll have to use the decorator @MapCSVRecorder and define the headers of the csv file (probably depends on how many neurons you want to record from). Please check one of the existing csv recorder TFs in the template experiment, for example $HBP/Experiments/braitenberg_husky/csv_robot_position.py.

If you’re running from the virtual coach, yuo can use the sim.save_csv() call to create a local copy of whatever csv files you’re recording.

Hope this helps,
Mahmoud


#3

Each time you call sim.save_csv(), a new directory with a timestamp will be created and your data will be stored inside.

One more thing to notice, is that the number of columns in the saved csv file will be always the number you defined + 1. This extra column is very sparse, as it is always False but only True in case a reset event occured in the simulation.


#4

Dear Mahmoud,

Thanks for the reply.
After reading the example again, I think I understand the procedure to save csv files.
However, I have one important point unclear that where and how the source information should come from. To be specific, the robot position is acquired from Gazebo. For my case, the code for getting synaptic weight lie in the brain model. How should I make the weight available for @MapCSVRecorder.

Should I write a ros publisher ? Actually I tried to write a publisher in the brain model like below. But it didn’t pub anything when I run the simulation. However, it will pub once when the brain model is reset.

def create_brain():
    """
    Initializes PyNN with the neuronal network that has to be simulated
    """
    rate = rospy.Rate(10)
    pub = rospy.Publisher('chatter', String, queue_size=10)
    pub.publish("Hello World")
    ...

I mean the brain model is kind of for initializing the brain, however can not provide information every step, right? Maybe if I can find the code place where the brain is simulated each time, I can make it print out ?

Regards,
Zhenshan


#5

I will try to reproduce your problem. How do you get the weight in PyNN (not in nest)?


#6

Hi Mahmoud,

Thanks for your help. I think I get them via NEST.
My brain has one input and output layer. It also has two neurons serves as the dopamine modulator. It looks like this:

# from hbp_nrp_cle.brainsim import simulator as sim
import numpy as np
import logging
import nest
import pyNN.nest as sim
from pyNN.nest import *
from hbp_nrp_excontrol.logs import clientLogger
import h5py

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


def create_brain():
"""
Initializes PyNN with the neuronal network that has to be simulated
"""
resolution = [2, 8]
# R-STDP parameters
# Minimum weight value
w_max = 2500.
# Maximum weight value
w_min = -w_max
# Minimum initial random value
w0_min = 0.1
# Maximum initial random value
w0_max = 0.11
# Time constant of reward signal in ms
tau_n = 200.
# Time constant of eligibility trace in ms
tau_c = 1000.
# Factor that dopamine modulator for turning is multiplied with
max_turning_dopamine_factor = 0.0015
# Factor that dopamine modulator for speed is multiplied with
max_speed_dopamine_factor = 0.001
# Constant scaling strength of potentiation
A_plus = 1.
# Constant scaling strength of depression
A_minus = 1.

nest.ResetKernel()

############################
###### CREATE LAYERS #######
############################

INPUT_OUTPUT_PARAMS = {'v_rest': -60.5,
                'cm': 0.025,
                'tau_m': 10.,
                # brain sim time length 1 s -> max rate 100 
                'tau_refrac': 20.0,
                'tau_syn_E': 2.5,
                'tau_syn_I': 2.5,
                'e_rev_E': 0.0,
                'e_rev_I': -75.0,
                'v_thresh': -60.0,
                'v_reset': -60.5}

all_neurons = sim.Population(20, sim.IF_cond_alpha, INPUT_OUTPUT_PARAMS)
input_layer = sim.PopulationView(all_neurons, slice(0, 16))
dm_layer = sim.PopulationView(all_neurons, slice(16, 18))
output_layer = sim.PopulationView(all_neurons, slice(18, 20))
spike_detector = nest.Create("spike_detector", 2, params={"withtime": True})
vt = nest.Create('volume_transmitter', 2)

############################
###### CONNECT LAYERS ######
############################
nest.CopyModel('stdp_dopamine_synapse', 'syn_left',
               {'Wmax': 2500.0,
                'Wmin': -2500.0,
                'tau_plus': 20.0,
                'A_minus': 1.0,
                'A_plus': -1.0,
                'b': 0.01,
                'tau_c': 10.0,
                'tau_n': 5.0,
                'vt': vt[0]})

nest.CopyModel('stdp_dopamine_synapse', 'syn_right',
               {'Wmax': 2500.0,
                'Wmin': -2500.0,
                'tau_plus': 20.0,
                'A_minus': 1.0,
                'A_plus': -1.0,
                'b': 0.01,
                'tau_c': 10.0,
                'tau_n': 5.0,
                'vt': vt[1]})

syn_con_left = nest.Connect(map(int, input_layer.all_cells),
                       map(int, output_layer[0:1]), syn_spec={'model': 'syn_left', 'weight': {'distribution': 'uniform', 'low': w0_min, 'high': w0_max}})

syn_con_right = nest.Connect(map(int, input_layer.all_cells),
                       map(int, output_layer[1:2]),
                       syn_spec={'model': 'syn_right', 'weight': {'distribution': 'uniform', 'low': w0_min, 'high': w0_max}})

nest.Connect(map(int, dm_layer[0:1]), vt[0:1])
nest.Connect(map(int, dm_layer[1:2]), vt[1:2])

conn_l = nest.GetConnections(target=[output_layer[0]])
weights_l = nest.GetStatus(conn_l, keys="weight")

conn_r = nest.GetConnections(target=[output_layer[1]])
weights_r = nest.GetStatus(conn_r, keys="weight")

cur_time_step = sim.get_time_step()

logger.debug("Synapse Left: " + str(weights_l))
logger.debug("Sim Time Step: " + str(cur_time_step) + " (ms)")
logger.debug("Circuit description: " + str(all_neurons.describe()))
return all_neurons

circuit = create_brain()

I use nest.Connect to connect neurons. Use nest.GetConnections to get synapses. Use nest.GetStatus to get the value of the weights.

Regards,
Zhenshan


#7

Actually, the brain seems learning when I run it. I just need to observe the synapse. Once the SNN has been trained successfully, I have to save the weights and load it for testing.
I try to get access the weights in TF but failed and do not know how.


#8

Hi Zhenshan,

The reason why you can’t access your weights from the transfer function - I guess - is because the variables in your brain files are not on the global scope. Try the same brain file without the create_brain function:

# from hbp_nrp_cle.brainsim import simulator as sim
import numpy as np
import logging
import nest
import pyNN.nest as sim
from pyNN.nest import *
from hbp_nrp_excontrol.logs import clientLogger
import h5py

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


"""
Initializes PyNN with the neuronal network that has to be simulated
"""
resolution = [2, 8]
# R-STDP parameters
# Minimum weight value
w_max = 2500.
# Maximum weight value
w_min = -w_max
# Minimum initial random value
w0_min = 0.1
# Maximum initial random value
w0_max = 0.11
# Time constant of reward signal in ms
tau_n = 200.
# Time constant of eligibility trace in ms
tau_c = 1000.
# Factor that dopamine modulator for turning is multiplied with
max_turning_dopamine_factor = 0.0015
# Factor that dopamine modulator for speed is multiplied with
max_speed_dopamine_factor = 0.001
# Constant scaling strength of potentiation
A_plus = 1.
# Constant scaling strength of depression
A_minus = 1.

nest.ResetKernel()

############################
###### CREATE LAYERS #######
############################

INPUT_OUTPUT_PARAMS = {'v_rest': -60.5,
                'cm': 0.025,
                'tau_m': 10.,
                # brain sim time length 1 s -> max rate 100 
                'tau_refrac': 20.0,
                'tau_syn_E': 2.5,
                'tau_syn_I': 2.5,
                'e_rev_E': 0.0,
                'e_rev_I': -75.0,
                'v_thresh': -60.0,
                'v_reset': -60.5}

all_neurons = sim.Population(20, sim.IF_cond_alpha, INPUT_OUTPUT_PARAMS)
input_layer = sim.PopulationView(all_neurons, slice(0, 16))
dm_layer = sim.PopulationView(all_neurons, slice(16, 18))
output_layer = sim.PopulationView(all_neurons, slice(18, 20))
spike_detector = nest.Create("spike_detector", 2, params={"withtime": True})
vt = nest.Create('volume_transmitter', 2)

############################
###### CONNECT LAYERS ######
############################
nest.CopyModel('stdp_dopamine_synapse', 'syn_left',
               {'Wmax': 2500.0,
                'Wmin': -2500.0,
                'tau_plus': 20.0,
                'A_minus': 1.0,
                'A_plus': -1.0,
                'b': 0.01,
                'tau_c': 10.0,
                'tau_n': 5.0,
                'vt': vt[0]})

nest.CopyModel('stdp_dopamine_synapse', 'syn_right',
               {'Wmax': 2500.0,
                'Wmin': -2500.0,
                'tau_plus': 20.0,
                'A_minus': 1.0,
                'A_plus': -1.0,
                'b': 0.01,
                'tau_c': 10.0,
                'tau_n': 5.0,
                'vt': vt[1]})

syn_con_left = nest.Connect(map(int, input_layer.all_cells),
                       map(int, output_layer[0:1]), syn_spec={'model': 'syn_left', 'weight': {'distribution': 'uniform', 'low': w0_min, 'high': w0_max}})

syn_con_right = nest.Connect(map(int, input_layer.all_cells),
                       map(int, output_layer[1:2]),
                       syn_spec={'model': 'syn_right', 'weight': {'distribution': 'uniform', 'low': w0_min, 'high': w0_max}})

nest.Connect(map(int, dm_layer[0:1]), vt[0:1])
nest.Connect(map(int, dm_layer[1:2]), vt[1:2])

conn_l = nest.GetConnections(target=[output_layer[0]])
weights_l = nest.GetStatus(conn_l, keys="weight")

conn_r = nest.GetConnections(target=[output_layer[1]])
weights_r = nest.GetStatus(conn_r, keys="weight")

cur_time_step = sim.get_time_step()

logger.debug("Synapse Left: " + str(weights_l))
logger.debug("Sim Time Step: " + str(cur_time_step) + " (ms)")
logger.debug("Circuit description: " + str(all_neurons.describe()))

circuit = all_neurons

#10

Hi Jacques,

It works! Very much appreciated for your advice.

For other users as an example, define a TF like:

import hbp_nrp_cle.tf_framework as nrp

nrp.config.brain_root.conn_l
@nrp.Neuron2Robot()
def csv_weights_monitor (t):

    import nest
    a = nest.GetStatus(nrp.config.brain_root.conn_l, keys="weight")
    clientLogger.info("TEST:", str(a))

Regards,
Zhenshan