我想从输出到输入画一条曲线箭头。有人能帮我吗?先谢谢了
\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{hidden 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 hidden layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-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 hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Output layer};
\end{tikzpicture}
% End of code
\end{document}
答案1
我猜,你想获得类似的东西:
但我不确定(你没有定义你的“输入”在哪里)
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\usetikzlibrary{calc}
\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50,
node distance = \layersep,
every pin edge/.style = {<-,shorten <=1pt},
neuron/.style = {circle,fill=black!25,minimum size=17pt,inner sep=0pt},
input neuron/.style = {neuron, fill=green!50},
output neuron/.style = {neuron, fill=red!50},
hidden 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 hidden layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-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 hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Output layer};
% curve from output to input
\draw[line width=1mm, gray,->]
([xshift=21mm] O.east) .. controls + (2,-1) and + (4,0) .. ([yshift=-7mm] H-5.south)
.. controls + (-5,0) and + (-2,-1) .. ([xshift=-27mm]$(I-1)!0.5!(I-4)$);
\end{tikzpicture}
% End of code
\end{document}
附录: 从 OP 评论可以看出,我猜测应该连接哪条曲线是错误的(抱歉,但我的水晶球罢工了 :()。即使是现在,我也不确定曲线是否如 OP 所愿。但是,原则在第一种和这种情况下是一样的,如果现在曲线不在应该在的确切位置,只需要相应地更改曲线的起点和终点坐标。
与第一个版本相比,以下版本更改了箭头类型。现在使用arrows.meta
和bending
库,通过选项可以更平滑地定位箭头flex
。此外,由于现在输入处的文本交叉,曲线在背景层上绘制。为此,库backgrounds
和提供服务scopes
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\usetikzlibrary{arrows.meta, bending, backgrounds, scopes}
\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50,
node distance = \layersep,
every pin edge/.style = {<-,shorten <=1pt},
neuron/.style = {circle,fill=black!25,minimum size=17pt,inner sep=0pt},
input neuron/.style = {neuron, fill=green!50},
output neuron/.style = {neuron, fill=red!50},
hidden 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 hidden layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-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 hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Output layer};
% curve from output to input
\scoped[on background layer]
\draw[line width=1mm, gray!50, shorten >=1mm, shorten <=1mm, -{Latex[flex]}]
(O.east) .. controls + (1,-0.5) and + (3.5,0) .. ([yshift=-7mm] H-5.south)
.. controls + (-6,0) and + (-2,-0.5) .. (I-1);
\end{tikzpicture}
% End of code
\end{document}