使用 tikz 绘制神经网络图

使用 tikz 绘制神经网络图

我想使用 tikZ 设计以下两个神经网络,有没有已经写好的数据包?

在此处输入图片描述

在此处输入图片描述

答案1

这是给你一个开始。恕我直言,你不需要包,只需为括号包裹的内容定义一个图片即可。其用法示例:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fit,positioning}
\tikzset{pics/paren/.style={code={%
\tikzset{paren/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/paren/##1}}%
\node[fit/.expanded=\pv{fit},inner xsep=\pv{xsep},inner ysep=\pv{ysep}] (\pv{aux}){};
\draw[rounded corners=\pv{arc}] (\pv{aux}.north west) |- 
([yshift=\pv{arc}]\pv{aux}.north) node[above] {\pv{upper label}}-| (\pv{aux}.north east);
\draw[rounded corners=\pv{arc}] (\pv{aux}.south west) |- 
([yshift=-\pv{arc}]\pv{aux}.south) node[below] {\pv{lower label}}-| 
(\pv{aux}.south east);
}},
paren/.cd,upper label/.initial={},lower label/.initial={},xsep/.initial=1em,ysep/.initial=1em,
fit/.initial={},aux/.initial=aux,arc/.initial=4pt}
\begin{document}
\begin{tikzpicture}[font=\sffamily,node distance=3em]
  \node[fill,minimum width=1em,minimum height=4em,label=below:4](A){};
  \node[right=of A,draw,thick,node font=\bfseries\large,
    label=below:{$\scriptstyle 2\times4$}](B) {W};
  \node[right=of B,draw,thick,minimum width=1.5em,minimum height=4em,
    label=below:4,path picture={\draw (180:1ex) -- (0:1ex) 
    (-135:1ex) -- (45:1ex); }](C) {};
  \draw[thick,-latex] (A) --  node[above,node font=\bfseries]{p} 
   node[below]{$\scriptstyle 4\times1$} (B);
  \draw[thick,-latex] (B) --  node[above,node font=\bfseries]{n} 
   node[below]{$\scriptstyle 2\times1$} (C);
  \draw[thick,-latex] (C.east) --  node[above,node font=\bfseries]{a} 
   node[below]{$\scriptstyle 2\times1$} ++ (3em,0);
  \path pic{paren={fit={(A)},upper label=Inputs}}
  pic{paren={fit={(B)(C)},upper label=Linear Layer,
  lower label={$\mathbf{a}=\mathbf{purelin}(\mathbf{W})$}}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容