In the MSE below, I define a function logsumexp as [declare function={logsumexp(\x)=\log(\sum{\exp^{\x_i}});}] to help in plotting the softmax activation function.
When I used the function, to add plot \addplot[blue,smooth] {exp(x) /logsumexp(x))}; everything messed-up.
MSE: (\addplot line commented out for the softmax function)
\documentclass[11pt]{article}
\usepackage{subfigure}
\usepackage{pgfplots}
\usepackage[top=3cm,left=3cm,right=3cm,bottom=3cm]{geometry}
% Scriptsize axis style.
\pgfplotsset{every axis/.append style={tick label style={/pgf/number format/fixed},font=\scriptsize,ylabel near ticks,xlabel near ticks,grid=major}}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{figure}[t!]
\centering
\subfigure[sigmoid activation function.]{
\begin{tikzpicture}[declare function={sigma(\x)=1/(1+exp(-\x));}]
\begin{axis}[width=5.5cm,height=6cm,ylabel=$\sigma(z)$,xlabel=$z$,ymin=0,ymax=1.25,xmin=-5,xmax=5]
\addplot[blue,smooth] {1/(1+exp(-x))};
\end{axis}
\end{tikzpicture}
}
\subfigure[Softmax activation function. ]{
\begin{tikzpicture}[declare function={logsumexp(\x)=\log(\sum{\exp^{\x_i}});}]
\begin{axis}[width=5.5cm,height=6cm,ylabel=$ \sigma(z)_j$,xlabel=$z$,ymin=-1.25,ymax=1.25,xmin=-5,xmax=5]
%\addplot[blue,smooth] {exp(x) /logsumexp(x))};
\end{axis}
\end{tikzpicture}
}
\caption[Activation functions.]{Sigmoid and Softmax activation functions}
\label{fig:sigmoid-tanh}
\end{figure}
logsumexp function: $logsumexp(x)=\log(\sum{\exp^{x_i}})$
\end{document}
when \addplot in uncommented, everything messed-up. What am I missing?
Check that your $'s match around math expressions. If they do, then you've probably used a symbol in normal text that needs to be in math mode. Symbols such as subscripts ( _ ), integrals ( \int ), Greek letters ( \alpha, \beta, \delta ), and modifiers (\vec{x}, \tilde{x} ) must be written in math mode. See the full list here.If you intended to use mathematics mode, then use $ … $ for 'inline math mode', $$ … $$ for 'display math mode' or alternatively \begin{math} … \end{math}.
EDIT
Giving an example with some values for x.
import numpy as np
x = [1.2, 2.5, 3.1, 4.4, 1.6, 2.4, 3.6]
np.exp(x) / np.sum(np.exp(x))
array([0.01933382, 0.07094152, 0.12926387, 0.47430749, 0.02884267,
0.06419054, 0.21312009])



declare function={logsumexp(\x)=\log(\sum{\exp^{\x_i}});. First of all, the\in\logand\expare wrong, these are instructions to typset these functions. But here at least functionslogandexpexist. Then the\in\sumis also wrong, and there is, as of now, no corresponding function implemented in pgf. (\exp^{\x_i}would also be typographically unfortunate.) – Dec 08 '19 at 02:17x+i, you can define something that plots the function you want to plot. Also consider using\DeclareMathOperator{\logsumexp}{logsumexp}for typesetting this combination. – Dec 08 '19 at 02:22sigma(\x)=1/(1+exp(-\x));. Even though you end up plotting1/(1+exp(-x)), you could plotsigma(x). There you correctly use theexpfunction, which is implemented in pgf. As I said, you could implement the second function in pgf, too, if it is well defined, i.e. if it is clear what thex_iare. – Dec 08 '19 at 02:40softmaxis the most tricky function to plot in LaTeX. My search for similar plot did not return any such. – arilwan Dec 08 '19 at 09:14x_iare. If you specify them, one could write something. Without this information, it is impossible. – Dec 08 '19 at 13:37x_itakes the list[1, 2, 3, 4, ,5 ,6]– arilwan Dec 08 '19 at 17:16xwhich one can plot. That is, if one just sums overx_i\in {1, 2, 3, 4, ,5 ,6}, one gets a constant. – Dec 08 '19 at 17:19logsumexpis not the softmax function. – DJP Dec 08 '19 at 17:46xand computedsoftmax– arilwan Dec 08 '19 at 17:47sagetexpackage gives you a computer algebra system, SAGE, along with the ability to run Python. My answer to plotting the Weierstrass function, here shows how those points can be used in plotting. I'm sure the whizzes here can work around having to use it, especially if you fix the length of a vector, but really a CAS, Python, and LaTeX is a better tool for the task. – DJP Dec 08 '19 at 17:53