我实际上正在努力绘制一个使用 MS PowerPoint 制作的自定义神经网络图。我的上一个问题但我仍然无法解决一些问题,无法复制我拥有的图形。
预期的:
目前,我能解决这个问题到这个程度。这是我的尝试。但是有以下错误:
- 如何为隐藏层添加点(显示中间的几个隐藏层)
- 如何使输出节点仅变为 1,并将其标签更改为总和(符号),
当前的:
\documentclass[border=3pt,tikz]{standalone}
\usepackage{tikz}
\usepackage{etoolbox} % for \ifnumcomp
\usepackage{listofitems} % for \readlist to create arrays
\tikzset{>=latex} % for LaTeX arrow head
\colorlet{mywhite}{white!80!black}
\colorlet{myred}{red!80!black}
\colorlet{myblue}{blue!80!black}
\colorlet{mygreen}{green!60!black}
\colorlet{mydarkred}{myred!40!black}
\colorlet{mydarkblue}{myblue!40!black}
\colorlet{mydarkgreen}{mygreen!40!black}
\tikzstyle{node}=[very thick,circle,draw=myblue,minimum size=22,inner sep=0.5,outer sep=0.6]
\tikzstyle{connect}=[->,thick,mydarkblue,shorten >=1]
\tikzset{ % node styles, numbered for easy mapping with \nstyle
node 1/.style={node,mydarkgreen,draw=mywhite,fill=mywhite!25},
node 2/.style={node,mydarkblue,draw=mywhite,fill=mywhite!20},
node 3/.style={node,mydarkred,draw=mywhite,fill=mywhite!20},
}
\def\nstyle{int(\lay<\Nnodlen?min(2,\lay):3)} % map layer number onto 1, 2, or 3
\begin{document}
% NEURAL NETWORK
\begin{tikzpicture}[x=2.4cm,y=1.2cm]
\readlist\Nnod{4,3,2} % array of number of nodes per layer
\readlist\Nstr{n,m,k} % array of string number of nodes per layer
\readlist\Cstr{x,h^{(\prev)},y} % array of coefficient symbol per layer
\def\yshift{0.55} % shift last node for dots
% LOOP over LAYERS
\foreachitem \N \in \Nnod{
\def\lay{\Ncnt} % alias of index of current layer
\pgfmathsetmacro\prev{int(\Ncnt-1)} % number of previous layer
\foreach \i [evaluate={\c=int(\i==\N); \y=\N/2-\i-\c*\yshift;
\x=\lay; \n=\nstyle;
\index=(\i<\N?int(\i):"\Nstr[\n]");}] in {1,...,\N}{ % loop over nodes
% NODES
\node[node \n] (N\lay-\i) at (\x,\y) {$\strut\Cstr[\n]_{\index}$};
% CONNECTIONS
\ifnumcomp{\lay}{>}{1}{ % connect to previous layer
\foreach \j in {1,...,\Nnod[\prev]}{ % loop over nodes in previous layer
\draw[white,line width=1.2,shorten >=1] (N\prev-\j) -- (N\lay-\i);
\draw[connect] (N\prev-\j) -- (N\lay-\i);
}
\ifnum \lay=\Nnodlen
\draw[connect] (N\lay-\i) --++ (0.5,0); % arrows out
\fi
}{
\draw[connect] (0.5,\y) -- (N\lay-\i); % arrows in
}
}
\path (N\lay-\N) --++ (0,1+\yshift) node[midway,scale=1.6] {$\vdots$}; % dots
}
% LABELS
\node[above=3,align=center,mydarkgreen] at (N1-1.90) {Input\\[-0.2em]layer};
\node[above=2,align=center,mydarkblue] at (N2-1.90) {Hidden\\[-0.2em]layers};
\node[above=3,align=center,mydarkred] at (N\Nnodlen-1.90) {Output\\[-0.2em]layer};
\begin{scope}[scale=0.75,xshift=18cm,yshift=-2cm] % <---- HERE
\draw[blue!50!black] plot[domain={-1.5:5}] (\x,{sin(\x^2 r)});
\draw[blue!50!black, densely dotted] (-2,0) -- (5,0);
\draw[black, densely dashed] (0,0)
-- ++(0,-2) node[below] {$x_1$} coordinate[pos=0] (A);
\draw[black, densely dashed] (0.5,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[black, densely dashed] (1.0,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[black, densely dashed] (1.5,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[black, densely dashed] (2.0,0)
-- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
\draw[thick, <->] (-2,4)
-- node[left, midway, anchor=south, rotate=90, font=\bfseries] (Y) {$f(x)$} (-2,-2)
-- node[below, pos=1, font=\bfseries] {Quad. points} (5,-2);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{positioning, calc, arrows.meta}
\begin{document}
\begin{tikzpicture}[
state/.style={draw, circle, minimum size=0.8cm, inner sep=0pt},
arrow/.style={-Stealth, shorten >=1pt},
dot/.style={minimum size=4pt, inner sep=0pt, rounded corners=1pt, fill},
node distance=0.1cm and 2cm,
]
\node[state] (y1) {$y_1$};
\node[state, below=of y1] (y2) {$y_2$};
\node[state, below=0.8cm of y2] (yn) {$y_n$};
\node[state, below=of yn] (b) {$b$};
\node[state, above right=of y1] (a11) {$a_{11}$};
\node[state, below=of a11] (a21) {$a_{21}$};
\node[state, below right=of yn] (an1) {$a_{n1}$};
\node[state, below=of an1] (b1) {$b_1$};
\node[state, right=of a11] (ak1) {$a_{k1}$};
\node[state, right=of a21] (ak2) {$a_{k2}$};
\node[state, right=of an1] (ann) {$a_{nn}$};
\node[state, right=of b1] (bn) {$b_n$};
\node[state] at ($(ak2)!0.5!(ann)+(2,0)$) (sigma) {$\Sigma$};
\node[dot] at ($(a21)!0.3!(an1)$) {};
\node[dot] at ($(a21)!0.5!(an1)$) {};
\node[dot] at ($(a21)!0.7!(an1)$) {};
\node[dot] at ($(ak2)!0.3!(ann)$) {};
\node[dot] at ($(ak2)!0.5!(ann)$) {};
\node[dot] at ($(ak2)!0.7!(ann)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.3!($(an1)!0.5!(ann)$)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.5!($(an1)!0.5!(ann)$)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.7!($(an1)!0.5!(ann)$)$) {};
\node[dot] (dot1) at ($(a11)!0.5!(ak1)$) {};
\node[dot] (dot2) at ($(b1)!0.5!(bn)$) {};
\draw[arrow] (a11) -- (dot1) edge (ak1);
\draw[arrow] (b1) -- (dot2) edge (bn);
\draw[arrow] (y1) --node[above, font=\small]{$w_{11}^1$} (a11);
\draw[arrow] (y1) -- (a21);
\draw[arrow] (y2) --node[above, font=\small]{$w_{11}^1$} (a21);
\draw[arrow] (y2) -- (a21);
\draw[arrow] (y2) -- (an1);
\draw[arrow] (yn) -- (a11);
\draw[arrow] (yn) --node[above, font=\small]{$w_{11}^1$} (an1);
\draw[arrow] (yn) --node[above, font=\small]{$w_{n1}^1$} (b1);
\draw[arrow] (b) -- (a11);
\draw[arrow] (b) -- (an1);
\draw[arrow] (b) -- (b1);
\draw[arrow] (ak1) -- (sigma);
\draw[arrow] (ak2) -- (sigma);
\draw[arrow] (ann) -- (sigma);
\draw[arrow] (bn) -- (sigma);
\draw[arrow] (sigma) -- +(1,0)node[right]{$I$};
\end{tikzpicture}
\end{document}