【问题标题】:graphviz python neural network layer alignmentgraphviz python 神经网络层对齐
【发布时间】:2018-08-14 03:10:54
【问题描述】:

我正在尝试在 MLP 网络中绘制特定的权重连接。我想要实现的是

简单的 graphviz 绘制 倾斜 图表 w.r.t.绘制边缘

from graphviz import Graph

graph = Graph(directory='graphs', format='png',
              graph_attr=dict(ranksep='2', rankdir='LR', color='white', splines='line'),
              node_attr=dict(label='', shape='circle', width='0.1'))


def draw_cluster(name, length):
    with graph.subgraph(name=f'cluster_{name}') as c:
        c.attr(label=name)
        for i in range(length):
            c.node(f'{name}_{i}')


draw_cluster('input', 10)
draw_cluster('output', 4)

source_active = [0, 1, 2, 3]
sink_active = [2, 3]

for i_input in source_active:
    for i_output in sink_active:
        graph.edge(f'input_{i_input}', f'output_{i_output}')

graph.view()

如果我在未连接的权重之间添加不可见的边,我会强制我的图形居中。

for i_input in set(range(10)).difference(source_active):
    for i_output in set(range(4)).difference(sink_active):
        graph.edge(f'input_{i_input}', f'output_{i_output}', style='invis')

但是要付出什么代价!我的层可以有超过 1000 个神经元,只有几十个连接。或许networkx能帮上忙,我没玩过。

对我没有帮助的类似问题:

  1. cluster location in graphviz python
  2. GraphViz - alignment of subgraph

【问题讨论】:

  • 使用networkx,您可以创建字典pos,以便pos[node]node(x,y) 坐标。然后用nx.draw(G, pos)画出来。

标签: python networkx graphviz


【解决方案1】:

我找到了解决方法。诀窍是您不必建立所有连接来使图形居中。只连接每一层的中心层就足够了:

def central_neurons(layer_size: int):
    if layer_size % 2 == 1:
        return {layer_size // 2}
    else:
        return {layer_size // 2, layer_size // 2 - 1}

for source_id in central_neurons(layer_size=source_size):
    for sink_id in central_neurons(layer_size=sink_size):
        graph.edge(f'input_{source_id}', f'output_{sink_id}', constraint='true', style='invis')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 2016-09-02
    • 2012-09-22
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多