Message Boards Message Boards

Setup a pointer like data structure for modeling a spike timing network?

Posted 7 years ago

Hello everyone,

I recently started using Mathematica for modeling a type of spike timing neural networks where every neuron is connected to a number of other nodes and is evaluated at each time step. These types of models require to hold some information about each node. That is:

  • the nodes that it receives input from (connections)
  • the weight for each connection
  • a list of the states for the last k timesteps (memory)

In addition the network structure is dynamic and nodes are frequently added and removed from the network. In a procedural language such as C, such a network would be implemented using pointers to objects or structs. However I have not found a good data structure for my models in Mathematica. In my recent attempts I used Associations with the the key referencing to the ID of a node and each node holding an association with all the information about the node (including an adjecency lists, list of weights, etc). Yet I find this structure to be highly inefficient as every reference from node i to node j causes a lookup of the index j in the association.

Does anyone know a good approach for implementing such a network in Mathematica?

POSTED BY: Emanuel Gerber
4 Replies
Posted 7 years ago

Thank you for your replies! Neil Singer's approach is very useful for my specific task. I did not consider using Function overloading myself. I will test the performance of this data structure. I will also provide code the next time that I make a post in this forum.

Regards

POSTED BY: Emanuel Gerber

Neil SInger's approach seems quite good for this sort of thing. I have used it to emulate (mutable) binary trees. If at all possible you would want to use the second method, with Set (that is, =) rather than SetDelayed (:=). The lookup of pattern-free downvalues is via hash table, and is effectively constant time (not the same as "free" unfortunately, but still reasonable especially if the data set is large).

If for some reason this does not fit the specifics of your task (to repeat what others stated, more information and actual code would help here), there is some discussion and code here for obtaining a pointer-like capability. I have also used this approach for tree implementation. It might be slightly faster than using downvalues but there is a learning curve.

As for using Association, again it is important to see actual code. If you post a minimal example that shows the badness you encounter, possibly someone will see a way to improve on it. As Sander Huisman noted, they can be very fast in many situations, if used carefully.

POSTED BY: Daniel Lichtblau

Emanuel,

I suggest you add some more details for what you want but I would suggest using Rules and Patterns. For example, each neuron could be defined as neuron[nodeI_, nodeJ_]:= {node that it receives info from, Weight, {list of previous states}} or possibly neuron[nodeI_, nodeJ_]= {node that it receives info from, Weight, {list of previous states}} if the information on the right had side can be evaluated at the time of definition. The lookup of a neuron is then just neuron[27,54] to find a particular neuron.

As an example

Note that if each neuron is different (as I suspect they are), you can make your definition do something like this (where you define the functions "from", "weight", and "state" to setup your neurons. You can define the neurons many different ways but the key is to rely on Mathematica's overloading capability (which is very fast).

In[1]:= neuron[nodeI_, nodeJ_] := 
 neuron[nodeI, nodeJ] = {from[nodeI], weight[nodeJ], state[nodeI, nodeJ]}

In[2]:= neuron[1, 5]

Out[2]= {from[1], weight[5], state[1, 5]}

In[3]:= neuron[34, 65]

Out[3]= {from[34], weight[65], state[34, 65]}

In[4]:= ?neuron

Global`neuron

neuron[1,5]={from[1],weight[5],state[1,5]}

neuron[34,65]={from[34],weight[65],state[34,65]}

neuron[nodeI_,nodeJ_]:=neuron[nodeI,nodeJ]={from[nodeI],weight[nodeJ],state[nodeI,nodeJ]}

In my example above, I used the same function to define the neurons as to hold the neurons so that the definition is only run the first time that particular neuron is called. Another alternative is to make a "createNeurons" function and create them all at once as neuron[x,y]={}. You can also have "UpdateState" functions that operate on your neurons and change them.

I hope this helps but without knowing more about what you want to do, Its hard to go further. Also note that Mathematica can pass functions as data so you can define neurons as lists with functions that travel with each one if that helps.

Regards

POSTED BY: Neil Singer

Please post your code so others can asses and understand what you're on about... Associations can be very fast---if used properly. Without any code it is very hard understand what you did and what you want.

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract