如何在 Tikz 中的节点顶部放置抛物线/分布?

如何在 Tikz 中的节点顶部放置抛物线/分布?

我现在正尝试将分布(最好是高斯曲线)放置在 TikzPicture 中的节点顶部。理想情况下,分布应与节点略微垂直偏移,尽管这不是必需的。我尝试扩展的代码如下:

\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=2.5cm, scale=1.5]
\tikzstyle{every pin edge}=[<-,shorten <=1pt]
\tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=green!50];
\tikzstyle{output neuron}=[neuron, fill=red!50];
\tikzstyle{hidden neuron}=[neuron, fill=blue!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, pin=left:Input \y] (I-\name) at (0,-\y) {};

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

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

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

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

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

导致

1层神经网络

我理想中的情况类似于以下内容:

所需图像

答案1

您可以neuron使用以下path picture命令将其嵌入到样式中:

neuron/.style={
    circle,fill=black!25,minimum size=17pt,inner sep=0pt,
    path picture={
        \draw[red,thick,-] plot[domain=-0.3:0.3,samples=11,smooth] ({\x},{0.05*g(\x)});
    },
},

我将高斯定义为

\tikzset{
    declare function={
        sig = 0.1;
        mu = 0;
        g(\x) = 1/(sig*sqrt(2*pi)) * exp(-1/2 * ((\x-mu)/sig)^2);
    }
}

其结果是:

在此处输入图片描述

请注意,我还更改了您的\tikzstyle命令,因为它被视为已弃用。完整的 MWE:

\documentclass[tikz,margin=2mm]{standalone}

\usepackage{tikz}

\tikzset{
    declare function={
        sig = 0.1;
        mu = 0;
        g(\x) = 1/(sig*sqrt(2*pi)) * exp(-1/2 * ((\x-mu)/sig)^2);
    }
}

\begin{document}

\begin{tikzpicture}[
    shorten >=1pt,
    ->,
    draw=black!50,
    node distance=2.5cm,
    scale=1.5,
    every pin edge/.style={<-,shorten <=1pt},
    neuron/.style={
        circle,fill=black!25,minimum size=17pt,inner sep=0pt,
        path picture={
            \draw[red,thick,-] plot[domain=-0.3:0.3,samples=11,smooth] ({\x},{0.05*g(\x)});
        },
    },
    input neuron/.style={neuron, fill=green!50},
    output neuron/.style={neuron, fill=red!50},
    hidden neuron/.style={neuron, fill=blue!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, pin=left:Input \y] (I-\name) at (0,-\y) {};

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

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

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

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

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


\end{tikzpicture}

\end{document}

答案2

代码很糟糕,但是可以工作(节点样式 - 仅在使用比例因子不等于 0 或 1 的比例盒以及一些其他错误时才有效):

\documentclass[border=.5cm,tikz]{standalone}
\tikzset{
    neuron/.style={
        append after command={
            \pgfextra{
                \node[thick,draw=blue!70,fill=blue!20,circle,inner sep=1cm] (a) at (\tikzlastnode) {};
                \draw[thick,red] (a.west) parabola ([xshift=1cm,yshift=.5cm]a.west) to[bend left=50] ([xshift=-1cm,yshift=.5cm]a.east);
                \draw[thick,red] (a.east) parabola ([xshift=-1cm,yshift=.5cm]a.east);
            }
        }
    }
}
\begin{document}
    \begin{tikzpicture}
        \scalebox{.5}[.5]{\node[neuron] at (0,0) {};}
    \end{tikzpicture}
\end{document}

输出:

截屏

相关内容