2

I am trying to draw a neural network with a gradient with Tikz. I have the code without errors for the normal neural network, but when I add path fading for the gradient a couple things go wrong. First some of the lines show thicker than the others, and second one of the lines shows "Dimension too large error" (even though this error doesn't show if I remove the path fading option).

I'm at a loss for what to do. Here's the code with the errors:

\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}

I suppose this is some sort of library conflict, but I can't find a workaround. Any help would be appreciated!

2
  • I just realized the line thickness issue was just the preview for the pdf on Overleaf, once I download the pdf all the lines have the same thickness. However I still get the "Dimension too large error" Jun 13, 2023 at 12:53
  • 2
    hidden-4 and output-3 are on the same y level, so the path has no height in the y direction. This probably messes with the fading calculation. It vanishes when a different line width is used, though. I'm more surprised this works at all. I thought this would just fade the path area and not the path itself. (The whole thing is very heavy on the render engine, it seems.) Jun 13, 2023 at 15:02

1 Answer 1

2

It seems that path fading won't work for horizontal lines. I found a workaround by drawing a rectangle of height the same thickness as my lines (0.5pt). The end result is this:

\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}

Code Result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .