我将如何进行元组解包,以便返回的变量基于二维网格中最近的邻居进行配对?
我正在探索这些“神经元”之间的神经网络模拟,如下图所示。我遇到的困难是将每个神经元与其最近的邻居连接起来。我首先想到可以通过元组解包来完成,但是它变得非常复杂。
def _connect_cells(self):
for source, target in zip(self.cells, self.cells[1:] + [self.cells[0]]):
nc = h.NetCon(source.soma(0.5)._ref_v, target.syn, sec=source.soma)
nc.weight[0] = self._syn_w
nc.delay = self._syn_delay
source._ncs.append(nc)
在此示例代码片段中,元组解包配置为第 i 个神经元连接到第 i + 1 个神经元,直到第 n 个神经元。当它到达神经元n时,第n个神经元将连接回第一个神经元。这种元组拆包适用于类似于神经元环的网络结构。
然而,就我而言,该结构是一个由 nx n 神经元组成的网格。下面的列表对应于神经元:
20 21 22 23 24
15 16 17 18 19
10 11 12 13 14
5 6 7 8 9
0 1 2 3 4
上面的元组解包对此不起作用,因为神经元 4 不应该连接到神经元 5。由于神经元的创建方式,该列表完全从左到右。我试图实现的确切连接如下图所示。可以手动执行此操作(这将需要大量代码),但是如何以与示例代码相同的方式使用 for 循环来实现它?
I am exploring neural network simulations between these "neurons" that you see in the figure below. The difficulty I have is connecting each neuron to its nearest neighbor. I first figured that I could do it by tuple unpacking, but it has become very complicated.
def _connect_cells(self):
for source, target in zip(self.cells, self.cells[1:] + [self.cells[0]]):
nc = h.NetCon(source.soma(0.5)._ref_v, target.syn, sec=source.soma)
nc.weight[0] = self._syn_w
nc.delay = self._syn_delay
source._ncs.append(nc)
In this example code snippet, the tuple unpacking is configured such that the i th neuron connects to i + 1 th neuron until neuron n. When it reaches neuron n, the n th neuron will connect back to the first neuron. This tuple unpacking is for a network structure resembling a ring of neurons.
However, in my case, the structure is a grid of n x n neurons. The list bellow corresponds to the neurons:
20 21 22 23 24
15 16 17 18 19
10 11 12 13 14
5 6 7 8 9
0 1 2 3 4
The above tuple unpacking would not work for this because neuron 4 is not supposed to connect to neuron 5. The list goes exclusively left to right due to how the neurons are created. The exact connection that I am trying to achieve is shown in the figure below. It is possible to do it manually (it would take a lot of code), but how can I approach it with a for loop in the same way as the example code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,你希望方形网格中的每个神经元水平、垂直或对角地连接到每个相邻的神经元。
这将完成这项工作:
If I understand correctly you want each neuron in a square grid connect to each neighbouring neuron, horizontally, vertically or diagonally.
This will do the job: