TransWikia.com

Unwanted Extra Circles in Tikz Neural Network (LaTeX)

TeX - LaTeX Asked by usr1729 on February 18, 2021

Using this code, the output is as follows. Why are there extra empty circles appearing in my input layer and how do I get rid of them?

Bonus points: how could I go about overlaying weight and bias matrices in between the layers?

    begin{figure}
    begin{tikzpicture}[
         % define styles 
     clear/.style={ 
         draw=none,
         fill=none
     },
     net/.style={
         matrix of nodes,
         nodes={ draw, circle, inner sep=10pt },
         %nodes in empty cells,
         column sep=2cm,
         row sep=-9pt
     },
     >=latex
]


matrix[net] (mat)
{
|[clear]| parbox{1.3cm}{centering Inputlayer} 
    & |[clear]| parbox{1.3cm}{centering Hiddenlayer 1} 
    & |[clear]| parbox{1.3cm}{centering Hiddenlayer 2} 
    & |[clear]| parbox{1.3cm}{centering Outputlayer} 
         
    & 10 (on)   

2  &                 & 13 (on) 

     & 0 (off)   &                  & 1 

1  &                 & 0 (off)        

    & 1 (on)     
}
;

foreach ai in {3, 5} {
            foreach aii in {2,4,6}
                draw[->] (mat-ai-1) -- (mat-aii-2);
                }
                
foreach ai in {2, 4, 6} {
            foreach aii in {3, 5}
                draw[->] (mat-ai-2) -- (mat-aii-3);
                }
                
foreach ai in {3, 5} {
            foreach aii in {4}
                draw[->] (mat-ai-3) -- (mat-aii-4);
                }
end{tikzpicture}
end{figure}

enter image description here

One Answer

Culprit are empty lines in your matrix. Delete them. Off.topic, you can make more concise code, for example as follows:

documentclass[tikz, margin=3mm]{standalone}
usetikzlibrary{matrix}

begin{document}
    begin{tikzpicture}[
         % define styles
net/.style={matrix of nodes,
            nodes = {circle, draw, text width=2.4em, align=center,
                     inner sep=0pt, anchor=center},
            row 1/.style = {nodes={rectangle, draw=none,
                            text width=4em, align=center,
                            inner sep=0pt, anchor=center}},
            column sep=12mm,
            row sep=6pt
            },
     >=latex
                    ]
matrix[net] (mat)
{
Input layer
    & Hidden layer 1
                & Hidden layer 2
                            & Output layer 
    & 10 (on)   &           &   
2   &           & 13 (on)   &   
    & 0 (off)   &           & 1 
1   &           & 0 (off)   &   
    & 1 (on)    &           &   
};

foreach ai in {3, 5} {
            foreach aii in {2,4,6}
                draw[->] (mat-ai-1) -- (mat-aii-2);
                }

foreach ai in {2, 4, 6} {
            foreach aii in {3, 5}
                draw[->] (mat-ai-2) -- (mat-aii-3);
                }

foreach ai in {3, 5} {
            foreach aii in {4}
                draw[->] (mat-ai-3) -- (mat-aii-4);
                }
    end{tikzpicture}
end{document}

produce

enter image description here

Answered by Zarko on February 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP