
Eu implementei a seguinte imagem tikz:
Este é o código:
\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}
Copiei o código da rede neural (\DrawNeuralNetwork), então não tenho ideia real de como funciona (não tenho muita habilidade em tikz). Eu apenas tentei encaixar da melhor maneira possível como eu quero. Agora quero adicionar um elemento e não sei como. Quero adicionar um retângulo atrás dos últimos neurônios e conectar cada um dos últimos neurônios com uma seta ao retângulo, conforme indicado no esboço abaixo. Então quero conectar a seta ao retângulo adicionado.
Seria bom se alguém pudesse me ajudar.
Responder1
Assim?
A rede neural é desenhada do zero. São definidos novos estilos, utilizados calc
, chains
e positioning
bibliotecas para posicionamento de neurônios. Os comentários no código descrevem a função das partes do código:
\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}