
He implementado la siguiente imagen tikz:
Este es el 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}
Copié el código de la red neuronal (\DrawNeuralNetwork), así que no tengo idea real de cómo funciona (no soy muy experto en tikz). Solo traté de adaptarme lo mejor posible a como lo quiero. Ahora quiero agregar un elemento y no sé cómo hacerlo. Quiero agregar un rectángulo detrás de las últimas neuronas y conectar cada una de las últimas neuronas con una flecha al rectángulo como se indica en el siguiente dibujo. Luego quiero conectar la flecha al rectángulo agregado.
Sería bueno si alguien pudiera ayudarme.
Respuesta1
¿Como esto?
La red neuronal se dibuja desde cero. Se definen nuevos estilos, usados calc
y bibliotecas para chains
el positioning
posicionamiento de neuronas. Los comentarios en el código describen la función de las partes del 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}