4

I am trying to create a simple neural network structure and have the following:

\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,draw=black!80,thick,minimum size=17pt,inner
        sep=0pt]
    \tikzstyle{annot} = [text width=4em, text centered]

    \foreach \name / \y in {1,...,3}
        \path[yshift=0.5cm]
            node[neuron] (H-\name) at (\layersep,-\y cm) {$x_\y$};

    \node[neuron,pin={[pin edge={->}]right:$h_\theta(x)$}, right of=H-2] (O) {};

    \foreach \source in {1,...,3}
        \path (H-\source) edge (O);

\end{tikzpicture}

Which, when rendered, produces the following:

I want to be able to label the \path, but everywhere I've looked talks only about labeling a \draw or \node. I thought I should be able to add:

\path (H-\source) edge (O) {Label Text};

Or set the label attribute in the edge:

\path (H-\source) edge[label=Label Text] (O);

But it doesn't seem to work.

I realize the this segment:

\foreach \source in {1,...,3}
    \path (H-\source) edge (O);

sets the edges. I've also tried creating a new node inside the loop, but wasn't sure what how to position it relative to the edge, because the edge does not have an identifier.

Thanks so much for your help!

  • 1
    Welcome to LaTeX! Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Moriambar Apr 16 '17 at 21:10
  • for example \path (H-\source) edge ["Label Text"] (O); (needed quotes library) or \path (H-\source) edge node[above] {Label Text} (O); ... is this what you looking for? – Zarko Apr 16 '17 at 21:17
8

Look up the syntax of the basic tikz commands in some tutorial. To add labels, you need a node. E.g., you can have arrows with labels by using

\draw[->] (H-\source) -- node[above]{a\source} (O);

or

\path (H-\source) edge node[above]{a\source} (O);

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\def\layersep{2.5cm}
\begin{tikzpicture}%
  [shorten >=1pt,->,draw=black!50, node distance=\layersep,
   every pin edge/.style={<-,shorten <=1pt},
   neuron/.style={circle,draw=black!80,thick,minimum size=17pt,inner
        sep=0pt},
   annot/.style = {text width=4em, text centered}
  ]
  \foreach \name / \y in {1,...,3}
     \node[neuron,yshift=0.5cm] (H-\name) at (\layersep,-\y cm) {$x_\y$};
  \node[neuron,pin={[pin edge={->}]right:$h_\theta(x)$}, right of=H-2] (O) {};
  \foreach \source in {1,...,3}
     \draw[->] (H-\source) -- node[above]{a\source} (O);
\end{tikzpicture}
\end{document}
| improve this answer | |
4

Let me convert my comments to answer:

for example \path (H-\source) edge ["Label Text"] (O); (needed quotes library) or \path (H-\source) edge node[above] {Label Text} (O); ... is this what you looking for?

Consider the first possibilities, library chains and positioning for neurons positioning and correct syntax for this, your MWE becomes:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{chains, positioning, quotes}

\begin{document}
\def\layersep{12mm}
\begin{tikzpicture}[shorten >=1pt, ->, draw=black!50, 
           node distance = \layersep and 2*\layersep,
             start chain = going below,
   every pin edge/.style = {<-,shorten <=1pt},
           neuron/.style = {circle,draw=black!80, thick,
                            minimum size=17pt, inner sep=0pt,
                            on chain},
            annot/.style = {text width=4em, text centered},
every edge quotes/.style = {above,font=\footnotesize}
                    ]
\foreach \y in {1,2,3}
     \node (H-\y) [neuron] {$x_\y$};
\node (O) [neuron,pin={[pin edge={->}]right:$h_\theta(x)$}, 
      right=of H-2] {};
\foreach \y in {1,2,3}
     \draw[->] (H-\y) edge [above,"a\y"] (O);
\end{tikzpicture}
\end{document}

enter image description here

I'm pretty sure, that few days ago I wrote answer on very similar question.

| improve this answer | |

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.