如何在graphviz中修复节点相对顺序?

时间:2017-05-22 05:54:36

标签: graphviz

我正在尝试在graphviz中绘制神经网络。这是我目前的照片:

neural network

如您所见,节点标签的顺序无法正确显示。例如,我希望$ a_0 ^(2)$坚持到第2层的顶部,我想在数字递增顺序中安排同一层中的其余节点(即$ a_1 ^(2)$,$ a_2 ^( 2)$,...)

我该怎么做?非常感谢!

这是我的代码:

digraph G {
    rankdir=LR;
    splines=line;
    nodesep=".05";
    edge [comment="Wildcard node added automatic in EG."];
    node [label=""];
    subgraph cluster_0 {
        color=white;
        label="layer 1 (input layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=chartreuse, 
              style=filled, 
              shape=circle];
        x0 [color=yellow, 
            fillcolor=yellow, 
            label=<x<sub>0</sub>>];
        x1 [fillcolor=chartreuse, 
            label=<x<sub>1</sub>>];
        x2 [fillcolor=chartreuse, 
            label=<x<sub>2</sub>>];
        x3 [fillcolor=chartreuse, 
            label=<x<sub>3</sub>>];
    }

    subgraph cluster_1 {
        color=white;
        label="layer 2 (hidden layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=dodgerblue, 
              style=filled, 
              shape=circle];
        a02 [color=yellow, 
             label=<a<sub>0</sub><sup>(2)</sup>>, 
             pos="0,0", 
             fillcolor=yellow];
        a12 [label=<a<sub>1</sub><sup>(2)</sup>>, 
             pos="0,-1", 
             fillcolor=dodgerblue];
        a22 [label=<a<sub>2</sub><sup>(2)</sup>>, 
             pos="0,-2", 
             fillcolor=dodgerblue];
        a32 [label=<a<sub>3</sub><sup>(2)</sup>>, 
             pos="0,-3", 
             fillcolor=dodgerblue];
        a42 [label=<a<sub>4</sub><sup>(2)</sup>>, 
             pos="0,-4", 
             fillcolor=dodgerblue];
        a52 [label=<a<sub>5</sub><sup>(2)</sup>>, 
             pos="0,-5", 
             fillcolor=dodgerblue];
    }

    subgraph cluster_2 {
        color=white;
        label="layer 3 (hidden layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=dodgerblue, 
              style=filled, 
              shape=circle];
        a03 [color=yellow, 
             fillcolor=yellow, 
             label=<a<sub>0</sub><sup>(3)</sup>>];
        a13 [fillcolor=dodgerblue, 
             label=<a<sub>1</sub><sup>(3)</sup>>];
        a23 [fillcolor=dodgerblue, 
             label=<a<sub>2</sub><sup>(3)</sup>>];
        a33 [fillcolor=dodgerblue, 
             label=<a<sub>3</sub><sup>(3)</sup>>];
        a43 [fillcolor=dodgerblue, 
             label=<a<sub>4</sub><sup>(3)</sup>>];
        a53 [fillcolor=dodgerblue, 
             label=<a<sub>5</sub><sup>(3)</sup>>];
    }

    subgraph cluster_3 {
        color=white;
        label="layer 4 (output layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=coral1, 
              style=filled, 
              shape=circle];
        O1 [fillcolor=coral1, 
            label=<a<sub>1</sub><sup>(4)</sup>>];
        O2 [fillcolor=coral1, 
            label=<a<sub>2</sub><sup>(4)</sup>>];
        O3 [fillcolor=coral1, 
            label=<a<sub>3</sub><sup>(4)</sup>>];
        O4 [fillcolor=coral1, 
            label=<a<sub>4</sub><sup>(4)</sup>>];
    }

    x0 -> a12;
    x0 -> a22;
    x0 -> a32;
    x0 -> a42;
    x0 -> a52;
    x1 -> a12;
    x1 -> a22;
    x1 -> a32;
    x1 -> a42;
    x1 -> a52;
    x2 -> a12;
    x2 -> a22;
    x2 -> a32;
    x2 -> a42;
    x2 -> a52;
    x3 -> a12;
    x3 -> a22;
    x3 -> a32;
    x3 -> a42;
    x3 -> a52;
    a02 -> a13;
    a02 -> a23;
    a02 -> a33;
    a02 -> a43;
    a02 -> a53;
    a12 -> a13;
    a12 -> a23;
    a12 -> a33;
    a12 -> a43;
    a12 -> a53;
    a22 -> a13;
    a22 -> a23;
    a22 -> a33;
    a22 -> a43;
    a22 -> a53;
    a32 -> a13;
    a32 -> a23;
    a32 -> a33;
    a32 -> a43;
    a32 -> a53;
    a42 -> a13;
    a42 -> a23;
    a42 -> a33;
    a42 -> a43;
    a42 -> a53;
    a52 -> a13;
    a52 -> a23;
    a52 -> a33;
    a52 -> a43;
    a52 -> a53;
    a03 -> O1;
    a13 -> O1;
    a23 -> O1;
    a33 -> O1;
    a43 -> O1;
    a53 -> O1;
    a03 -> O2;
    a13 -> O2;
    a23 -> O2;
    a33 -> O2;
    a43 -> O2;
    a53 -> O2;
    a03 -> O3;
    a13 -> O3;
    a23 -> O3;
    a33 -> O3;
    a43 -> O3;
    a53 -> O3;
    a03 -> O4;
    a13 -> O4;
    a23 -> O4;
    a33 -> O4;
    a43 -> O4;
    a53 -> O4;
}

2 个答案:

答案 0 :(得分:2)

我不认为更换标签是一个很好的解决方案。

如果每个层中节点的排序很重要,我建议在每个层中添加不可见边,强制节点的顺序。

这样的事情:

digraph G {
    rankdir = LR;
    splines=false;
    edge[style=invis];
    ranksep= 1.4;
    {
        rank=same;
        x0->x1->x2->x3;
    }
    {
        rank=same;
        a0->a1->a2->a3->a4;
    }
    {
        rank=same;
        b0->b1->b2->b3->b4;
    }
    {
        rank=same;
        c0->c1->c2->c3;
    }

    edge[style=solid, tailport=e, headport=w];
    {x0; x1; x2; x3} -> {a0;a1;a2;a3;a4};
    {a0;a1;a2;a3;a4} -> {b0;b1;b2;b3;b4};
    {b0;b1;b2;b3;b4} -> {c0,c1,c2,c3};
}

答案 1 :(得分:1)

所以,我使用一些肮脏的技巧来获得以下图片:

nn-work

  1. 我使用以下代码连接所有节点,如下所示:

    x0 -> a02 [style=invisible, dir=none];

  2. 我根据图片中显示的每个节点位置重新标记节点文本(这是非常脏的)

  3. 因此,我的完整代码可在here处找到。

    如果有更好的解决方案,请告诉我。