我正在尝试根据第一个答案建立一个神经网络这里。最终的网络应该有 3 个输入节点、4 个隐藏节点和 2 个输出节点。
现在,图像如下所示:
为什么隐藏层的中间节点比其他节点大?我需要它们大小相同。注意:我已经成功制作了一个神经网络(如下)。我只是希望这是一个没有“数据”列的修改版本。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\begin{document}
\begin{figure}[htp]
\centering
\begin{tikzpicture}[
plain/.style={
draw=none,
fill=none,
},
active/.style={
draw=none,
fill=green!20,
},
invisible/.style={
draw=none,
fill=none,
text=white,
},
halfinvisible/.style={
draw,
fill=none,
text=white,
},
net/.style={
matrix of nodes,
nodes={
draw,
circle,
inner sep=8.5pt
},
nodes in empty cells,
column sep=0.5cm,
row sep=-5pt
},
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
& & \\ %Top Data and Input nodes
& |[halfinvisible]| \parbox{0.2cm}{\centering{ii}} & \\
};
\end{tikzpicture}
\end{figure}
\end{document}
这是我想要复制的版本(删除数据列)。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\begin{document}
\begin{figure}[htp]
\centering
\begin{tikzpicture}[
plain/.style={
draw=none,
fill=none,
},
active/.style={
draw=none,
fill=green!20,
},
invisible/.style={
draw=none,
fill=none,
text=white,
},
net/.style={
matrix of nodes,
nodes={
draw,
circle,
inner sep=8.5pt
},
nodes in empty cells,
column sep=0.5cm,
row sep=-5pt
},
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1cm}{\centering Data} & |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
%& & |[plain]| \\ %Top Data and Input nodes
%|[plain]| & |[plain]| & \\ %Top Hidden node
|[plain]| \parbox{0.2cm}{\centering 20} & & &\\
|[plain]| \parbox{0.2cm}{\centering 30} & & & \\
|[plain]| \parbox{0.2cm}{\centering 40} & & \\
|[invisible]| \parbox{0.2cm}{} & |[invisible]| \parbox{0.2cm}{\centering 40} & \\
};
\foreach \ai [count=\mi ]in {2,3,4}
\draw[<-] (mat-\ai-2) --node[above]{Input \mi} +(-2cm,0);
\foreach \ai in {2,...,4}
{\foreach \aii in {2,...,5}
\draw[->] (mat-\ai-2) -- (mat-\aii-3);
}
\foreach \ai in {2,...,5}
{\foreach \aii in {2,3}
\draw[->] (mat-\ai-3) -- (mat-\aii-4);
}
\end{tikzpicture}
\end{figure}
\end{document}
它看起来像这样:
答案1
它之所以更大,是因为里面有文字,具体来说就是|[halfinvisible]| \parbox{0.2cm}{\centering{ii}}
。因此,如果你删除该文本(并稍微修改一下row sep
),你会得到这样的结果:
这是第二段代码的稍微修改后的版本,其中删除了第一列:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\ctab}[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}
\begin{document}
\begin{figure}[htp]
\centering
\begin{tikzpicture}[
plain/.style={
rectangle, % <-- added
draw=none,
fill=none,
},
active/.style={
draw=none,
fill=green!20,
},
invisible/.style={
draw=none,
fill=none,
text=white,
},
net/.style={
matrix of nodes,
nodes={
draw,
circle,
inner sep=8.5pt
},
row 1/.style={
nodes={plain} % set style for nodes in first row
},
nodes in empty cells,
column sep=1.5cm, % <-- modified
row sep=3pt % <-- modified
},
>=latex
]
\matrix[net] (mat)
{
\ctab{Input\\layer} &
\ctab{Hidden\\layer} &
\ctab{Output\\layer} \\
& &\\
& & \\
& \\
|[invisible]| & & |[invisible]| \\
};
\foreach \ai in {2,...,4}
{\foreach \aii in {2,...,5}
\draw[->] (mat-\ai-1) -- (mat-\aii-2); % decreased column numbers by 1
}
\foreach \ai in {2,...,5}
{\foreach \aii in {2,3}
\draw[->] (mat-\ai-2) -- (mat-\aii-3); % decreased column numbers by 1
}
\end{tikzpicture}
\end{figure}
\end{document}