tikz picuture에 일부 요소 추가 - 고급 tikz 명령

tikz picuture에 일부 요소 추가 - 고급 tikz 명령

다음 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}

관련 정보