我试图建立一个稀疏的递归神经网络,总共有100个神经元,每个神经元只随机连接其他10个神经元,权重是从高斯分布中随机提取的,平均值为5e-05标准差。
我知道在Python中,要从高斯分布中提取权重,我可以使用:
np.random.normal(0, 5e-05, (100, 100))但是,怎样才能有效地将每个神经元随机连接到网络中的其他10个神经元?我想这可能可以通过基本的python函数来实现,而不需要使用tensorflow或py手电筒,但我欢迎所有可能的解决方案。
谢谢,
莉莉
发布于 2022-02-27 18:47:11
使用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条边,对角线为0(没有节点自关联)。
https://stackoverflow.com/questions/71273963
复制相似问题