我正在尝试创建下图的基于 TikZ 的版本(取自这里),但我不知道该如何处理。
它不需要看起来和原版一模一样,但基本思想应该保留。如果从犚犻sum 函数中的线条都是直线,没有像原始函数那样的“弯曲”。这使得矩阵布局难以使用。
输出函数的定义可以省略。
有人能告诉我在这种情况下可行的方法吗,或者甚至创建一个tikz 示例?
更新:我最终结合了@m0nhawk 和@Toscho 的方法并得出了以下结论:
源代码在这里:https://github.com/dbrgn/blog/blob/master/content/images/2013/3/26/perceptron.tex
答案1
以下是简单的重建。问题:
- 等式左边的,可以很容易地与相加
node
。 - 制作输入和重量和
\foreach
。 - 增加激活函数使其更像原版。
- 改进 TikZ 样式。
代码:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{basic/.style={draw,fill=blue!20,text width=1em,text badly centered}}
\tikzset{input/.style={basic,circle}}
\tikzset{weights/.style={basic,rectangle}}
\tikzset{functions/.style={basic,circle,fill=blue!10}}
\begin{document}
\begin{tikzpicture}
\node[functions] (center) {};
\node[below of=center,font=\scriptsize,text width=4em] {Activation function};
\draw[thick] (0.5em,0.5em) -- (0,0.5em) -- (0,-0.5em) -- (-0.5em,-0.5em);
\draw (0em,0.75em) -- (0em,-0.75em);
\draw (0.75em,0em) -- (-0.75em,0em);
\node[right of=center] (right) {};
\path[draw,->] (center) -- (right);
\node[functions,left=3em of center] (left) {$\sum$};
\path[draw,->] (left) -- (center);
\node[weights,left=3em of left] (2) {$w_2$} -- (2) node[input,left of=2] (l2) {$x_2$};
\path[draw,->] (l2) -- (2);
\path[draw,->] (2) -- (left);
\node[below of=2] (dots) {$\vdots$} -- (dots) node[left of=dots] (ldots) {$\vdots$};
\node[weights,below of=dots] (n) {$w_n$} -- (n) node[input,left of=n] (ln) {$x_n$};
\path[draw,->] (ln) -- (n);
\path[draw,->] (n) -- (left);
\node[weights,above of=2] (1) {$w_1$} -- (1) node[input,left of=1] (l1) {$x_1$};
\path[draw,->] (l1) -- (1);
\path[draw,->] (1) -- (left);
\node[weights,above of=1] (0) {$w_0$} -- (0) node[input,left of=0] (l0) {$1$};
\path[draw,->] (l0) -- (0);
\path[draw,->] (0) -- (left);
\node[below of=ln,font=\scriptsize] {inputs};
\node[below of=n,font=\scriptsize] {weights};
\end{tikzpicture}
\end{document}
图片:
答案2
这里有一个较短的代码,没有定位,因此您可以缩放图片。
\documentclass[tikz]{article}
\usepackage{tikz}
\tikzset{basic/.style={draw,fill=blue!50!green!20,
text badly centered,minimum width=3em}}
\tikzset{input/.style={basic,circle}}
\tikzset{weights/.style={basic,rectangle,minimum width=2em}}
\tikzset{functions/.style={basic,circle,fill=blue!50!green!20}}
\newcommand{\addsymbol}{\draw[thick] (0.5em,0.5em) -- (0,0.5em) --
(0,-0.5em) -- (-0.5em,-0.5em)
(0em,0.75em) -- (0em,-0.75em)
(0.75em,0em) -- (-0.75em,0em);}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\foreach \h [count=\hi ] in {$x_n$,$x_2$,$x_1$,$1$}{%
\node[input] (f\hi) at (0,\hi*2cm-5 cm) {\h};
}
\node[functions] (sum) at (4,0) {$\sum$};
\foreach \h [count=\hi ] in {$w_n$,$w_2$,$w_1$,$w_0$}{%
\path (f\hi) -- node[weights] (w\hi) {\h} (sum);
\draw[->] (f\hi) -- (w\hi);
\draw[->] (w\hi) -- (sum);
}
\node[functions] (step) at (7,0) {};
\begin{scope}[xshift=7cm,scale=.75]
\addsymbol
\end{scope}
\draw[->] (sum) -- (step);
\draw[->] (step) -- ++(1,0);
% Labels
\node[above=1cm] at (f4) {inputs};
\node[above=1cm] at (w4) {weights};
\end{tikzpicture}
\end{document}
答案3
有 PSTricks 但尚未完成...
\documentclass[preview,border=12pt]{standalone}
\usepackage{pst-node}
\usepackage{amsmath}
\psset
{
rowsep=4mm,
colsep=8mm,
mnodesize=10mm,
mnode=r,
}
\newcommand{\C}[1]{[mnode=circle,fillstyle=solid,fillcolor=blue!20]$#1$}
\newcommand{\R}[1]{[mnode=r]\psframebox[fillstyle=solid,fillcolor=blue!20]{\parbox{4mm}{\strut\centering$#1$}}}
\def\Symbol{%
\psset{unit=3mm}
\pspicture(-1,-1)(1,1)
\psset{linewidth=0.4pt}
\psline(-1,0)(1,0)
\psline(0,-1)(0,1)
\psline[linewidth=2\pslinewidth](-0.75,-0.5)(0,-0.5)(0,0.5)(0.75,0.5)
\endpspicture
}
\begin{document}
\offinterlineskip
\begin{psmatrix}
\C{1} & \R{w_0}\\
\C{x_1} & \R{w_1}\\
\C{x_2} & \R{w_2} & \C{\Sigma} & [mnode=circle,fillstyle=solid,fillcolor=blue!20,name=symbol]\Symbol & [mnode=p]\\
\vdots & \vdots\\
\C{x_n} & \R{w_n}\\
\text{inputs} & \text{weights}
\psset{arrows=->}
\ncline{1,1}{1,2}
\ncline{2,1}{2,2}
\ncline{3,1}{3,2}
\ncline{5,1}{5,2}
\ncdiagg{1,2}{3,3}
\ncdiagg{2,2}{3,3}
\ncdiagg{3,2}{3,3}
\ncdiagg{5,2}{3,3}
\ncline{3,3}{3,4}
\ncline{3,4}{3,5}
\end{psmatrix}
\end{document}
答案4
我修改了@Naphat 的解决方案以使其符合 OP 要求的解决方案:
\documentclass[tikz]{article}
\usepackage{tikz}
\tikzset{basic/.style={draw,fill=none,
text badly centered,minimum width=3em}}
\tikzset{input/.style={basic,circle,minimum width=3.5em}}
\tikzset{weights/.style={basic,rectangle,minimum width=2em}}
\tikzset{functions/.style={basic,circle, minimum width=4em}}
\newcommand{\addaxes}{\draw (0em,1em) -- (0em,-1em)
(-1em,0em) -- (1em,0em);}
\newcommand{\relu}{\draw[line width=1.5pt] (-1em,0) -- (0,0)
(0,0) -- (0.75em,0.75em);}
\newcommand{\stepfunc}{\draw[line width=1.5pt] (0.65em,0.65em) -- (0,0.65em)
-- (0,-0.65em) -- (-0.65em,-0.65em);}
\begin{document}
\begin{tikzpicture}[scale=1.2]
% Draw input nodes
\foreach \h [count=\hi ] in {$x_2$,$x_1$,$1$}{%
\node[input] (f\hi) at (0,\hi*1.5cm-1.5 cm) {\h};
}
% Dot dot dot ... x_n
\node[below=0.62cm] (idots) at (f1) {\vdots};
\node[input, below=0.62cm] (last_input) at (idots) {$x_n$};
% Draw summation node
\node[functions] (sum) at (4,0) {\Huge$\sum$};
% Draw edges from input nodes to summation node
\foreach \h [count=\hi ] in {$w_2$,$w_1$,$w_0$}{%
\path (f\hi) -- node[weights] (w\hi) {\h} (sum);
\draw[->] (f\hi) -- (w\hi);
\draw[->] (w\hi) -- (sum);
}
% Dot dot dot ... w_n
\node[below=0.05cm] (wdots) at (w1) {\vdots};
\node[weights, below=0.45cm] (last_weight) at (wdots) {$w_n$};
% Add edges for last node and last weight etc
\path[draw,->] (last_input) -- (last_weight);
\path[draw,->] (last_weight) -- (sum);
% Draw node for activation function
\node[functions] (activation) at (7,0) {};
% Place activation function in its node
\begin{scope}[xshift=7cm,scale=1.25]
\addaxes
% flexible selection of activation function
% \relu
\stepfunc
\end{scope}
% Connect sum to activation function
\draw[->] (sum) -- (activation) node (sum_eq) [midway, below=2cm, scale=1.2] {$\sum\limits_{i=0}^n w_ix_i$} node (sum_activation_midway) [midway, below] {};
\path[draw,->] (sum_eq) -- (sum_activation_midway);
\draw[->] (activation) -- ++(3,0) node (perceptron_output) [below=0.2cm, scale=1.2] {$output = \begin{cases}1 & \text{if }\ \sum\limits_{i=0}^n w_ix_i > 0,\\-1 & \text{otherwise}\end{cases}$};
% Labels
\node[above=1cm] at (f3) {Inputs};
\node[above=1cm] at (w3) {Weights};
\node[above=1.5cm] at (activation) {Activation Function};
\end{tikzpicture}
\end{document}