如何在Python中构建有限连接的稀疏循环神经网络?

发布于 2025-01-10 01:16:42 字数 327 浏览 0 评论 0原文

我正在尝试构建一个稀疏循环神经网络,其中总共有 100 个神经元,每个神经元仅与其他 10 个神经元随机连接,权重是从具有 0 均值 5e-05 标准差的高斯分布中随机抽取的。

我知道在 Python 中,要从高斯分布中提取权重,我可以使用:

np.random.normal(0, 5e-05, (100, 100))

但是将每个神经元随机连接到网络中的其他 10 个神经元的有效方法是什么?我想这可能可以通过基本的Python函数来实现,而不需要使用tensorflow或pytorch,但我欢迎所有可能的解决方案。

谢谢,

莉莉

I’m trying to build a sparse recurrent neural network where there are 100 neurons in total, and each neuron is only randomly connected with 10 other neurons, and the weight is randomly drawn from a gaussian distribution with 0 mean 5e-05 standard deviation.

I know that in Python, to drawn weights from Gaussian distribution, I could use:

np.random.normal(0, 5e-05, (100, 100))

But what would be an efficient way to set up each neuron randomly connected to 10 other neurons in the network? I guess this could probably be achieved with basic python functions, without going to tensorflow or pytorch, but I’m welcoming all possible solutions.

Thanks,

Lily

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

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

发布评论

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

评论(1

请恋爱 2025-01-17 01:16:42

使用 numpy 中的 random.choice 功能。

import pandas as pd
import numpy as np

# Create a RNG
rng = np.random.default_rng(10)

n = 100 # Number of nodes
k = 10  # Number of edges per node

# Create an empty connectivity matrix
c = np.zeros((n, n), dtype=bool)

for i in range(c.shape[0]):
    # End when all nodes have the right number of edges
    if np.all(c.sum(axis=1) == k):
        break
        
    # Select more edges from nodes with fewer edges by weighting probability
    p = 1 - c.sum(axis=1) / k
    
    # Set the probability of self-association to zero
    p[i] = 0
    
    # Choose as many edges as needed for this node to bring it up to k
    new_edges = rng.choice(np.arange(n), 
                           size=k - c[i, :].sum(), 
                           p=p / np.sum(p), 
                           replace=False)
    
    # Add the randomly selected edges for this node to a symmetric connectivity matrix
    c[i, new_edges] = True
    c[new_edges, i] = True

这给出了一个连接矩阵,其中所有行和列的总和为 10 个边,并且对角线为零(没有节点自关联)。

Use the random.choice functionality from numpy.

import pandas as pd
import numpy as np

# Create a RNG
rng = np.random.default_rng(10)

n = 100 # Number of nodes
k = 10  # Number of edges per node

# Create an empty connectivity matrix
c = np.zeros((n, n), dtype=bool)

for i in range(c.shape[0]):
    # End when all nodes have the right number of edges
    if np.all(c.sum(axis=1) == k):
        break
        
    # Select more edges from nodes with fewer edges by weighting probability
    p = 1 - c.sum(axis=1) / k
    
    # Set the probability of self-association to zero
    p[i] = 0
    
    # Choose as many edges as needed for this node to bring it up to k
    new_edges = rng.choice(np.arange(n), 
                           size=k - c[i, :].sum(), 
                           p=p / np.sum(p), 
                           replace=False)
    
    # Add the randomly selected edges for this node to a symmetric connectivity matrix
    c[i, new_edges] = True
    c[new_edges, i] = True

This gives a connectivity matrix where all the rows and columns sum to 10 edges, and the diagonal is zero (no nodes self-associate).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文