Skip to content

Instantly share code, notes, and snippets.

@Uiuran
Created December 8, 2012 17:49
Show Gist options
  • Save Uiuran/4241160 to your computer and use it in GitHub Desktop.
Save Uiuran/4241160 to your computer and use it in GitHub Desktop.
Minimum framework for simulation of neural networks evolution and computing
""" NeuralNetwork class to arrange neurons in a topology and do dynamics """
# MinimumBrain is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MinimumBrain is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MinimumBrain. If not, see <http://www.gnu.org/licenses/>.
#
#
# A NeuralNetwork receives a adjacency matrix as input, that is, a bi-dimensional list
# where the first index is a presynaptic neuron (signal sender), the second index is a post
# synaptic neuron (signal receiver). Entry > 0 or < 0 means connected, 0 means unconnected.
#
# Dynamics:
# Network support dynamics in the following sense: adjacency matrix can be modified at any
# time to reflect synaptic changing or neuro-muta-genesis, i.e., one can change entries in the list
# or add rows(in case of data-injector neuron) or columns( case of outputers neurons).
# Beware that one may be only mutating a neuron function by changing adjacency matrix size,
# e.g. suppose that matrix is 4 X 3, than neuron 4 is sending data only (although he can process,
# he cannot receive from others, in computer science jargon he is not 'hidden'), once you change
# the matrix for 4x4, he can now receive from another neuron(even from himself!!). Suppose now that
# you change the matrix for 4x5, you give birth to a neuron that outputs signal (i.e. is not 'hidden'),
# one more change for 5x5 may transform this neuron in a hidden neuron.
#
# Neuron Communications
# Network support event handling for its units, a neuron may be receiving('r'), sending('s'), the both('rs') or resting('rest').
# This is done by keeping record of the neurons states( 'r','s','rs' or 'rest' for each) in tandem and updating neuron activities.
# Each time stamp the network 1-) does the activity registred in actual state, 2-) update the state following the rules of timed
# synapses and topology. If the input is persistent, i.e., the network is receiving data at each time stamp, than the neurons must
# be in one of the three first state ('r','s','rs'), else there may be some neurons in the 'rest' state.
import numpy
import neuron_logistic as neuron
class NeuralNetwork:
def __init__(self,adjacency,time_unit):
''' .NeuralNetwork(adjacencyMatrix,time_unit), the adjacencyMatrix must be bidimensional list, any other entry
such unidimensional terms will be interpreted as [0], or if it is a (n > 2)-dimensional term it will be interpreted
as [any_elements] = 0. Time_unit refer to the minimum time_unit step for events propagation in the network, continuous
neuronal process may take place in time 0 < t <= time_unit. '''
if type(adjacency) != list:
self.edges = [];
else:
self.edges = adjacency;
self.time_unit = time_unit;
self.clock = 0;
self.findNeuronNumber();
self.state_book = {};
self.registerState();
def setNetInput(self,inputmatrix,inputlayer,initialweight):
''' Set neurons that receive the data input vector and its weights. Note that each input real value has a
set of synaptic weights for each neuron in the input layer. Inputlayer is a list of integer indexes, listing input neurons, in that case initialweight is a matrix DataVecSize X InputLayerSize of float numbers or it's simple 'random' for random input weights.'''
# TODO - Verificar matrix de entrada de acordo com o tipo desejado.
self.data_in = inputmatrix;
self.input_neurons = inputlayer;
self.input_weight = initialweight;
def verifyEdge(self,index1,index2):
''' Use .verifyEdge(index1,index2), where index1 is the pre-synaptic neuron, index2 is the post synaptic neuron.
returns Weight if the connection does exist and 0 otherwise. Note that to verify the inverse relation you must use .verifyEdge(index2,index1)'''
if type(index1) != int or type(index2) != int:
return 0;
rowl = len(self.edges)-1;
if (index1 > rowl):
return 0;
elif (index1 <= rowl and index1 >= 0):
if (type(self.edges[index1]) == list) and (index2 <= len(self.edges[index1])-1):
return self.edges[index1][index2];
else:
return 0;
else:
return 0;
def findNeuronNumber(self):
''' Function detects the number of neurons in the network by peeking at the adjacency matrix '''
self.vertex_num = len(self.edges);
for i in self.edges:
if type(i) == list:
for n in i:
if type(n) != float:
tmp = self.edges.index(i);
self.edges[tmp][self.edges[tmp].index(n)] = 0.0;
if len(i) > self.vertex_num:
self.vertex_num = len(i);
else:z
self.edges[self.edges.index(i)] = [0];
def changeState(self,index,state):
''' Change the state of neuron(index) to 'state' ('r'or 's'or 'rs'or 'rest') in the actual .state_book entry. '''
if index <= self.vertex_num - 1:
if state == 'r' or state == 's' or state == 'rs' or state == 'rest':
self.state_book[self.clock][index] = state;
def registerState(self):
''' Register de system state in timestamp = self.clock in the .state_book[timestamp] = [stateneuron0,stateneuron1,...] '''
if self.clock == 0:
self.state_book[self.clock] = [];
for i in xrange(self.vertex_num):
self.state_book[self.clock].append('rest');
else:
self.state_book[self.clock] = [];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment