Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synapse trace #61

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions Example/helpers/mask_example.ipynb

Large diffs are not rendered by default.

70 changes: 42 additions & 28 deletions Example/test/layer4.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@
"init_s": null
}
},
{
"key": 360,
"class": [
"python_callable",
"conex.behaviors.neurons.specs",
"SpikeTrace"
],
"parameters_args": [],
"parameters_kwargs": {
"tau_s": 10.0
}
},
{
"key": 380,
"class": [
Expand All @@ -89,8 +77,7 @@
"max_delay": 1,
"proximal_min_delay": 0,
"distal_min_delay": 0,
"apical_min_delay": 0,
"have_trace": null
"apical_min_delay": 0
}
},
{
Expand Down Expand Up @@ -169,18 +156,6 @@
"init_s": null
}
},
{
"key": 360,
"class": [
"python_callable",
"conex.behaviors.neurons.specs",
"SpikeTrace"
],
"parameters_args": [],
"parameters_kwargs": {
"tau_s": 10.0
}
},
{
"key": 380,
"class": [
Expand All @@ -193,8 +168,7 @@
"max_delay": 1,
"proximal_min_delay": 0,
"distal_min_delay": 0,
"apical_min_delay": 0,
"have_trace": null
"apical_min_delay": 0
}
}
],
Expand Down Expand Up @@ -250,6 +224,16 @@
"parameters_kwargs": {
"current_coef": 1
}
},
{
"key": 420,
"class": [
"python_callable",
"conex.behaviors.synapses.specs",
"PreSpikeCatcher"
],
"parameters_args": [],
"parameters_kwargs": {}
}
],
"device": "cpu",
Expand Down Expand Up @@ -305,6 +289,16 @@
"parameters_kwargs": {
"current_coef": 1
}
},
{
"key": 420,
"class": [
"python_callable",
"conex.behaviors.synapses.specs",
"PreSpikeCatcher"
],
"parameters_args": [],
"parameters_kwargs": {}
}
],
"device": "cpu",
Expand Down Expand Up @@ -360,6 +354,16 @@
"parameters_kwargs": {
"current_coef": 1
}
},
{
"key": 420,
"class": [
"python_callable",
"conex.behaviors.synapses.specs",
"PreSpikeCatcher"
],
"parameters_args": [],
"parameters_kwargs": {}
}
],
"device": "cpu",
Expand Down Expand Up @@ -415,6 +419,16 @@
"parameters_kwargs": {
"current_coef": 1
}
},
{
"key": 420,
"class": [
"python_callable",
"conex.behaviors.synapses.specs",
"PreSpikeCatcher"
],
"parameters_args": [],
"parameters_kwargs": {}
}
],
"device": "cpu",
Expand Down
39 changes: 30 additions & 9 deletions Example/test/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
SimpleDendriteStructure,
SimpleDendriteComputation,
LIF,
SpikeTrace,
NeuronAxon,
)
from conex.behaviors.synapses import (
SynapseInit,
PreTrace,
PostTrace,
PreSpikeCatcher,
PostSpikeCatcher,
WeightInitializer,
SimpleDendriticInput,
SimpleSTDP,
Expand Down Expand Up @@ -55,7 +58,6 @@
MNIST_ROOT = "~/Temp/MNIST/"
SENSORY_SIZE_HEIGHT = 28
SENSORY_SIZE_WIDTH = 28 # MNIST's image size
SENSORY_TRACE_TAU_S = 2.7

# Layer 4
L4_EXC_DEPTH = 4
Expand All @@ -66,7 +68,6 @@
L4_EXC_TAU = 10.0
L4_EXC_V_RESET = 0.0
L4_EXC_V_REST = 0.0
L4_EXC_TRACE_TAU = 10.0

L4_INH_SIZE = 576
L4_INH_R = 5.0
Expand Down Expand Up @@ -95,11 +96,18 @@


INP_CC_MODE = "random"
INP_CC_WEIGHT_SHAPE = (4,1,5,5)
INP_CC_WEIGHT_SHAPE = (4, 1, 5, 5)
INP_CC_COEF = 1
INP_CC_A_PLUS = 0.01
INP_CC_A_MINUS = 0.002

L4_EXC_L23_EXC_PRE_TRACE = 10.0
L4_EXC_L23_EXC_POST_TRACE = 10.0


SENSORY_L4_PRE_TRACE = 10.0
SENSORY_L4_POST_TRACE = 10.0


##################################################
# making dataloader
Expand Down Expand Up @@ -134,9 +142,8 @@
sensory_size=NeuronDimension(
depth=1, height=SENSORY_SIZE_HEIGHT, width=SENSORY_SIZE_WIDTH
),
sensory_trace=SENSORY_TRACE_TAU_S,
instance_duration=POISSON_TIME,
output_ports={"data_out": (None,[("sensory_pop", {})])}
output_ports={"data_out": (None, [("sensory_pop", {})])},
)

