如何在节点中间画一条直线?
本质上我正在寻找将节点划分为两个半球(左和右)的方法,以及上下绘制线条的方法。
我正在做一个演示:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\def\layersep{2.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{compute neuron}=[neuron, fill=blue!50];
\tikzstyle{annot} = [text width=4em, text centered]
% Draw the input layer nodes
\foreach \name / \y in {1,...,4}
% 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) {};
% Draw the compute layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[compute neuron] (C-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=C-3] (O) {};
% Connect every node in the input layer with every node in the
% compute layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\path (I-\source) edge (C-\dest);
% Connect every node in the compute layer with the output layer
\foreach \source in {1,...,5}
\path (C-\source) edge (O);
% Annotate the layers
\node[annot,above of=C-1, node distance=1cm] (hl) {compute layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Compute layer};
\end{tikzpicture}
% End of code
\end{document}
我希望每个神经元被分成两半。
答案1
您只需用 -- 替换circle
tikz 样式中的神经元circle split
,然后您需要将其添加\usetikzlibrary{shapes}
到序言中。这将产生:
我还添加了,draw=block
因为在紫色神经元内部很难看到分裂,但是,这也会添加您可能不想要的边框。
此外,\tikzstyle{...}
我并不使用命令,而是直接将它们放在选项中,或者如果环境不止一个,则将tikzpicture
它们全部放在序言中。这是您修改后的 MWE:\tikz{...}
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\pagestyle{empty}
\def\layersep{2.5cm}
\begin{tikzpicture}[
shorten >=1pt,->,draw=black!50, node distance=\layersep,
every pin edge/.style={<-,shorten <=1pt},
neuron/.style={circle split,fill=black!25,minimum size=17pt,inner sep=0pt,draw=black},
input neuron/.style={neuron, fill=green!50},
output neuron/.style={neuron, fill=red!50},
compute neuron/.style={neuron, fill=blue!50},
annot/.style={text width=4em, text centered}
]
% Draw the input layer nodes
\foreach \name / \y in {1,...,4}
% 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) {};
% Draw the compute layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[compute neuron] (C-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=C-3] (O) {};
% Connect every node in the input layer with every node in the
% compute layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\path (I-\source) edge (C-\dest);
% Connect every node in the compute layer with the output layer
\foreach \source in {1,...,5}
\path (C-\source) edge (O);
% Annotate the layers
\node[annot,above of=C-1, node distance=1cm] (hl) {compute layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Compute layer};
\end{tikzpicture}
% End of code
\end{document}
编辑
要进行垂直分割,只需添加节点即可rotate=90
,如TikZ:圆形垂直分割。不幸的是,这会弄乱相对节点的位置。解决这个问题有点棘手,但以下是实现此目的的一种方法:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\pagestyle{empty}
\def\layersep{2.5cm}
\begin{tikzpicture}[
shorten >=1pt,->,draw=black!50, node distance=\layersep,
every pin edge/.style={<-,shorten <=1pt},
neuron/.style={circle split,fill=black!25,minimum size=17pt,inner sep=0pt},
input neuron/.style={neuron, fill=green!50},
output neuron/.style={neuron, fill=red!50},
compute neuron/.style={neuron, fill=blue!50},
annot/.style={text width=4em, text centered}
]
% Draw the input layer nodes
\foreach \name / \y in {1,...,4}
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
\node[input neuron, pin position=-90, pin=Input \#\y, rotate=90] (I-\name) at (0,-\y) {};
% Draw the compute layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[compute neuron,rotate=90] (C-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=C-3,rotate=90] (O) {};
% Connect every node in the input layer with every node in the
% compute layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\path (I-\source) edge (C-\dest);
% Connect every node in the compute layer with the output layer
\foreach \source in {1,...,5}
\path (C-\source) edge (O);
% Annotate the layers
\node[annot,above of=C-1, node distance=1cm] (hl) {compute layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Compute layer};
\end{tikzpicture}
% End of code
\end{document}
我有不是添加rotate=90
到neuron
样式中,因为这会破坏后续的相对节点放置。相反,我已添加rotate=90
到每个节点(我本以为这是等效的,但事实并非如此!)。此外,我还添加pin position=-90
到“输入层”中的节点。这产生了我认为是所需的输出:
答案2
无需使用过时的\tikzstyle{...}
:
\documentclass{article}
\usepackage{tikz}
\newcommand\ppbb{path picture bounding box}
\begin{document}
\pagestyle{empty}
\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt, ->, draw=black!50,
node distance = \layersep,
every pin edge/.style = {<-, shorten <=1pt},
neuron/.style = {circle,minimum size=17pt,inner sep=0pt,
path picture={%
\path[draw=black!75,semithick,-] (\ppbb.north) -- (\ppbb.south);},
},
input neuron/.style = {neuron, fill=green!50},
output neuron/.style = {neuron, fill=red!50},
compute neuron/.style = {neuron, fill=blue!50},
annot/.style = {text width=4em, text centered},
]
% Draw the input layer nodes
\foreach \name / \y in {1,...,4}
% 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) {};
% Draw the compute layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[compute neuron] (C-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=C-3] (O) {};
% Connect every node in the input layer with every node in the
% compute layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\path (I-\source) edge (C-\dest);
% Connect every node in the compute layer with the output layer
\foreach \source in {1,...,5}
\path (C-\source) edge (O);
% Annotate the layers
\node[annot,above of=C-1, node distance=1cm] (hl) {compute layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Compute layer};
\end{tikzpicture}
% End of code
\end{document}
编辑: 您在下面评论中的请求不是很清楚标签在哪里......到目前为止,看看您的意思是这样的:
现在我稍微清理一下你的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand\ppbb{path picture bounding box}
\begin{document}
\pagestyle{empty}
\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt, ->, draw=black!50,
node distance = \layersep,
every pin edge/.style = {<-, shorten <=1pt},
neuron/.style = {circle, minimum size=17pt, inner sep=0pt,
path picture={%
\path[draw=black!75,semithick,-] (\ppbb.north) -- (\ppbb.south);},
},
input neuron/.style = {neuron, fill=green!50},
output neuron/.style = {neuron, fill=red!50},
compute neuron/.style = {neuron, fill=blue!50},
annot/.style = {text width=4em, text centered},
]
% Draw the input layer nodes
\foreach \y in {1,...,4}
\node[input neuron, pin=left:Input \#\y] (I-\y) at (0,-\y) {};
\node[above=0pt of I-1] {$h_0$};
% Draw the compute layer nodes
\foreach \y in {1,...,5}
\node[yshift=0.5cm,compute neuron] (C-\y) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right=of C-3.west] (O) {};
% Connect every node in the input layer with every node in the
% compute layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\path (I-\source) edge (C-\dest);
% Connect every node in the compute layer with the output layer
\foreach \source in {1,...,5}
\path (C-\source) edge (O);
% Annotate the layers
\begin{scope}[node distance=0pt]
\node[annot,above=3mm of C-1] (hl) {compute layer};
\node[annot,above=of hl.south -| I-1] {Input layer};
\node[annot,above=of hl.south -| O] {Compute layer};
\end{scope}
\end{tikzpicture}
% End of code
\end{document}