fangwei123456 / spikingjelly

SpikingJelly is an open-source deep learning framework for Spiking Neural Network (SNN) based on PyTorch.
https://spikingjelly.readthedocs.io
Other
1.21k stars 233 forks source link

Set initial value for membrane potential #529

Open JackCaster opened 2 months ago

JackCaster commented 2 months ago

Issue type

SpikingJelly version 0.0.0.0.15

Description Hi @fangwei123456, in a custom neuron, is it possible to add a parameter to set the initial value for v? Instead of starting from 0.0, I'd like to let the network figure out what is the best starting value. I'd need this as I am looking at fitting some timeseries that I chunked (so later chunks may be more likely to have already a starting value for the membrane potential). How would you go about it?

Met4physics commented 2 months ago

You can write a function which make a traversal in your model to set initial membrane potential. For example:

def set_initial(model, value):
    for module in model.modules():
        if isinstance(module, neuron):
            module.v = value
JackCaster commented 2 months ago

You can write a function which make a traversal in your model to set initial membrane potential. For example:

def set_initial(model, value):
    for module in model.modules():
        if isinstance(module, neuron):
            module.v = value

Thanks, this is close to what I was looking for! But I was wondering if it would be possible to have the initial value being a parameter to learn

Met4physics commented 2 months ago

You can write a function which make a traversal in your model to set initial membrane potential. For example:


def set_initial(model, value):

    for module in model.modules():

        if isinstance(module, neuron):

            module.v = value

Thanks, this is close to what I was looking for! But I was wondering if it would be possible to have the initial value being a parameter to learn

Then you need to use nn.parameter to represent the initial value.

JackCaster commented 2 months ago

Yes exactly, I think I need a parameter v_init that by default is set to 0.0. Where is the right place to add this parameter. I usually subclass the base neuron to add these kind of changes, but I struggle to find the right way to add this parameter in there.

Met4physics commented 2 months ago

Yes exactly, I think I need a parameter v_init that by default is set to 0.0. Where is the right place to add this parameter. I usually subclass the base neuron to add these kind of changes, but I struggle to find the right way to add this parameter in there.

Just like this:

class Neuron():
    def __init__(self):
        self.init_v = nn.parameter([1])

Note that this code is only a draft for illustration. And you should also write a new reset function to set v to be equal with v_init after a timestep.