[MMA 11.0.1] I have defined various activation functions, and plotted them using a little helper to put the equation into the plot

tanmoid[x_] := tanh[x] sigmoid[x];

This is the helper function

fnDef[f_] :=  Block[{x}, SymbolName[f] <> " ="  (f[x] // TraditionalForm) /. E -> "e"]

(the substition replaces the fancy "e", which looks like a graphics error, with a plain "e")


Block[{imSize = 200},
    Plot[{tanmoid'[x], tanmoid[x]}, {x,-4.2,4.2}, 
        PlotLabel-> Pane[Style[fnDef@tanmoid, Bold, 12], Alignment->Right,ImageSize -> imSize], (* MMA SE  *)
        PlotRange->{Automatic, {-1.2,1.2}}, 
        PlotStyle->{{RGBColor[0.58847, 0.22163, 0.16064], Thickness[0.01], Dashing[{0.01,0.01}]}, {ColorData[97,"ColorList"][[1]], Thickness[0.015]}},
        Ticks->{Range[-4,4], {-1, -0.5, 0.5 ,1}}]


enter image description here

This is all good - the problem comes when I want to plot (with Plot3d) the softmax activation function defined thus:

softmax[x_List] := Table[Exp[x[[i]]]/Total@Exp@x, {i, 1, Length[x]}]

in this instance the helper function fnDef[softmax] does not give me the expression for softmax.

I tried to force a list through but this doesn't work either:

listFnDef[f_] :=  Block[{x}, SymbolName[f] <> " ="  (f[List @@ x] // TraditionalForm) /. E -> "e"]

Question how can I produce the expression of softmax to use in e.g. a plot label?


1 Answer 1


Here's one possible way to do it:

listFnDef[f_, n_Integer : 3, ellipsis : _?(BooleanQ) : True] :=  
  Row[{SymbolName[f] <> "(", 
       TraditionalForm[If[ellipsis, Append["..."], Identity] @ Table[Subscript[x, i], {i, 1, n}]],
       ") = ",
       (If[ellipsis, Append["..."], Identity] @ f[Table[Subscript[x, i], {i, 1, n}]] // TraditionalForm) /. E -> "e"}]]

some tests of the above code with their outputs

  • $\begingroup$ That's interesting, but a) I would rather see the summation as using a summation symbol and b) the summation should also contain ellipsis as examples with ellipsis shouldn't end with the final explicit x. I would like to think that MMA is perfectly capable of doing this without all the hand coding to reproduce list structures. If nothing else presents itself this does show one way to approach the question; thanks. $\endgroup$ May 1, 2021 at 9:00
  • $\begingroup$ @JulianMoore hmm, right, the termination of the sum on the bottom slipped past me. I think to get the summation symbol Sum as opposed to Total might need to be used...I'll come back to try to fix it if I have the chance! $\endgroup$
    – thorimur
    May 1, 2021 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.