나는 다음과 같은 다층 퍼셉트론을 그리고 싶습니다.
그리고 다음 코드를 만들었습니다.
\documentclass[tikz, margin=3mm] {standalone}
\usetikzlibrary{arrows.meta, matrix}
\begin{document}
\begin{tikzpicture}[
> = Stealth, thick,
plain/.style = {draw=none, fill=none, yshift=11mm, text width=7ex, align=center},% for text in images,
ec/.style = {draw=none, fill=none},% for emty cells,
net/.style = {matrix of nodes, % for matrix style
nodes={circle,fill=blue!20, draw, semithick, minimum width=12mm, inner sep=0mm},% circles in image
nodes in empty cells,% for not used cells in matrix
column sep = 16mm, % distance between columns in matrix
row sep = -3mm % distance between rows in matrix
}]
\matrix[net] (m)% m is matrix name, it is used for names of cell: firs has name m-1-1
% in empty space between ampersands will show circles:
% i.e.: nodes of the neural network
{
|[plain]| Input layer & |[plain]| Hidden layer & |[plain]| Output layer \\
|[ec]| & |[ec]| & |[ec]| \\
& -1 & |[ec]| \\
|[ec]| & |[ec]| & |[ec]| \\
|[ec]| & |[ec]| & 3 \\
|[ec]| & |[ec]| & |[ec]| \\
& -1 & |[ec]| \\
|[ec]| & |[ec]| & |[ec]| \\
};
\draw[<-] (m-3-1) -- node[left, xshift=-0.65cm] {$x_1$} +(-2cm,0);
\draw[<-] (m-7-1) -- node[left, xshift=-0.65cm] {$x_2$} +(-2cm,0);
\draw[->] (m-5-3) -- node[right, xshift=0.65cm] {$y$} +(2cm,0);
\draw[->] (m-3-1) -- node[above] {$-2$} (m-3-2);
\draw[->] (m-3-1) -- node[below, yshift=-0.75cm, xshift=-0.5cm] {$2$} (m-7-2);
\draw[->] (m-7-1) -- node[above, yshift=0.75cm, xshift=-0.5cm] {$2$} (m-3-2);
\draw[->] (m-7-1) -- node[below] {$-2$} (m-7-2);
\draw[->] (m-3-2) -- node[above] {$2$} (m-5-3);
\draw[->] (m-7-2) -- node[below] {$2$} (m-5-3);
\end{tikzpicture}
\end{document}
이것은 abovie의 출력입니다.
문제는 첫 번째 레이어에서 노드를 제거할 때마다 tikz 도면이 비대칭이라는 것입니다. 거기에 "1"을 그리면 그림이 대칭이 됩니다.
그러면 이 문제를 어떻게 해결할 수 있나요? 최소 크기를 조정하려고 했지만 도움이 되지 않습니다.
감사합니다!
답변1
A는 matrix of nodes
내부 노드 앵커를 로 변경합니다 base
( 다이어그램 center
은 nodes={... anchor=center}
다시 대칭이 됩니다).
\documentclass[tikz, margin=3mm] {standalone}
\usetikzlibrary{arrows.meta, matrix}
\begin{document}
\begin{tikzpicture}[
> = Stealth, thick,
plain/.style = {draw=none, fill=none, yshift=11mm, text width=7ex, align=center},% for text in images,
ec/.style = {draw=none, fill=none},% for emty cells,
net/.style = {matrix of nodes, % for matrix style
nodes={circle,fill=blue!20, draw, semithick, minimum width=12mm, inner sep=0mm, anchor=center},% circles in image
nodes in empty cells,% for not used cells in matrix
column sep = 16mm, % distance between columns in matrix
row sep = -3mm % distance between rows in matrix
}]
\matrix[net] (m)% m is matrix name, it is used for names of cell: firs has name m-1-1
% in empty space between ampersands will show circles:
% i.e.: nodes of the neural network
{
|[plain]| Input layer & |[plain]| Hidden layer & |[plain]| Output layer \\
|[ec]| & |[ec]| & |[ec]| \\
& -1 & |[ec]| \\
|[ec]| & |[ec]| & |[ec]| \\
|[ec]| & |[ec]| & 3 \\
|[ec]| & |[ec]| & |[ec]| \\
& -1 & |[ec]| \\
|[ec]| & |[ec]| & |[ec]| \\
};
\draw[<-] (m-3-1) -- node[left, xshift=-0.65cm] {$x_1$} +(-2cm,0);
\draw[<-] (m-7-1) -- node[left, xshift=-0.65cm] {$x_2$} +(-2cm,0);
\draw[->] (m-5-3) -- node[right, xshift=0.65cm] {$y$} +(2cm,0);
\draw[->] (m-3-1) -- node[above] {$-2$} (m-3-2);
\draw[->] (m-3-1) -- node[below, yshift=-0.75cm, xshift=-0.5cm] {$2$} (m-7-2);
\draw[->] (m-7-1) -- node[above, yshift=0.75cm, xshift=-0.5cm] {$2$} (m-3-2);
\draw[->] (m-7-1) -- node[below] {$-2$} (m-7-2);
\draw[->] (m-3-2) -- node[above] {$2$} (m-5-3);
\draw[->] (m-7-2) -- node[below] {$2$} (m-5-3);
\end{tikzpicture}
\end{document}