% latexdraw.com
% 21/02/2021 at 22:20
\documentclass[border = 0.2cm]{standalone}
% Required package
\usepackage{tikz}
\begin {document}
% Input layer neurons'number
\newcommand{\inputnum}{12}
% Hidden layer neurons'number
\newcommand{\hiddennum}{12}
% Output layer neurons'number
\newcommand{\outputnum}{1}
\begin{tikzpicture}
% Input Layer
\foreach \i in {1,...,\inputnum}
{
\node[circle,
minimum size = 6mm,
fill=orange!30] (Input-\i) at (0,-\i) {};
}
% Hidden Layer
\foreach \i in {1,...,\hiddennum}
{
\node[circle,
minimum size = 6mm,
fill=teal!50,
yshift=(\hiddennum-\inputnum)*5 mm
] (Hidden-\i) at (2.5,-\i) {};
}
% Output Layer
\foreach \i in {1,...,\outputnum}
{
\node[circle,
minimum size = 6mm,
fill=purple!50,
yshift=(\outputnum-\inputnum)*5 mm
] (Output-\i) at (5,-\i) {};
}
% Connect neurons In-Hidden
\foreach \i in {1,...,\inputnum}
{
\foreach \j in {1,...,\hiddennum}
{
\draw[->, shorten >=1pt] (Input-\i) -- (Hidden-\j);
}
}
% Connect neurons Hidden-Out
\foreach \i in {1,...,\hiddennum}
{
\foreach \j in {1,...,\outputnum}
{
\draw[->, shorten >=1pt] (Hidden-\i) -- (Output-\j);
}
}
% Inputs
\foreach \i in {1,...,\inputnum}
{
\draw[<-, shorten >=1pt] (Input-\i) -- ++(-1,0)
node[left]{$x_{\i}$};
}
% Outputs
\foreach \i in {1,...,\outputnum}
{
\draw[->, shorten >=1pt] (Output-\i) -- ++(1,0)
node[right]{$y_{\i}$};
}
\end{tikzpicture}
\end{document}```
答案1
调整我对类似问题的回答经过
- 调整样式
layers
、input
和hidden
、output
- 定义
every pin edge
风格, - 删除除一个之外的所有隐藏层,
- 添加实际的“别针”。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{ext.positioning-plus,graphs}
\tikzset{
node matrix/.style={row sep=y_node_dist, column sep=x_node_dist,
every outer matrix/.append style={/pgf/inner sep=+0pt, /pgf/outer sep=+0pt, draw=none, fill=none, shape=rectangle}},
node matrix/node/.style={node contents=,anchor=center,
name/.expanded={\tikzmatrixname_\ifnum\pgfmatrixcurrentrow=1 \the\pgfmatrixcurrentcolumn\else\the\pgfmatrixcurrentrow\fi},
{nm \ifnum\pgfmatrixcurrentrow=1 \the\pgfmatrixcurrentcolumn\else\the\pgfmatrixcurrentrow\fi}/.try},
node matrix/place 1st node/.code args={#1,#2}{\node[node matrix/node,#1];},
node matrix/place other nodes/.style args={#1,#2}{/tikz/node matrix/place oth node/.list={#2}},
vertical node matrix/.style={/tikz/node matrix/place oth node/.code={\pgfmatrixendrow \node[node matrix/node,##1];}},
horizontal node matrix/.style={/tikz/node matrix/place oth node/.code={\pgfmatrixnextcell\node[node matrix/node,##1];}}}
\newcommand*\tikzMatrixNodes[2][1]{%
\matrix[every node matrix/.try,node matrix,#1]{
\tikzset{node matrix/place 1st node={#2},node matrix/place other nodes={#2}}\\};}
\begin{document}
\begin{tikzpicture}[
node distance=5mm and 10mm,
layers/.style={circle, minimum size=+6mm},
input/.style ={layers, fill=orange!30},
hidden/.style={layers, fill=teal!50},
output/.style={layers, fill=purple!50},
every node matrix/.style={vertical node matrix},
second to last/.style={
nm #1/.style={node contents=\vdots, text height=2ex, path only, minimum size=+0pt,inner sep=+0pt, shape=rectangle},
row #1/.append style={row sep=.5*y_node_dist}, row \pgfinteval{#1-1}/.append style={row sep=.5*y_node_dist}},
every pin edge/.append style={<-, black, thin}
]
\tikzMatrixNodes[name=hidden0, nodes=input, second to last=5]{,,,,,}
\foreach \hidden/\number in {1, 2, 3, 4, 6/n}
\path (hidden0_\hidden) [late options={pin={left:$x_\number$}}];
\foreach \hidden[count=\lastHidden from 0] in {1}{% loop can be removed using
\tikzMatrixNodes[ % right=of hidden0, name=hidden=1
right=of hidden\lastHidden, name=hidden\hidden,
nodes=hidden, second to last=5]{,,,,,}}
\node[right=of hidden1, output, pin={[pin edge=->]right:$y_1$}](output){};
\path[shorten >=1pt] graph[use existing nodes]{
{\foreach \x in {1,2,3,4,6}{hidden0_\x}}
-> [complete bipartite] {\foreach \x in {1,...,4,6}{hidden1_\x}}
-> {output};
};
%\path[nodes={align=center}]
% (hidden1_1.north) node[above,blue!70] (hl) {Hidden\\Layers} % could've been a label
% (hidden0_1|-hl.south) node[above,blue] {Input\\Layers}
% (output|-hl.south) node[above,red] {Output\\Layers};
\end{tikzpicture}
\end{document}