5
$\begingroup$

I searched "Mathematica LogSumExp", and literally nothing is coming up. It's a relatively important function in machine learning and for numerically stable computation. Will I just have to implement it myself?

$\endgroup$
3
  • $\begingroup$ I wrote a version for the neural net functionality a while back. You can use it as a ResourceFunction $\endgroup$ Commented May 15, 2023 at 15:20
  • $\begingroup$ You could call the NumPy implementation $\endgroup$
    – Alan
    Commented May 15, 2023 at 15:24
  • 5
    $\begingroup$ There's Optimization`LogSumExp. $\endgroup$
    – Michael E2
    Commented May 15, 2023 at 23:57

1 Answer 1

4
$\begingroup$

I just made the answer for myself, since it's not too hard. Still, Mathematica really should provide a native implementation.

LogSumExp[x_] := With[{m = Max[x]}, m + Log[Total @ Exp[x - m]]];

The input x has to be a list (e.g. x={1, 2, 3}. The max stuff is to keep things numerically stable.

$\endgroup$
3
  • 2
    $\begingroup$ It's probably neater to do With[{m = Max[x]}, ...] so you don't have to compute the Max twice $\endgroup$ Commented May 15, 2023 at 16:00
  • 2
    $\begingroup$ Replace Plus @@ with Total @ and it will be faster, esp. on large data. $\endgroup$
    – Michael E2
    Commented May 15, 2023 at 23:53
  • $\begingroup$ LogSumExp[x_] := Log[Total@Exp[x]] $\endgroup$ Commented Jun 2, 2023 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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