首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中构造具有有限连接的稀疏递归神经网络?

如何在Python中构造具有有限连接的稀疏递归神经网络?
EN

Stack Overflow用户
提问于 2022-02-26 03:55:12
回答 1查看 67关注 0票数 0

我试图建立一个稀疏的递归神经网络,总共有100个神经元,每个神经元只随机连接其他10个神经元,权重是从高斯分布中随机提取的,平均值为5e-05标准差。

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

代码语言:javascript
复制
np.random.normal(0, 5e-05, (100, 100))

但是,怎样才能有效地将每个神经元随机连接到网络中的其他10个神经元?我想这可能可以通过基本的python函数来实现,而不需要使用tensorflow或py手电筒,但我欢迎所有可能的解决方案。

谢谢,

莉莉

EN

回答 1

Stack Overflow用户

发布于 2022-02-27 18:47:11

使用numpy中的random.choice功能。

代码语言:javascript
复制
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条边,对角线为0(没有节点自关联)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71273963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档