I am trying to produce the expression for the softmax of a vector in sympy. I am doing the following:
<code>import sympy as sp
# vector
x = sp.MatrixSymbol('x', p, 1)
# take the elementwise exponential
exp_x = x.applyfunc(sp.exp)
# take the sum across elements
sum_x = sp.Sum(exp_x, (j, 1, n))
</code>
<code>import sympy as sp
# vector
x = sp.MatrixSymbol('x', p, 1)
# take the elementwise exponential
exp_x = x.applyfunc(sp.exp)
# take the sum across elements
sum_x = sp.Sum(exp_x, (j, 1, n))
</code>
import sympy as sp
# vector
x = sp.MatrixSymbol('x', p, 1)
# take the elementwise exponential
exp_x = x.applyfunc(sp.exp)
# take the sum across elements
sum_x = sp.Sum(exp_x, (j, 1, n))
The code above works as expected, however, I get a strange NotImplemented
error when I try to divide exp_x
by sum_x
to obtain the softmax result.
NotImplementedError: noncommutative scalars in MatMul are not supported.
Notably if I try to divide exp_x
by any other scalar symbol, the error doesnt show up. For example, exp_x/Symbol('c')
does not throw an error. Is this a bug or a feature? What is the workaround?