如何为节点添加边框

如何为节点添加边框

如何给节点添加细边框?

Tikz 图表

我尝试在几个可能的地方添加thinline width属性,但无济于事。我想这很简单,但我是 Tikz 的初学者。

代码:

\begin{tikzpicture}[shorten >=0pt,->,draw=black!80, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,>=stealth]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=green!60];
    \tikzstyle{output neuron}=[neuron, fill=red!60];
    \tikzstyle{hidden neuron}=[neuron, fill=blue!60];
    \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 1 nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden neuron] (H1-\name) at (\layersep,-\y cm) {};

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

    % Draw the output layer nodes
    \foreach \name / \y in {1,2}
        \path[yshift=-1cm]
            node[output neuron,pin={[pin edge={->}]right:Output \y}] (O-\name) at (3*\layersep,-\y cm){};    

    % Connect every node in the input layer with every node in the 1st hidden layer.
    \foreach \source in {1,...,4}
        \foreach \dest in {1,...,5}
            \draw [->,>=stealth] (I-\source) -- (H1-\dest);

    % Connect every node in the 1st hidden layer with every node in the 2nd hidden layer.
    \foreach \source in {1,...,5}
        \foreach \dest in {1,...,5}
            \draw [->,>=stealth] (H1-\source) -- (H2-\dest);

    % Connect every node in the 2nd hidden layer with the output layer
    \foreach \source in {1,...,5}
        \foreach \dest in {1,2}
            \draw [->,>=stealth] (H2-\source) -- (O-\dest);    

    % Annotate the layers
    \node[annot,above of=H1-1, node distance=1cm] (hl1) {Hidden layer 1};
    \node[annot, above of=H2-1, node distance=1cm] (hl2) {Hidden layer 2};
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl2] {Output layer};
\end{tikzpicture}

答案1

两条评论:

  • 使用\tikzset超过\tikzstyle
  • 用于draw=color绘制边框。

神经网络

代码:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\newlength{\layersep}
\setlength{\layersep}{1.5cm}

\begin{document}
\begin{tikzpicture}[shorten >=0pt,->,draw=black!80, node distance=\layersep]
\tikzset{
    every pin edge/.style={<-,>=stealth},
    neuron/.style={draw,circle,fill=black!25,minimum size=17pt,inner sep=0pt},
    input neuron/.style={neuron, fill=green!60},
    output neuron/.style={neuron, fill=red!60},
    hidden neuron/.style={neuron, fill=blue!60},
    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 1 nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H1-\name) at (\layersep,-\y cm) {};

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

% Draw the output layer nodes
\foreach \name / \y in {1,2}
\path[yshift=-1cm]
node[output neuron,pin={[pin edge={->}]right:Output \y}] (O-\name) at (3*\layersep,-\y cm){};    

% Connect every node in the input layer with every node in the 1st hidden layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\draw [->,>=stealth] (I-\source) -- (H1-\dest);

% Connect every node in the 1st hidden layer with every node in the 2nd hidden layer.
\foreach \source in {1,...,5}
\foreach \dest in {1,...,5}
\draw [->,>=stealth] (H1-\source) -- (H2-\dest);

% Connect every node in the 2nd hidden layer with the output layer
\foreach \source in {1,...,5}
\foreach \dest in {1,2}
\draw [->,>=stealth] (H2-\source) -- (O-\dest);    

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

相关内容