我正在嘗試創建一個簡單的神經網路結構並具有以下內容:
\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[“標籤文字”](O); (需要引號庫) 或 \path (H-\source) 邊緣節點[above] {標籤文字} (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}
我很確定,幾天前我就非常類似的問題寫了答案。