修复神经网络图上的对齐

修复神经网络图上的对齐

我正在研究一些简单的神经网络图,但在对齐工作方面遇到了一些麻烦。有人能帮我弄清楚如何让隐藏节点和输出节点的对齐工作正常吗?我只是不确定如何处理这些图中的偏移量。

这是图片,后面是代码。

在此处输入图片描述

\documentclass[12pt, a4paper, oneside, headinclude, footinclude]{article}

\usepackage{tikz}
\usetikzlibrary{shapes.multipart, positioning, decorations.markings,
  arrows.meta, calc, fit}
\def\layersep{3.5cm}

\begin{document}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=25pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=red!50];
    \tikzstyle{output neuron}=[neuron, fill=orange!50];
    \tikzstyle{hidden neuron}=[neuron, fill=green!50];
    \tikzstyle{annot} = [text width=4em, text centered]

    % Draw the input layer nodes
    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron] (I-\name) at (0,-\y) {$x_{\y}$};

    % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,3}
        \path[yshift=0.5cm]
            node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};

    % Draw the output layer node
    \node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-2] (O) {};

    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,4}
        \foreach \dest in {1,...,3}
            \path (I-\source) edge (H-\dest);

    % Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,3}
        \path (H-\source) edge (O);

    % Annotate the layers
    \node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
    \node[annot,left of=hl] {Input layer};
    \node[annot,right of=hl] {Output layer};
\end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta,shapes,shapes.multipart, positioning, decorations.markings,arrows.meta, calc, fit}
\def\layersep{2.5cm}

    \begin{tikzpicture}[
        shorten >=1pt,->,draw=black!50, 
        node distance=\layersep,
        every pin edge/.style={<-,shorten <=1pt},
        neuron/.style={circle,fill=black!25,minimum size=25pt,inner sep=0pt},
        input neuron/.style={neuron, fill=red!50},
        output neuron/.style={neuron, fill=orange!50},
        hidden neuron/.style={neuron, fill=green!50},
        annot/.style ={text width=4em, text centered}
        ]
        % Draw the input layer nodes
        \foreach \name / \y in {1,...,4}
        % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron] (I-\name) at (0,-\y) {$x_{\y}$};
        
        % Draw the hidden layer nodes
        \foreach \name / \y in {1,...,3}
        \path[yshift=-0.5cm]
        node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
%       
%       % Draw the output layer node
        \node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-2] (O) {};
%       
%       % Connect every node in the input layer with every node in the
%       % hidden layer.
        \foreach \source in {1,...,4}
        \foreach \dest in {1,...,3}
        \path (I-\source) edge (H-\dest);
%       
%       % Connect every node in the hidden layer with the output layer
        \foreach \source in {1,...,3}
        \path (H-\source) edge (O);
%       
%       % Annotate the layers
        \node[annot,above=1cm of H-1, node distance=1cm] (hl) {Hidden layer};
        \node[annot,left=1cm of hl] {Input layer};
        \node[annot,right=1cm of hl] {Output layer};
    \end{tikzpicture}
\end{document}

相关内容