2

I have a neural network diagram drawn by using the package neuralnetwork. I have problem with labeling. The labels from input to hidden layers should be 1,2,3,4,5,6 but the code give me 1,2,3,1,2,3. How can I correct it?

Code:

\documentclass{standalone}
\usepackage{neuralnetwork}
\usetikzlibrary{positioning}
\usepackage{lmodern}

\begin{document}
\newcommand{\biasx}[2]{\ifnum0=#2 1 \else $x_#2$ \fi}
\newcommand{\ys}[2]{$y$}
\newcommand{\linki}[4]{\ifcase#2 $1$ \or $2$ \or $3$ \or $4$ \or $5$ \or $6$ \fi}
\newcommand{\hid}[2]{\ifcase#2 $1$ \or $a_1^{(2)}$ \or $a_2^{(2)}$ \fi}
\newcommand{\linkii}[4]{\ifcase#2 $7$ \or $8$ \or $9$ \fi}

\tikzset{weight/.style = {above,sloped,font=\scriptsize,outer sep=0.7} }

\setdefaultnodetext{\boslar}
    \begin{neuralnetwork} [nodespacing=20mm, layerspacing=25mm, maintitleheight=2.5em, layertitleheight=2.5em,
    height=4, toprow=false, nodesize=17pt, style={}, title={}, titlestyle={}]
% 
    \inputlayer[count=2, bias=true, text=\biasx]
    \hiddenlayer[count=2,bias=true,text=\hid]
            \linklayers[title={},style={weight},labels={\linki},not from={},not to={}]
    \outputlayer[count=1,bias=false,text=\ys]
            \linklayers[title={},style={weight},labels={\linkii},not from={},not to={}]
    \node[right= 0.5cm of L2-1] (y) {$h_\theta\left(\mathbf{x}\right)$};
    \draw[->, thin, draw=black!45](L2-1) edge (y);
%   
    \end{neuralnetwork}    
\end{document}

Output:

enter image description here

1

The package neuralnetwork doesn't have actual documentation, and I don't know if it is a bug or a feature.

However, you can easily solve your problem removing the wrong \linklayers and adding some standard TikZ \draw commands (I've put them within two nested \foreach, for convenience).

\documentclass{standalone}
\usepackage{neuralnetwork}
\usetikzlibrary{positioning}
\usepackage{lmodern}

\newcommand{\biasx}[2]{\ifnum0=#2 1 \else $x_#2$ \fi}
\newcommand{\ys}[2]{$y$}
\newcommand{\hid}[2]{\ifcase#2 $1$ \or $a_1^{(2)}$ \or $a_2^{(2)}$ \fi}
\newcommand{\linkii}[4]{\ifcase#2 $7$ \or $8$ \or $9$ \fi}

\tikzset{
    weight/.style ={above,sloped,font=\scriptsize,outer sep=0.7},
    myar/.style ={->, thin, draw=black!45},
    }

\setdefaultnodetext{\boslar}
\begin{document}
    \begin{neuralnetwork}[nodespacing=20mm, 
        layerspacing=25mm, 
        maintitleheight=2.5em, 
        layertitleheight=2.5em,
        height=4, 
        toprow=false, 
        nodesize=17pt, 
        style={}, 
        title={}, 
        titlestyle={}
        ]
        \inputlayer[count=2, bias=true, text=\biasx]
        \hiddenlayer[count=2,bias=true,text=\hid]
        \outputlayer[count=1,bias=false,text=\ys]
            \linklayers[title={},style={weight},labels={\linkii},not from={},not to={}]
        \node[right= 0.5cm of L2-1] (y) {$h_\theta\left(\mathbf{x}\right)$};
        \draw[myar](L2-1) edge (y);
        \foreach \i in {0,1,2}{%
            \foreach[evaluate=\i as \num using int(2*(\i)+\j)] \j in {1,2}{%
                \draw[myar, shorten > =1pt](L0-\i) edge node[weight, near start, inner sep=0pt]{$\num$} (L1-\j);
                }
            }
    \end{neuralnetwork}    
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.