我读这他们建议使用变换画布,它可以重新缩放,但也会将图片移动到有文本的位置(见数字)。我也尝试使用\resizebox{}{}
或\scalebox{}{}
,但这只会让图片消失。
这就是我生成图片的方式
\begin{center}
\begin{tikzpicture}[thick,scale=1.2,transform canvas={scale=1.2},
clear/.style={
draw=none,
fill=none
},
net/.style={
matrix of nodes,
nodes={ draw, circle, inner sep=10pt },
nodes in empty cells,
column sep=2cm,
row sep=-9pt
},
>=latex
]
% define matrix mat to hold nodes
% using net as default style for cells
\matrix[net] (mat)
{
% Define layer headings
|[clear]| \parbox{1.3cm}{\centering Input\\layer}
& |[clear]| \parbox{1.3cm}{\centering Hidden\\layer}
& |[clear]| \parbox{1.3cm}{\centering Output\\layer} \\
|[clear]| & & |[clear]| \\
& |[clear]| & |[clear]| \\
|[clear]| & &|[clear]| \\
& |[clear]| & |[clear]| \\
|[clear]| & & \\
& |[clear]| & |[clear]| \\
|[clear]| & & |[clear]| \\
& |[clear]| & |[clear]| \\
|[clear]| & & |[clear]| \\
};
\draw[-] (mat-3-1) -- node[above] {$s_t^1$} +(-1.5cm,0);
\draw[-] (mat-5-1) -- node[above] {$s_t^2$} +(-1.5cm,0);
\draw[-] (mat-7-1) -- node[above] {$s_t^3$} +(-1.5cm,0);
\draw[-] (mat-9-1) -- node[above] {$s_t^4$} +(-1.5cm,0);
% lines from a_{i}^{0} to each a_{j}^{1}
\foreach \ai in {3,5,...,9} {
\foreach \aii in {2,4,6,8,10}
\draw[-] (mat-\ai-1) -- (mat-\aii-2);
}
% lines from a_{i}^{1} to a_{0}^{2}
\foreach \ai in {2,4,6,8,10}
\draw[-] (mat-\ai-2) -- (mat-6-3);
% right most line with Output label
\draw[->] (mat-6-3) -- node[above] {Output} +(2cm,0);
\end{tikzpicture}
\end{center}
有人知道如何解决这个问题吗?
答案1
这里能够使用\scalebox
等的一种方法是ampersand replacement
向矩阵中添加一个。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{center}
\scalebox{0.5}{\begin{tikzpicture}[thick,
clear/.style={
draw=none,
fill=none
},
net/.style={ampersand replacement=\&,
matrix of nodes,
nodes={ draw, circle, inner sep=10pt },
nodes in empty cells,
column sep=2cm,
row sep=-9pt
},
>=latex
]
% define matrix mat to hold nodes
% using net as default style for cells
\matrix[net] (mat)
{
% Define layer headings
|[clear]| \parbox{1.3cm}{\centering Input\\layer}
\& |[clear]| \parbox{1.3cm}{\centering Hidden\\layer}
\& |[clear]| \parbox{1.3cm}{\centering Output\\layer} \\
|[clear]| \& \& |[clear]| \\
\& |[clear]| \& |[clear]| \\
|[clear]| \& \&|[clear]| \\
\& |[clear]| \& |[clear]| \\
|[clear]| \& \& \\
\& |[clear]| \& |[clear]| \\
|[clear]| \& \& |[clear]| \\
\& |[clear]| \& |[clear]| \\
|[clear]| \& \& |[clear]| \\
};
\draw[-] (mat-3-1) -- node[above] {$s_t^1$} +(-1.5cm,0);
\draw[-] (mat-5-1) -- node[above] {$s_t^2$} +(-1.5cm,0);
\draw[-] (mat-7-1) -- node[above] {$s_t^3$} +(-1.5cm,0);
\draw[-] (mat-9-1) -- node[above] {$s_t^4$} +(-1.5cm,0);
% lines from a_{i}^{0} to each a_{j}^{1}
\foreach \ai in {3,5,...,9} {
\foreach \aii in {2,4,6,8,10}
\draw[-] (mat-\ai-1) -- (mat-\aii-2);
}
% lines from a_{i}^{1} to a_{0}^{2}
\foreach \ai in {2,4,6,8,10}
\draw[-] (mat-\ai-2) -- (mat-6-3);
% right most line with Output label
\draw[->] (mat-6-3) -- node[above] {Output} +(2cm,0);
\end{tikzpicture}}
\end{center}
\end{document}
应该说这可以治标。然而,为了得到更优雅的解决方案,您可能需要(从根本上)重写代码,从质疑\parbox
矩阵中 es 的用途开始。
获得非常相似的输出的一种方法是
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.8,transform shape,
neuron/.style={circle,inner sep=1em,draw}]
\begin{scope}[x=3cm,y=1.5cm,local bounding box=net]
\foreach \Y [count=\Z,remember=\Y as \LastY] in
{4,5,1}
{\foreach \YY in {1,...,\Y}
{\node[neuron] (n-\Z-\YY) at (\Z,-\YY+\Y/2+1/2) {};
\ifnum\Z=1
\draw (n-\Z-\YY.west) -- node[above] {$s^t_{\YY}$}++ (-2em,0);
\else
\foreach \YYY in {1,...,\LastY}
{\draw[-,semithick] (n-\the\numexpr\Z-1\relax-\YYY) -- (n-\Z-\YY) ;}
\fi
}}
\end{scope}
\path foreach \X [count=\Y] in {Input,Hidden,Outpt}
{ (net.north-|n-\Y-1) node[above=1em,align=center] {\X\\ Layer}
};
\end{tikzpicture}
\end{center}
\end{document}
我放入了一个非平凡的比例因子来证明它有效。但是,您还有其他可能更好的控制此图大小的方法。通过在范围内更改x
和,y
您可以分别控制神经元之间的水平和垂直距离。后者的大小由样式控制。此代码还避免了|[clear]|
到处添加的需要,如果您想更改每层的神经元数量,只需更改即可{4,5,1}
。当然,这个网站上还有许多其他绘制神经网络的示例(我以为甚至有一个包,但出于某种原因我现在找不到它)。