如何可视化神经网络的神经元节点之间的连接?
1 导出表格
将网络的权重矩阵导出为三个表格
(1)nodes.csv
一列表示,为每个节点的编码
node |
---|
node1 |
node2 |
node3 |
node4 |
node5 |
node6 |
node7 |
node8 |
node9 |
(2)links.csv
这个表格表示节点的连接,第一列为顺序编码,唯一表示,第二列是节点的起点,第二个为节点的终点
id | source | target |
---|---|---|
1 | node1 | node2 |
2 | node1 | node3 |
3 | node1 | node4 |
4 | node1 | node7 |
5 | node1 | node8 |
6 | node1 | node10 |
7 | node1 | node13 |
8 | node1 | node14 |
9 | node1 | node16 |
10 | node1 | node19 |
11 | node1 | node20 |
12 | node1 | node26 |
13 | node1 | node29 |
14 | node1 | node38 |
(3)node_class.csv
这个表格表示每个节点的类别,方便后续对不同的节点绘制不同的颜色
id | class |
---|---|
node1 | hub node |
node2 | hub node |
node3 | hub node |
node4 | non-hub node |
node5 | hub node |
node6 | hub node |
node7 | hub node |
node8 | non-hub node |
import pandas as pd
# 将小数值的权重矩阵转为0-1表示的矩阵
def convert_to_binary(matrix):
binary_matrix = []
for row in matrix:
binary_row = [1 if value != 0 else 0 for value in row]
binary_matrix.append(binary_row)
return np.array(binary_matrix)
def generate_data(W):
adj_binary_matrix = convert_to_binary(W)
# 初始化邻接矩阵
adj_matrix = adj_binary_matrix
# 获取节点数量
num_nodes = adj_matrix.shape[0]
# 生成节点编码
node_codes = ['node' + str(i+1) for i in range(num_nodes)]
# 创建 nodes.csv 文件
nodes_df = pd.DataFrame(node_codes, columns=['node'])
# 创建 links.csv 文件
links = []
id = 1
for i in range(num_nodes):
for j in range(num_nodes):
if adj_matrix[i][j] == 1:
links.append((id, node_codes[i], node_codes[j]))
id+=1
links_df = pd.DataFrame(links, columns=['id', 'source', 'target'])
links_df.to_csv(f'links.csv', index=False)
nodes_df.to_csv(f'nodes.csv', index=False)
# 对网络不同度的节点分类,分为高度节点((hub))和非高度节点(non-hub)
# 计算每个节点的度
degrees = np.sum(adj_matrix != 0, axis=1)
# 定义度的连接比例为threshold的为hub节点
threshold = 0.7
threshold_degree = np.quantile(degrees,threshold)
# 根据节点的度将它们分为两类
node_classes = ['hub node' if degree >= threshold_degree else 'non-hub node' for degree in degrees]
# 将结果保存到名为node_class.csv的表格中
result = pd.DataFrame({'id': node_codes, 'class': node_classes})
result.to_csv('node_class.csv', index=False)
# 假设神经网络每个神经元之间的邻接矩阵
# 初始化一个10*10的邻接矩阵,初始值全为0
adj_matrix = [
[-0.000000,0.000000,0.000000,0.000000,0.000000,-0.000000,0.122116,-0.029914,0.364874,0.000000],
[0.000000,-0.000000,-0.003972,-0.035493,-0.412647,-0.000000,-0.000000,-0.000000,-0.000000,-0.000000],
[-0.000000,-0.086977,-0.000000,0.190105,0.000000,-0.153798,-0.000000,0.189070,0.000000,-0.000000],
[-0.000000,0.368288,-0.000000,-0.000000,-0.000000,0.057519,0.000000,-0.000000,0.000000,-0.000000],
[0.000000,-0.000000,-0.214501,0.000000,-0.443936,0.000000,0.000000,0.000000,-0.000000,-0.000000],
[-0.000000,-0.172708,0.052662,-0.184544,0.000000,0.024721,0.000000,-0.300713,-0.414200,0.000000],
[-0.137718,0.000000,0.000000,-0.000000,-0.000000,0.258740,0.099708,0.000000,0.032948,-0.190810],
[-0.000000,-0.106000,0.195423,0.000000,-0.000000,0.029322,-0.000000,0.077991,0.000000,-0.000000],
[0.252584,-0.077399,-0.000000,-0.000000,0.000000,-0.338475,0.498515,0.000000,-0.238516,0.597768],
[-0.000000,0.000000,0.048298,-0.000000,-0.000000,0.000000,0.016180,-0.000000,0.176588,0.000000]]
generate_data(adj_matrix)
2 绘图
免费从Chiplot网络绘制,导入以上生成的三个表格,网站有具体的教程
ChiPlot网站