##################################################
Expand All @@ -159,7 +166,6 @@
v_reset=L4_EXC_V_RESET,
v_rest=L4_EXC_V_REST,
),
SpikeTrace(tau_s=L4_EXC_TRACE_TAU),
NeuronAxon(),
]
),
Expand All @@ -180,7 +186,6 @@
v_reset=L4_INH_V_RESET,
v_rest=L4_INH_V_REST,
),
SpikeTrace(tau_s=L4_INH_TRACE_TAU),
NeuronAxon(),
]
),
Expand All @@ -196,6 +201,7 @@
SynapseInit(),
WeightInitializer(mode=L4_EXC_EXC_MODE),
SimpleDendriticInput(current_coef=L4_EXC_EXC_COEF),
PreSpikeCatcher(),
]
),
)
Expand All @@ -210,6 +216,7 @@
SynapseInit(),
WeightInitializer(mode=L4_EXC_INH_MODE),
SimpleDendriticInput(current_coef=L4_EXC_INH_COEF),
PreSpikeCatcher(),
]
),
)
Expand All @@ -224,6 +231,7 @@
SynapseInit(),
WeightInitializer(mode=L4_INH_EXC_MODE),
SimpleDendriticInput(current_coef=L4_INH_EXC_COEF),
PreSpikeCatcher(),
]
),
)
Expand All @@ -238,6 +246,7 @@
SynapseInit(),
WeightInitializer(mode=L4_INH_INH_MODE),
SimpleDendriticInput(current_coef=L4_INH_INH_COEF),
PreSpikeCatcher(),
]
),
)
Expand Down Expand Up @@ -314,6 +323,10 @@
WeightInitializer(mode=L4_L2_MODE),
SimpleDendriticInput(current_coef=L4_L2_COEF),
SimpleSTDP(a_plus=L4_L2_A_PLUS, a_minus=L4_L2_A_MINUS),
PreSpikeCatcher(),
PostSpikeCatcher(),
PreTrace(tau_s=L4_EXC_L23_EXC_PRE_TRACE),
PostTrace(tau_s=L4_EXC_L23_EXC_POST_TRACE),
]
),
"Proximal",
Expand Down Expand Up @@ -357,9 +370,17 @@
synapsis_behavior=prioritize_behaviors(
[
SynapseInit(),
WeightInitializer(mode=INP_CC_MODE, weight_shape=INP_CC_WEIGHT_SHAPE, kernel_shape=INP_CC_WEIGHT_SHAPE),
WeightInitializer(
mode=INP_CC_MODE,
weight_shape=INP_CC_WEIGHT_SHAPE,
kernel_shape=INP_CC_WEIGHT_SHAPE,
),
Conv2dDendriticInput(current_coef=INP_CC_COEF),
Conv2dSTDP(a_plus=INP_CC_A_PLUS, a_minus=INP_CC_A_MINUS),
PreSpikeCatcher(),
PostSpikeCatcher(),
PreTrace(tau_s=SENSORY_L4_PRE_TRACE),
PostTrace(tau_s=SENSORY_L4_POST_TRACE),
]
),
synaptic_tag="Proximal",
Expand Down
15 changes: 1 addition & 14 deletions conex/behaviors/neurons/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ class NeuronAxon(Behavior):
"""
Propagate the spikes and apply the delay mechanism.

Note: should be added after fire and trace behavior.
Note: should be added after fire.

Args:
max_delay (int): Maximum delay of all dendrites connected to the neurons. This value determines the delay buffer size.
proximal_min_delay (int): Minimum delay of proximal dendrites. The default is 0.
distal_min_delay (int): Minimum delay of distal dendrites. The default is 0.
apical_min_delay (int): Minimum delay of apical dendrites. The default is 0.
have_trace (boolean): whether to calculate trace or not. None checks if trace is available.
"""

def __init__(
Expand All @@ -27,7 +26,6 @@ def __init__(
proximal_min_delay=0,
distal_min_delay=0,
apical_min_delay=0,
have_trace=None,
**kwargs,
):
super().__init__(
Expand All @@ -36,7 +34,6 @@ def __init__(
proximal_min_delay=proximal_min_delay,
distal_min_delay=distal_min_delay,
apical_min_delay=apical_min_delay,
have_trace=have_trace,
**kwargs,
)

Expand All @@ -45,11 +42,8 @@ def initialize(self, neurons):
self.proximal_min_delay = self.parameter("proximal_min_delay", 0)
self.distal_min_delay = self.parameter("distal_min_delay", 0)
self.apical_min_delay = self.parameter("apical_min_delay", 0)
self.have_trace = self.parameter("have_trace", hasattr(neurons, "trace"))

self.spike_history = neurons.vector_buffer(self.max_delay, dtype=torch.bool)
if self.have_trace:
self.trace_history = neurons.vector_buffer(self.max_delay)

neurons.axon = self

Expand All @@ -70,14 +64,7 @@ def update_min_delay(self, neurons):
def get_spike(self, neurons, delay):
return self.spike_history.gather(0, delay.unsqueeze(0)).squeeze(0)

def get_spike_trace(self, neurons, delay):
return self.trace_history.gather(0, delay.unsqueeze(0)).squeeze(0)

def forward(self, neurons):
self.spike_history = neurons.buffer_roll(
mat=self.spike_history, new=neurons.spikes
)
if self.have_trace:
self.trace_history = neurons.buffer_roll(
mat=self.trace_history, new=neurons.trace
)
26 changes: 0 additions & 26 deletions conex/behaviors/neurons/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,6 @@ def forward(self, neurons):
neurons.v += neurons.vector(mode=self.mode, scale=self.scale) + self.offset


class SpikeTrace(Behavior):
"""
Calculates the spike trace.

Note : should be placed after Fire behavior.

Args:
tau_s (float): decay term for spike trace. The default is None.
"""

def __init__(self, tau_s, *args, **kwargs):
super().__init__(*args, tau_s=tau_s, **kwargs)

def initialize(self, neurons):
"""
Sets the trace attribute for the neural population.
"""
self.tau_s = self.parameter("tau_s", None, required=True)
neurons.trace = neurons.vector(0.0)

def forward(self, neurons):
"""
Calculates the spike trace of each neuron by adding current spike and decaying the trace so far.
"""
neurons.trace += neurons.spikes
neurons.trace -= (neurons.trace / self.tau_s) * neurons.network.dt


class Fire(Behavior):
Expand Down
Loading