BindsNET / bindsnet

Simulation of spiking neural networks (SNNs) using PyTorch.
GNU Affero General Public License v3.0
1.5k stars 331 forks source link

how to create feedback connections in bindsnet? #332

Closed ati6868 closed 5 years ago

ati6868 commented 5 years ago

Hi I'm trying to create a network of two layers while the network has feedforward and feedback connections feedback_connection = Connection( source=target_layer, target=source_layer, w=0.05 + 0.1 * torch.randn(target_layer.n, source_layer.n), )

but it shows me this error: network.run(inpts=inpts, time=time) File "C:\python\Python37\lib\site-packages\bindsnet\network\network.py", line 383, in run inpts.update(self._get_inputs()) File "C:\python\Python37\lib\site-packages\bindsnet\network\network.py", line 230, in _get_inputs inpts[c[1]] += self.connections[c].compute(source.s) File "C:\python\Python37\lib\site-packages\bindsnet\network\topology.py", line 188, in compute post = s.float().view(s.size(0), -1) @ self.w + self.b RuntimeError: size mismatch, m1: [100 x 1], m2: [100 x 1000] at C:\w\1\s\windows\pytorch\aten\src\TH/generic/THTensorMath.cpp:197

bindsnet

i would be grateful if someone could help me on this

djsaunde commented 5 years ago

Hi @ati6868, at the moment, it makes no sense to connect to a layer of Input nodes, they simply discard their input. Input nodes spikes are set by the user by passing them to network.run(inputs=...).

You could instead create a network with a layer of Input nodes (A), connected to a layer of LIFNodes (B), which is recurrently connected to itself, or connected to a third layer of LIFNodes (C) which connects back to B.

It might be good to support adding support for your use case, though; I'll look into it.

ati6868 commented 5 years ago

thank you @djsaunde for your response . my model is supposed to use predictive coding that's why i need to use feedback connections for propagating predictions of lower layers generated by upper layers . if it's not possible , Could i have two layers of LIFNodes instead of " one layer of Input nodes and one layer of LIFNodes " , while i need to use mnist images for my model? i mean is that possible that first layer is LIFNodes that receives mnist images and second layer is a LIFNodes too and then it connects from second layer to first layer?

djsaunde commented 5 years ago

No, that wouldn't work either, since inputs to LIFNodes can't be provided by passing them to network.run(...); they are generated during the simulation.

I will look into refactoring the Input nodes to support your use case.

ati6868 commented 5 years ago

thank you very much @djsaunde just how can i be informed when u do that because i'm working on my project with bindsnet??

ati6868 commented 5 years ago

By the way ....As you said that layer of Input nodes works based on user-specified spiking behavior. and i'm going to feed pre-defined spike trains from layer 2 to layer 1(input)....then why i can't do this?

dee0512 commented 5 years ago

@ati6868 The input layer takes a pre-defined spike train and outputs it. I do not think you want to do that. The input layer simply provides a way to input spikes into a network. For your network, therefore, you would want 3 layers as @djsaunde suggested:

You could instead create a network with a layer of Input nodes (A), connected to a layer of LIFNodes (B), which is recurrently connected to itself, or connected to a third layer of LIFNodes (C) which connects back to B.

To elaborate, the Input nodes (A) will be connected on a one to one basis to the LIFNodes, thus ensuring that the first LIFNodes (B) receive the MNIST images. (B) is then connected to a third layer of LIFNodes (C) which connects back to the lower layer (B) creating a feedback. Therefore, essentially, your network just contains 2 layers (B) and (C), the input nodes just provide a way to input the MNIST images as spikes into your network.

ati6868 commented 5 years ago

thank you very much @dee0512 .... you always help me a lot As you said i need a one to one connection between input layer(A) and layer(B) 1-number of nodes in both layers(A, B) should be 784 for mnist images? 2-Do the connections between (A) and (B) have weights? and spikes that are delivered to layer (B) , are transformed by W*X+b? because i don't want any change before delivering to layer (B) Or it is just delivering images in form of poisson spike trains to layer(B)? I will thank you if you clarify me on this i also attached what i guess aaaaaaaaaaaa

djsaunde commented 5 years ago

@ati6868 That should work. Just set the weights from layer1 to layer2 as torch.ones(784, 784). so the spikes are unmodified and are simply transmitted.

ati6868 commented 5 years ago

OKay thank you I will try it 👍

dee0512 commented 5 years ago

I think for one to one connection you want torch.eye(784) instead of torch.ones(784, 784)

ati6868 commented 5 years ago

yes @dee0512 offcourse ...i did it with torch.eye(784)..you're right because with torch.ones(784,784) it will be fully connected...thank you