我想使用 if-then-else 语句来绘制 \vdots 而不是节点。在下图中:
\documentclass[border=1mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}
\usepackage{xifthen}
\pgfplotsset{compat=1.15}
\begin{document}
\pagestyle{empty}
\def\layersep{2.5cm}
\def\layersepp{5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,]
\tikzstyle{every pin edge}=[<-,shorten <=1pt]
\tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=green!50];
\tikzstyle{output neuron}=[neuron, fill=red!50];
\tikzstyle{hidden neuron}=[neuron, fill=blue!50];
\tikzstyle{annot} = [text width=4em, text centered]
% Draw the input layer nodes
\foreach \name / \y in {1,...,5}
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
\node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {}; % {$p_\y$};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.cm]
node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.cm]
node[hidden neuron] (H1-\name) at (\layersepp,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H1-3] (O) {};
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,5}
\foreach \dest in {1,...,5}
\path (H-\source) edge (H1-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H1-\source) edge (O);
% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,above of=H1-1, node distance=1cm] (hl1) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl1] {Output layer};
\end{tikzpicture}
\end{document}
绘制该图:
要删除前三个节点列中的倒数第二个(第 4 个)节点,我使用\ifthenelse
以下代码:
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,]
\tikzstyle{every pin edge}=[<-,shorten <=1pt]
\tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=green!50];
\tikzstyle{output neuron}=[neuron, fill=red!50];
\tikzstyle{hidden neuron}=[neuron, fill=blue!50];
\tikzstyle{annot} = [text width=4em, text centered]
% Draw the input layer nodes
\foreach \name / \y in {1,...,5}
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
\ifthenelse{\NOT 4 = \y }{
\node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {}; % {$p_\y$};
}{$\vdots$}
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,5}
\ifthenelse{\NOT 4 = \y }{
\path[yshift=0.cm]
node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
}{$\vdots$}
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,5}
\ifthenelse{\NOT 4 = \y }{
\path[yshift=0.cm]
node[hidden neuron] (H1-\name) at (\layersepp,-\y cm) {};
}{$\vdots$}
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H1-3] (O) {};
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,5}
\foreach \dest in {1,...,5}
\ifthenelse{\NOT 4 = \y }{
\path (I-\source) edge (H-\dest);
}{}
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,5}
\foreach \dest in {1,...,5}
\ifthenelse{\NOT 4 = \y }{
\path (H-\source) edge (H1-\dest);
}{}
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\ifthenelse{\NOT 4 = \y }{
\path (H1-\source) edge (O);
}{}
% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,above of=H1-1, node distance=1cm] (hl1) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl1] {Output layer};
\end{tikzpicture}
但它不能编译,第一次出现以下错误ifthenelse
Latex Error: ./rnn_split_2.tex:103 Paragraph ended before \pgffor@next was complete.
答案1
主要问题是您不能将其放入\vdots
环境中间tizpicture
。相反,您需要将其放在节点内。这类似于\node at (0,0) {$\vdots$};
。另一个问题是“长”\foreach
语句需要用括号括起来。为了安全起见,我总是写
\foreach \name / \y in {1,...,5} {
...
}
您的一些测试涉及\y
应该使用\dest
或进行测试\source
。除此之外,我一直使用 直线,\ifnum\y=4 ...\else ...\fi
因为我更喜欢这样。进行这些更改后,您的代码将编译并生成:
以下是更新后的代码:
\documentclass[border=1mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}
%\usepackage{xifthen}
\pgfplotsset{compat=1.15}
\begin{document}
\pagestyle{empty}
\def\layersep{2.5cm}
\def\layersepp{5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,]
\tikzstyle{every pin edge}=[<-,shorten <=1pt]
\tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=green!50];
\tikzstyle{output neuron}=[neuron, fill=red!50];
\tikzstyle{hidden neuron}=[neuron, fill=blue!50];
\tikzstyle{annot} = [text width=4em, text centered]
% Draw the input layer nodes
\foreach \name / \y in {1,...,5} {
\ifnum\y=4 \node at (0,-\y) {$\vdots$}; % {$p_\y$};
\else
\node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {}; % {$p_\y$};
\fi
}
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,5} {
\ifnum\y=4 \node at (\layersep,-\y cm) {$\vdots$}; % {$p_\y$};
\else
\path[yshift=0.cm] node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
\fi
}
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,5} {
\ifnum\y=4 \node at (\layersepp,-\y cm) {$\vdots$}; % {$p_\y$};
\else
\path[yshift=0.cm] node[hidden neuron] (H1-\name) at (\layersepp,-\y cm) {};
\fi
}
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H1-3] (O) {};
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,5} {
\ifnum\source=4\else
\foreach \dest in {1,...,5} {
\ifnum\dest=4\else
\path (I-\source) edge (H-\dest);
\fi
}
\fi
}
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,5} {
\ifnum\source=4\else
\foreach \dest in {1,...,5} {
\ifnum\dest=4\else
\path (H-\source) edge (H1-\dest);
\fi
}
\fi
}
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5} {
\ifnum\source=4\else
\path (H1-\source) edge (O);
\fi
}
% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,above of=H1-1, node distance=1cm] (hl1) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl1] {Output layer};
\end{tikzpicture}
\end{document}
最后,\tiksstyle
已经贬值了。这些天你应该使用\tikzset
,但我没有改变这一点。