我正在尝试创建一个简单的神经网络结构,并具有以下内容:
\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}
渲染后,会产生以下内容:
我希望能够标记\path
,但是我所到之处只讨论了标记 a\draw
或\node
。我认为我应该能够添加:
\path (H-\source) edge (O) {Label Text};
或者label
在边缘设置属性:
\path (H-\source) edge[label=Label Text] (O);
但它似乎不起作用。
我意识到这个片段:
\foreach \source in {1,...,3}
\path (H-\source) edge (O);
设置边缘。我也尝试node
在循环内创建一个新的,但不确定如何将其相对于边缘定位,因为边缘没有标识符。
非常感谢你的帮助!
答案1
在一些教程中查找基本 tikz 命令的语法。要添加标签,您需要一个node
。例如,您可以使用
\draw[->] (H-\source) -- node[above]{a\source} (O);
或者
\path (H-\source) edge node[above]{a\source} (O);
\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}
答案2
让我将我的评论转化为答案:
例如 \path (H-\source) edge ["Label Text"] (O); (需要引号库) 或 \path (H-\source) edge node[above] {Label Text} (O); ...这是您要找的吗?
考虑第一种可能性,库chains
和positioning
神经元的定位以及正确的语法,你的 MWE 变成:
\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}
我很确定,几天前我曾对非常相似的问题写过答案。