我正在尝试使用 Tikz 绘制一个带渐变的神经网络。我有正常神经网络的代码,没有错误,但是当我为渐变添加路径渐变时,出现了一些问题。首先,一些线条比其他线条更粗,第二条线条显示“尺寸过大错误”(即使我删除路径渐变选项,也不会显示此错误)。
我不知道该怎么办。以下是包含错误的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\definecolor{my_grad_1}{HTML}{3A1C71}
\definecolor{my_grad_2}{HTML}{D76D77}
\definecolor{my_grad_3}{HTML}{FFAF7B}
\begin{document}
\tikzset{%small neurons for the input and hidden layers
small neuron/.style={
circle,
draw,
minimum size=0.75cm
},
big neuron/.style={%big neurons for the output layer
circle,
draw,
minimum size=3cm,
}
}
%% Tikzpicture for the neural Network
\begin{tikzpicture}
\foreach \m [count=\y] in {1,...,10}{
\pgfmathsetmacro\z{4.25+\y}
\node [small neuron, color = my_grad_1] (input-\m) at (0, \z) {};
}
\foreach \m [count=\y] in {1,...,7}{
\pgfmathsetmacro\z{5.75+\y}
\node [small neuron, color = my_grad_2] (hidden-\m) at (3, \z) {};
}
\foreach \m [count=\y] in {1,...,5}{
\pgfmathsetmacro\z{3.25*\y}
\node [big neuron, color= my_grad_3] (output-\m) at (8, \z) {};
}
\foreach \i in {1,...,10}
\foreach \j in {1,...,7}{
\draw [path fading = east, color = my_grad_1, line width = 0.5pt] (input-\i) -- (hidden-\j);
\draw [path fading = west, color = my_grad_2, line width = 0.5pt] (input-\i) -- (hidden-\j);
}
\foreach \i in {1,...,7}
\foreach \j in {1,...,5}{
\draw[path fading = east, color=my_grad_2, line width = 0.5pt] (hidden-\i) -- (output-\j);
\draw[path fading = west, color=my_grad_3, line width = 0.5pt] (hidden-\i) -- (output-\j);
}
\end{tikzpicture}
\end{document}
我猜这是某种库冲突,但我找不到解决方法。如能得到任何帮助我将不胜感激!
答案1
路径淡入淡出似乎不适用于水平线。我找到了一种解决方法,即绘制一个高度与线条粗细相同的矩形(0.5pt)。最终结果是这样的:
\documentclass[tikz, border = 1mm]{standalone}
\usetikzlibrary{fadings}
\definecolor{my_grad_1}{HTML}{3A1C71} % purple
\definecolor{my_grad_2}{HTML}{D76D77} % pink
\definecolor{my_grad_3}{HTML}{FFAF7B} % orange
\begin{document}
\tikzset{%small neurons for the input and hidden layers
small neuron/.style={
circle,
draw,
minimum size=0.75cm,
},
big neuron/.style={%big neurons for the output layer
circle,
draw,
minimum size=3cm,
}
}
%% Tikzpicture for the neural Network
\begin{tikzpicture}
\draw [left color = my_grad_2, right color = my_grad_3, draw = none] (3, 9.75cm-0.2pt) rectangle (6.5, 9.75cm+0.2pt);
\foreach \m [count=\y] in {1,...,10}{
\pgfmathsetmacro\z{4.25+\y}
\node [small neuron, color = my_grad_1, fill = white] (input-\m) at (0, \z) {};
}
\foreach \m [count=\y] in {1,...,7}{
\pgfmathsetmacro\z{5.75+\y}
\node [small neuron, color = my_grad_2, fill = white] (hidden-\m) at (3, \z) {};
}
\foreach \m [count=\y] in {1,...,5}{
\pgfmathsetmacro\z{3.25*\y}
\node [big neuron, color= my_grad_3, fill = white] (output-\m) at (8, \z) {};
}
\foreach \i in {1,...,10}
\foreach \j in {1,...,7}{
\draw [path fading = east, color = my_grad_1, line width = 0.5pt] (input-\i) -- (hidden-\j);
\draw [path fading = west, color = my_grad_2, line width = 0.5pt] (input-\i) -- (hidden-\j);
}
\foreach \i in {1,2,3,5,6,7}
\foreach \j in {1,...,5}{
\draw[path fading = east, color=my_grad_2, line width = 0.5pt] (hidden-\i) -- (output-\j);
\draw[path fading = west, color=my_grad_3, line width = 0.5pt] (hidden-\i) -- (output-\j);
}
\foreach \j in {1,2,4,5}{
\draw[path fading = east, color=my_grad_2, line width = 0.5pt] (hidden-4) -- (output-\j);
\draw[path fading = west, color=my_grad_3, line width = 0.5pt] (hidden-4) -- (output-\j);
}
\end{tikzpicture}
\end{document}