![在 tikz 图片中添加一些元素 - 高级 tikz 命令](https://linux22.com/image/449741/%E5%9C%A8%20tikz%20%E5%9B%BE%E7%89%87%E4%B8%AD%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%BA%9B%E5%85%83%E7%B4%A0%20-%20%E9%AB%98%E7%BA%A7%20tikz%20%E5%91%BD%E4%BB%A4.png)
我已经实现了以下 tikz 图片:
这是代码:
\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}
\usetikzlibrary{fit}
\tikzset{%
neuron missing/.style={
draw=none,
scale=2,
text height=0.333cm,
execute at begin node=\color{black}$\vdots$
},
}
\newcommand{\DrawNeuronalNetwork}[2][]{
\xdef\Xmax{0}
\foreach \Layer/\X/\Col/\Miss/\Lab/\Count/\Content [count=\Y] in {#2}
{\pgfmathsetmacro{\Xmax}{max(\X,\Xmax)}
\xdef\Xmax{\Xmax}
\xdef\Ymax{\Y}
}
\foreach \Layer/\X/\Col/\Miss/\Lab/\Count/\Content [count=\Y] in {#2}
{\node[anchor=south] at ({2*\Y},{\Xmax/2+0.1}) {\Layer};
\foreach \m in {1,...,\X}
{
\ifnum\m=\Miss
\node [neuron missing] (neuron-\Y-\m) at ({2*\Y},{\X/2-\m}) {};
\else
\node [circle,fill=\Col!50,minimum size=0.3cm] (neuron-\Y-\m) at
({2*\Y},{\X/2-\m}) {\Content};
\ifnum\Y=1
\else
\pgfmathtruncatemacro{\LastY}{\Y-1}
\foreach \Z in {1,...,\LastX}
{
\ifnum\Z=\LastMiss
\else
\draw[->] (neuron-\LastY-\Z) -- (neuron-\Y-\m);
\fi
}
\fi
\fi
\ifnum\Y=1
\ifnum\m=\X
\draw [overlay] (neuron-\Y-\m) -- (state);
\else
\ifnum\m=\Miss
\else
\draw [overlay] (neuron-\Y-\m) -- (state);
\fi
\fi
\else
\fi
}
\xdef\LastMiss{\Miss}
\xdef\LastX{\X}
}
}
\begin{document}
\centering
\begin{tikzpicture}[scale = 0.3, x=1.5cm, y=1.5cm,
>=stealth,font=\sffamily,nodes={align=center}]
\begin{scope}[local bounding box=T]
\path node[coordinate,minimum width=3em,minimum height=2em] (state) {State};
\begin{scope}[local bounding box=NN]
\DrawNeuronalNetwork{/5/black/4///,
/5/black/4//11/,
/4/black/3//11/}
\end{scope}
\path (NN.south) node[below]{parameter $\theta$};
\path(NN.east) -- node[above]{policy\\ $\pi(\theta,a)$}++ (2em,10em);
\end{scope}
\node[fit=(T),label={[anchor=north west]north west:agent},inner sep=0.7em,draw]
(TF){};
\node[below=3em of TF,draw,inner sep=1.5em, fill=gray!30] (Env) {environment};
\draw[<-] (TF.200) -- ++ (-8em,0) |- (Env.170) node[pos=0.27,right]{$r_t$};
\draw[<-] (state) -- ++ (-16em,0) |- (Env.180) node[pos=0.31,left]{$s_t$};
\draw[->] (TF.east) -- ++ (12em,0) |- (Env)
node[pos = 0.3,right]{$a_t$};
\end{tikzpicture}
\end{document}
我复制了神经网络的代码(\DrawNeuralNetwork),所以我真的不知道它是如何工作的(我不太擅长 tikz)。我只是尽量让它尽可能符合我的要求。现在我想添加一个元素,但我不知道该怎么做。我想在最后一个神经元后面添加一个矩形,并用箭头将最后一个神经元连接到矩形,如下图所示。然后我想将箭头连接到添加的矩形。
如果有人能帮助我就太好了。
答案1
像这样?
神经网络是从头开始绘制的。定义了新样式,使用了calc
和chains
库positioning
来定位神经元。代码中的注释描述了代码部分的功能:
\documentclass[border=2mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
calc, chains,
fit,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 2mm and 12mm,
start chain = going below,
annot/.style = {text width=3em, align=center},
arr/.style = {-{Stealth[length=3pt,width=2pt]}, semithick},
dots/.style = {text height=2ex,
node contents={$\vdots$},
on chain},
FIT/.style = {draw, inner sep=2mm, fit=#1, node contents={}},
lbl/.style = {inner sep=2pt, font=\footnotesize},
neuron/.style = {circle, fill=#1!50,
minimum size=3mm, inner sep=0pt, node contents={},
on chain},
every edge/.style = {arr, draw, very thin, shorten > =0.5pt},
every label/.style = {align=center},
]
% input layer nodes
\foreach \i in {1,...,5}
{
\ifnum\i=4
\node (I-\i) [dots];
\else
\node (I-\i) [neuron=green];
\fi
}
% middle layer nodes
\node (H-1) [neuron=blue,
right=of I-1];
\foreach \i in {2,...,5}
{
\ifnum\i=4
\node (H-\i) [dots];
\else
\node (H-\i) [neuron=blue!50];
\fi
}
% out layer nodes
\node (O-1) [neuron=red,
right=of {$(H-1)!0.5!(H-2)$}];
\foreach \i in {2,...,4}
{
\ifnum\i=3
\node (O-\i) [dots];
\else
\node (O-\i) [neuron=red];
\fi
}
%%%% conections
\coordinate[left=of I-3] (in);
\foreach \i in {1,2,3,5}
\draw (in) edge (I-\i);
%
\foreach \i in {1,2,3,5}
{
\foreach \j in {1,2,3,5}
\draw (I-\i) edge (H-\j);
}
\foreach \i in {1,2,3,5}
{
\foreach \j in {1,2,4}
\draw (H-\i) edge (O-\j);
}
%%%% out rectangle
\coordinate[right=of I-1 -| O-1.west] (out1);
\coordinate[right=of I-5 -| O-4.west] (out2);
\node (out) [FIT=(out1) (out2)];
\foreach \i in {1,2,4}
\draw (O-\i) edge (O-\i -| out.center);
%%%% neuron fit rectangle
\node (nrn) [FIT=(in) (I-1) (I-2) (out),
label={[anchor=north west]north west:agent},
label={[anchor=north east]north east: policy\\$\pi(\theta,a)$},
inner ysep=4ex, yshift=3ex];
%%%% feedback
\node (env) [below=of nrn,
minimum height=5ex, text depth=0.5ex,
draw, fill=gray!30] {environment};
\draw[arr] (out.east) -- ++ (2em,0)
|- (env) node[lbl, pos=0.25,right] {$a_t$};
\draw[arr] (env.190) -| ([xshift=-5em] in) node[lbl, pos=0.71,left]{$s_t$}
-- (in);
\draw[arr] (env.170) -| ([xshift=-3em] nrn.200) node[lbl, pos=0.75,right]{$r_t$}
-- (nrn.200);
\end{tikzpicture}
\end{document}