我想用 Tikz 绘制一个完全连接的神经网络,想知道是否有一种简单的方法可以将所有神经元垂直居中?如果可能的话,我想使用我的方法。这是我的代码和示例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\def\layersep{2cm}
\def\hsep{1cm}
\def\ilsize{4}
\def\hlsize{8}
\def\olsize{6}
\def\rootlrp{6}
\def\neuronsize{4mm}
\tikzset{>=latex}
\begin{figure}
\centering
\begin{tikzpicture}[shorten >=0pt, ->, draw=black!100, node distance=\layersep]
\tikzstyle{every pin edge}=[<-,shorten <=1pt]
\tikzstyle{neuron}=[circle, draw, fill=black!100, minimum size=\neuronsize,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=black!0]
\tikzstyle{hidden neuron}=[neuron, fill=black!0]
\tikzstyle{output neuron}=[neuron, fill=black!0]
%%%%%%%%%%%%
% DRAW NODES
%%%%%%%%%%%%
% Draw the input layer nodes
\foreach \name / \y in {1,...,\ilsize}
\node[input neuron] (In-\name) at (0.0cm+\hsep,-\y cm) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,\hlsize}
\node[hidden neuron] (H0-\name) at (1.5cm+\hsep,-\y cm) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,\hlsize}
\node[hidden neuron] (H1-\name) at (3.0cm+\hsep,-\y cm) {};
% Draw the output layer nodes
\foreach \name / \y in {1,...,\olsize}
\node[hidden neuron] (Out-\name) at (4.5cm+\hsep,-\y cm) {};
%%%%%%%%%%%%%%%%%%
% DRAW CONNECTIONS
%%%%%%%%%%%%%%%%%%
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,\ilsize}
\foreach \dest in {1,...,\hlsize}
\path (In-\source) edge (H0-\dest);
% Connect first with second hidden layer
\foreach \source in {1,...,\hlsize}
\foreach \dest in {1,...,\hlsize}
\path (H0-\source) edge (H1-\dest);
% Connect every node from the last hidden layer with the output layer
\foreach \source in {1,...,\hlsize}
\foreach \dest in {1,...,\olsize}
\path (H1-\source) edge (Out-\dest);
\end{tikzpicture}
\end{figure}
\end{document}
答案1
这是另一个版本ABlueChampions 的回答,我现在才看到,这让你的情节保持完整,但我真的不能把\tikzstyle
s 留在里面。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
% really bad practice, sorry
\def\layersep{2cm}
\def\hsep{1cm}
\def\ilsize{4}
\def\hlsize{8}
\def\olsize{6}
\def\rootlrp{6}
\def\neuronsize{4mm}
\tikzset{>=latex}
\begin{figure}
\centering
\begin{tikzpicture}[shorten >=0pt, ->, draw=black!100, node distance=\layersep,
every pin edge/.style={<-,shorten <=1pt},
neuron/.style={circle, draw, fill=black!100, minimum size=\neuronsize,inner sep=0pt},
input neuron/.style={neuron, fill=black!0},
hidden neuron/.style={neuron, fill=black!0},
output neuron/.style={neuron, fill=black!0}]
\pgfmathsetmacro{\iyshift}{0.5*\ilsize-0.5*\hlsize}
\pgfmathsetmacro{\oyshift}{0.5*\olsize-0.5*\hlsize}
%%%%%%%%%%%%
% DRAW NODES
%%%%%%%%%%%%
% Draw the input layer nodes
\foreach \name / \y in {1,...,\ilsize}
\node[input neuron] (In-\name) at (0.0cm+\hsep,-\y cm+\iyshift cm) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,\hlsize}
\node[hidden neuron] (H0-\name) at (1.5cm+\hsep,-\y cm) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,\hlsize}
\node[hidden neuron] (H1-\name) at (3.0cm+\hsep,-\y cm) {};
% Draw the output layer nodes
\foreach \name / \y in {1,...,\olsize}
\node[hidden neuron] (Out-\name) at (4.5cm+\hsep,-\y cm+\oyshift cm) {};
%%%%%%%%%%%%%%%%%%
% DRAW CONNECTIONS
%%%%%%%%%%%%%%%%%%
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,\ilsize}
\foreach \dest in {1,...,\hlsize}
\path (In-\source) edge (H0-\dest);
% Connect first with second hidden layer
\foreach \source in {1,...,\hlsize}
\foreach \dest in {1,...,\hlsize}
\path (H0-\source) edge (H1-\dest);
% Connect every node from the last hidden layer with the output layer
\foreach \source in {1,...,\hlsize}
\foreach \dest in {1,...,\olsize}
\path (H1-\source) edge (Out-\dest);
\end{tikzpicture}
\end{figure}
\end{document}
答案2
你可以随时回到古老的数学,并添加- 0.5*\hlsize cm + 0.5*\ilsize cm
到是- 输入和输出节点的坐标(如果我没有犯一些令人尴尬的错误,这应该可以正常工作)。我希望这就是你的意思/想要的:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\def\layersep{2cm}
\def\hsep{1cm}
\def\ilsize{4}
\def\hlsize{8}
\def\olsize{6}
\def\rootlrp{6}
\def\neuronsize{4mm}
\tikzset{>=latex}
\begin{figure}
\centering
\begin{tikzpicture}[shorten >=0pt, ->, draw=black!100, node distance=\layersep]
\tikzstyle{every pin edge}=[<-,shorten <=1pt]
\tikzstyle{neuron}=[circle, draw, fill=black!100, minimum size=\neuronsize,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=black!0]
\tikzstyle{hidden neuron}=[neuron, fill=black!0]
\tikzstyle{output neuron}=[neuron, fill=black!0]
%%%%%%%%%%%%
% DRAW NODES
%%%%%%%%%%%%
% Draw the input layer nodes
\foreach \name / \y in {1,...,\ilsize}
\node[input neuron] (In-\name) at (0.0cm+\hsep,-\y cm - 0.5*\hlsize cm + 0.5*\ilsize cm) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,\hlsize}
\node[hidden neuron] (H0-\name) at (1.5cm+\hsep,-\y cm) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,\hlsize}
\node[hidden neuron] (H1-\name) at (3.0cm+\hsep,-\y cm) {};
% Draw the output layer nodes
\foreach \name / \y in {1,...,\olsize}
\node[hidden neuron] (Out-\name) at (4.5cm+\hsep,-\y cm - 0.5*\hlsize cm + 0.5*\olsize cm) {};
%%%%%%%%%%%%%%%%%%
% DRAW CONNECTIONS
%%%%%%%%%%%%%%%%%%
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,\ilsize}
\foreach \dest in {1,...,\hlsize}
\path (In-\source) edge (H0-\dest);
% Connect first with second hidden layer
\foreach \source in {1,...,\hlsize}
\foreach \dest in {1,...,\hlsize}
\path (H0-\source) edge (H1-\dest);
% Connect every node from the last hidden layer with the output layer
\foreach \source in {1,...,\hlsize}
\foreach \dest in {1,...,\olsize}
\path (H1-\source) edge (Out-\dest);
\end{tikzpicture}
\end{figure}
\end{document}