我将如何进行元组解包,以便返回的变量基于二维网格中最近的邻居进行配对?

发布于 2025-01-10 21:53:37 字数 927 浏览 0 评论 0原文

我正在探索这些“神经元”之间的神经网络模拟,如下图所示。我遇到的困难是将每个神经元与其最近的邻居连接起来。我首先想到可以通过元组解包来完成,但是它变得非常复杂。

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?

Connection matrix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2025-01-17 21:53:37

如果我理解正确的话,你希望方形网格中的每个神经元水平、垂直或对角地连接到每个相邻的神经元。
这将完成这项工作:

def connect(side):
    maxcol = side-1
    maxrow = side*maxcol
    neurons = list(range(side**2))
    conns = []
    for col in range(side):
        for row in range(0, side**2, side):
            if col < maxcol: #connect left
                conns.append((neurons[row+col],neurons[row+col+1]))
                if row > 0: #connect left up
                    conns.append((neurons[row+col],neurons[row+col+1-side]))
                if row < maxrow: #connect left down
                    conns.append((neurons[row+col],neurons[row+col+1+side]))
            if row < maxrow: #connect down
                conns.append((neurons[row+col],neurons[row+col+side]))
    return conns

connect(5)
[(0, 1), (0, 6), (0, 5), (5, 6), (5, 1), (5, 11), (5, 10), (10, 11), (10, 6), (10, 16), (10, 15), (15, 16), (15, 11), (15, 21), (15, 20), (20, 21), (20, 16), (1, 2), (1, 7), (1, 6), (6, 7), (6, 2), (6, 12), (6, 11), (11, 12), (11, 7), (11, 17), (11, 16), (16, 17), (16, 12), (16, 22), (16, 21), (21, 22), (21, 17), (2, 3), (2, 8), (2, 7), (7, 8), (7, 3), (7, 13), (7, 12), (12, 13), (12, 8), (12, 18), (12, 17), (17, 18), (17, 13), (17, 23), (17, 22), (22, 23), (22, 18), (3, 4), (3, 9), (3, 8), (8, 9), (8, 4), (8, 14), (8, 13), (13, 14), (13, 9), (13, 19), (13, 18), (18, 19), (18, 14), (18, 24), (18, 23), (23, 24), (23, 19), (4, 9), (9, 14), (14, 19), (19, 24)]

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:

def connect(side):
    maxcol = side-1
    maxrow = side*maxcol
    neurons = list(range(side**2))
    conns = []
    for col in range(side):
        for row in range(0, side**2, side):
            if col < maxcol: #connect left
                conns.append((neurons[row+col],neurons[row+col+1]))
                if row > 0: #connect left up
                    conns.append((neurons[row+col],neurons[row+col+1-side]))
                if row < maxrow: #connect left down
                    conns.append((neurons[row+col],neurons[row+col+1+side]))
            if row < maxrow: #connect down
                conns.append((neurons[row+col],neurons[row+col+side]))
    return conns

connect(5)
[(0, 1), (0, 6), (0, 5), (5, 6), (5, 1), (5, 11), (5, 10), (10, 11), (10, 6), (10, 16), (10, 15), (15, 16), (15, 11), (15, 21), (15, 20), (20, 21), (20, 16), (1, 2), (1, 7), (1, 6), (6, 7), (6, 2), (6, 12), (6, 11), (11, 12), (11, 7), (11, 17), (11, 16), (16, 17), (16, 12), (16, 22), (16, 21), (21, 22), (21, 17), (2, 3), (2, 8), (2, 7), (7, 8), (7, 3), (7, 13), (7, 12), (12, 13), (12, 8), (12, 18), (12, 17), (17, 18), (17, 13), (17, 23), (17, 22), (22, 23), (22, 18), (3, 4), (3, 9), (3, 8), (8, 9), (8, 4), (8, 14), (8, 13), (13, 14), (13, 9), (13, 19), (13, 18), (18, 19), (18, 14), (18, 24), (18, 23), (23, 24), (23, 19), (4, 9), (9, 14), (14, 19), (19, 24)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文